Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 1 month ago.
serial
I want to put the data from the serial port 1 into the array,then I want to print via the seria port 2!
- include "mbed.h"
DigitalOut led1(LED1); DigitalOut led2(LED2);
Serial device(p28, p27); tx, rx Serial pc(USBTX, USBRX); char buf[120]; char dat[150]; int i=0; char flag=0; void callback() { Note: you need to actually read from the serial to clear the RX interrupt if (device.readable()){ if there is data waiting buf[i++] = device.getc(); if ( (buf[i-1] == '}' ) && (i>1) && (buf[i-2] == '}') ) { flag=1; memset(buf,0,i); i=0; } } } int main() {
device.baud(9600); pc.baud(9600); device.attach(&callback,SerialBase::RxIrq); while (1) { if(flag){ flag=0; printf(buf); memset(buf,0,i); } led1 = !led1; wait(0.5); } }
The output reault is as follwows:
{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}} //the correct result ///////////////////the error Temerature and humidity///////// {"d": {"Temperatity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}ow_water_level}}{"d": {"Temperatity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}ow_water_level}}{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperatity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}ow_water_level}}{"d": {"Temperatity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}ow_water_level}}{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperatity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}ow_water_level}}{"d": {"Temperatity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}ow_water_level}}{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperatity1":435.2 ,"Wate騵"d": {"Temperature1":0.0,"Humidity1":435.2
I need your help! thank you!
1 Answer
10 years, 1 month ago.
From what I can make of the code after the format has been trashed you are using memset to set buf to all 0's as soon as you get the end of line. That will wipe all the data before you output it so I'm not sure how you get any output at all. I'll assume that that line is supposed to be commented out.
If you want to add a 0 to the end of the string (the standard c string terminator) then once you detect the end use buf[i] = 0; to add a 0.
In the interrupt code replace the if(device.readable()) with while(device.readable()), you want to read all the data waiting, not just the first byte.
flag should be volatile, without it the compiler could optimize things and never check it in the main loop.
There is also an assumption in your code that there is a pause in the incoming data after }}. If there is any data immediately after that then your interrupt will continue to run and overwrite the start of buf with the new input. Then when the main loop gets a chance to run the memset there will wipe buf and so the first byte of buf will be 0 and the next time printf(buf) runs there won't be any output.
Finally I still don't see why you have a wait in there.
Please use
posted by Andy A 03 Oct 2014<<code>> and <</code>>
around your code so that it is readable.