9 years, 7 months ago.

serial

How to port into the data that read from the serial port in an array? for eaxmple: int i=0; while(device.readable()){ buf[i++]=device.getc(); } wait(1); printf(buf); but the result are as follows:"

Temperat"{"d": {"Temperat"{"d": {"Temperat"{"d": {"Temperat".2 ,"Water_flow""{"d": {"Temperat"{"d": {"Temperat"{"d": {"Temperat"{"d": {"Temperat"":low_water_leve"{"d": {"Temperat"1":0.0,"Humidity"{"d": {"Temperat"{"d": {"Temperat"{"d": {"Temperat"{"d": {"Temperat,"stress":0,"wate,{"d": {"Temperat,{"d": {"Temperat,{"d": {"Temperat,{"d": {"Temperat,}}{"d": {"Temper,{"d": {"Temperat1":435.2 ,"Water_1{"d": {"Temperat1{"d": {"Temperat1{"d": {"Temperat1{"d": {"Temperat1_level":low_wate1{"d": {"Temperat1rature1":0.0,"Hu1{"d": {"Temperat1{"d": {"Temperat1{"d": {"Temperat1{"d": {"Temperat1ow":0,"stress":01{"d": {"Temperat1{"d": {"Temperat1

Looking forward to the results is so: {"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}}{"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}}{"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}}{"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}}{"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}}{"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}}{"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}}

help?

Question relating to:

1 Answer

9 years, 7 months ago.

int i=0;
while(device.readable()){
   buf[i++]=device.getc();
}
wait(1);
printf(buf);

The code runs a lot faster than the serial port data arrives. This means it will sit in the while loop and read all the the data currently waiting in the serial port receive buffer. At that point there is no more data, there hasn't been time for it to arrive, since the port is no longer readable it will exit the while loop. Since the receive buffer is normally 16 bytes this means you'll get 16 bytes of data each printf. You then wait for a second during that time the next 16 bytes to arrive will go into the receive buffer and then the buffer will start to overflow and you'll lose data.

If you take the wait out then you should get all your data but buf[] will only ever hold 16 bytes at a time.

If you want the full line in the buffer then you need to either know how long it is or use some other indicator that the line is over. e.g.

char buf[64]; // buffer large enough to hold a full line

while (true) {
  int i=0;
 
  while (i < 64) {  // make sure we don't overflow the buffer
    if (device.readable()){               // if there is data waiting
       buf[i++] = device.getc();        // read data
       if ( (buf[i-1] == '}' ) && (i>1) && (buf[i-2] == '}') )   // if we have two }s in a row 
         break;                                                                // exit the while loop
    }
  }
 wait(0.9);  // if the data is coming in at 1 line a second don't wait a full second or you'll have trouble keeping up with the incoming data.
 printf(buf);
}

I tried your method, the results are as follows: {"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperat{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperat{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperat{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperat{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperat{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}{"d": {"Temperat{"d": {"Temperature1":0.0,"Humidity1":435.2 ,"Water_flow":0,"stress":0,"water_level":low_water_level}}

Excuse me, here what should I do?

posted by zhang jun 02 Oct 2014

Well it looks like your lines of data are about 103 characters long so for a start change the buf[64] and while (i<64) lines to be 128 or so rather than 64.

If that doesn't fix it get it to put a new line or some other notable character after printing the buffer out to the terminal so that it's obvious what blocks of data you are getting. You should be able to tell this by the timing that data arrives on the output but I have no way of telling.

What happens if you remove the wait? Why did you have a wait in there anyway, what is it for other that to allow time for your receive buffer to overflow?

posted by Andy A 02 Oct 2014