UART sensor doesn't work

Discussion in 'UDOO NEO' started by Gorgo, Dec 5, 2017.

Tags:
  1. Gorgo

    Gorgo UDOOer

    Joined:
    Nov 9, 2016
    Messages:
    153
    Likes Received:
    16
    Hi all,
    today I'm testing a formaldehyde sensor with my UDOO NEO.
    This is the sensor (https://www.seeed.cc/project_detail.html?id=380) and at the same page I found a working Arduino code.
    Now, that uses softwareSerial but I think our Serial0 is fine...

    [​IMG]

    His pinout:
    VCC -> (Arduino) 3V3
    GND -> (Arduino) GND
    TX -> (Arduino) Pin 8
    RX -> (Arduino) Pin 12

    My pinout:
    VCC -> UDOO_3V3
    GND -> UDOO_GND
    TX -> UART5_RX
    RX -> UART5_TX

    His setup (part of it):
    Code:
    SoftwareSerial airSerial(8, 12); // RX, TX
    ...
    Serial.begin(9600);
    airSerial.begin(9600);
    In my setup I used Serial0 instead of airSerial.

    Then, according to the chinese datasheet (sigh!), if you send 0x42, 0x4d, 0x01, 0x00, 0x00, 0x00, 0x90, the sensor replies with the formaldehyde value and other data.

    So this is his request part in the loop:

    Code:
    for (int i=0;i<7;i++) {
       airSerial.write(testCmd[i]);
    }
    // response handle
    int pos = 0;
    for (int i=0;i<20;i++) testResponse[i]=0x00; // clean buffer
    while (airSerial.available() && pos < 20) {
       testResponse[pos] = airSerial.read();
       if (pos == 0 && testResponse[pos] != 0x42) {
          testResponse[pos] = 0; // clear the buffer
       }
    else if (pos == 1 && testResponse[pos] != 0x4d) {
       testResponse[0] = testResponse[1] = 0;
       pos = 0;
    }
    else {
       pos++;
    }
    }
    Basically it writes the bytes and then if the Serial is available, it reads 20 byte. The starting bytes are 0x42 and 0x4d.

    So I changed it a bit with:

    Code:
    char testCmd[7] = {0x42, 0x4d, 0x01, 0x00, 0x00, 0x00, 0x90};
    
    void loop() {
      byte b=0;
      for (int i=0;i<7;i++) {
        Serial0.write(testCmd[i]); Serial0.flush(); //According to https://www.udoo.org/forum/threads/issue-with-serial0-write-0.5135/ I need to flush if I want to send 0x00
      }
     
      // response handle
      int pos = 0;
      for (int i=0;i<20;i++) testResponse[i]=0x00; // clean buffer
    
      //PUT this control because Serial0.available returns false
      while(b != 0x42){
        Serial.println(b, HEX);
        b = Serial0.read();
        delay(100);
      }
    Eventually the code loops without hanging but I'm not reading values from the sensors except for 0xFF.

    Any ideas? :/
     
  2. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,192
    Likes Received:
    557
    I think you have an issue with the known bug writing 0x00b to serial. I believe a Serial0.flush could solve this for Serial0 (but not for normal Serial).

    Edit: I see now you already did that......
     
  3. Gorgo

    Gorgo UDOOer

    Joined:
    Nov 9, 2016
    Messages:
    153
    Likes Received:
    16
    Exactly :( Without flush() the sketch hangs. In my case the sketch sends the bytes but the sensor doesn't reply.
     
  4. Gorgo

    Gorgo UDOOer

    Joined:
    Nov 9, 2016
    Messages:
    153
    Likes Received:
    16

Share This Page