8 years, 2 months ago.

UART have read data like Arduino serial.read()?

HI! I have question .i want read my sensor,I use getc or gets but it cant recive complete... Does mbed have like arduino serial.read()? it my sensor pms3003 data https://www.dfrobot.com/wiki/index.php?title=PM2.5_laser_dust_sensor_SKU:SEN0177 I only get Start Character

uart

#include "mbed.h"
 
Serial pc(D1,D0);
Serial device(D8,D2);
//Serial uart(D10,D2);

 

DigitalOut led(LED1);
//AnalogIn device(A2);
char buffer[256];
char buf[256];
int timer=200;
int length=0;
long pm25=0;
int count;

int main() {
   while(1){
    pc.printf("in while 1\n");//test
    while(device.getc() !='B')
    {
        pc.printf("in while getc\n");//test
            for(int i=0;i<32;i++){
                buffer[i]=device.getc();
                wait(0.5);
                 }//for
                pc.printf("uart is %s",buffer);
    
   }//while(getc)
            
//pc.printf("%s",buf);

Please post your code and the response you are getting. Simply saying "It doesn't get all the data" doesn't tell anyone anything useful.

posted by Andy A 26 Jan 2016

thanks i post my code

posted by CHANG rozen 26 Jan 2016

2 Answers

8 years, 2 months ago.

Start with removing the very very very slow print statements inside your while loop. You need to fetch (read) the serial data as quickly as possible and the printf statements do not help your case. No issue to print the results once the data is grabbed from your external hardware.

There are buffered serial routines available through MBED such as MODSERIAL which are worth a review:

https://developer.mbed.org/cookbook/MODSERIAL

Using MODSERIAL, the MBED routine will read in the values off the RX line to a local buffer in bulk rather than force you to read in a byte at a time which also would cause great overhead if performing this task manually as you are doing with your present code. I would consider to read into a local buffer then parse the buffer values to see if your condition has been met and exit or loop accordingly.

Please post your results after testing.

I think you'll find that the wait for half a second after each byte causes a slightly larger delay than a printf after the first byte of data ;-)

posted by Andy A 27 Jan 2016
8 years, 2 months ago.

You are looking for a 32 byte data sequence that starts with BM and contains a further 30 bytes of binary data. The data consists of 15 16 bit integers that represent the packet size, 13 data values and a checksum.

When you use printf with a %s to try to print the data it will try to treat the data as text which it isn't. You'll get the B and the M followed by random junk until you happen to have a 0 in the data. Since the first value is the message length which is less than 255 you'll get the 0 almost straight away and it'll stop outputting.

char buffer[32];

int main() {
  buffer[0] = 'B'; // first two bytes are always the same...
  buffer[1] = 'M';
   while(1){
    pc.printf("in while 1\n");//test
    while(device.getc() !='B') {  // loop until the data is "B" just in case we are in the middle of a packet.
    }
    if (device.getc() == 'M') { // next byte was an M... looks like a valid packet.
            for(int i=2;i<32;i++){ // read remaining bytes.
                buffer[i]=device.getc();
            } // full packet now in buffer.
         pc.printf("Got packet\n");
         for (int value = 0; value < 15; value++) {  // display the values
           pc.printf("Value %i = %i\n",value , *(buffer + 2 + value*2));
         }
    }
} // end while 1
} // end main