Change stop bit in Serial.begin

Hello everyone, im new in arduino, and i faced the problem with recieving data from sensor, wich transmitted by uart port. Sensor's data is 48 bits and ending with a stop byte 0x13 and i should print that data into Serial monitor. My code below:

#include <SoftwareSerial.h>

SoftwareSerial mySerial = SoftwareSerial(10, 11);

void setup() 
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() 
{

  if (mySerial.available())
  {
    Serial.print(mySerial.read());
  }
}

I just founded in documentation that in Serial.begin() method i can give two arguments: (bod, config), and that config may be:

#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
#define SERIAL_8N1 0x06
#define SERIAL_5N2 0x08
#define SERIAL_6N2 0x0A
#define SERIAL_7N2 0x0C
#define SERIAL_8N2 0x0E
#define SERIAL_5E1 0x20
#define SERIAL_6E1 0x22
#define SERIAL_7E1 0x24
#define SERIAL_8E1 0x26
#define SERIAL_5E2 0x28
#define SERIAL_6E2 0x2A
#define SERIAL_7E2 0x2C
#define SERIAL_8E2 0x2E
#define SERIAL_5O1 0x30
#define SERIAL_6O1 0x32
#define SERIAL_7O1 0x34
#define SERIAL_8O1 0x36
#define SERIAL_5O2 0x38
#define SERIAL_6O2 0x3A
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E

There is no config variable wich can i use for my purpuse.
How can i set my value in begin method?

No, there is not. Because you are confusing a stop BIT with a message termination BYTE!!!!
Does your documentation ALSO show that a message always begins with a specific byte value?

Please post a link to the sensor product page, data sheet or user manual.

stop bit 0x13 This is the ASCII DC3 (^S) character, often used for message endings.

XOFF

Of course, you are right, its message termination byte.
ADUC814 sensor transmit data, and there are no begin byte value, only end byte - 0x13.
The fact is, this sensor has already been programmed by another person, in short, the implementation is a black box, I only deal with the output value of this sensor, and all I know is that the sensor transmits message in hex format with message terminating byte.

The implementation is black box, i deal with output sensor data, this data is a string in hex format with x013 at the end, as in the screenshot below. This data needs to be parsed through measurement channels and converted to decimal format. In short, I need to display a line in the Serial Monitor as in the screenshot in hex format, and then, the question of how to parse it is not a problem for me.

The Arduino code doesn't really have any concept of "terminating byte"; you have to check for it within your own code.

void loop() {
  if (mySerial.available())
  {
    char c = mySerial.read();
    if (c == 0x13) {
      Serial.println(" <end of message>");
    } else {
      Serial.print(mySerial.read());
    }
  }
}

What about Serial.readBytesUntil(character, buffer, length) with 0x13 as the compare char?

Use the code posted above, but replace this line:

Serial.print(mySerial.read());

with

Serial.print(mySerial.read(), HEX);

You will have to programmatically add leading zeros, if you want them.

Can we say that the code below correctly reads the input data? I think its better than readBytesUntil.

if (mySerial.available())
  {
    String str = (mySerial.readStringUntil("0x13"));
    for (int i = 0; i < str.length(); i ++)
    {
      Serial.print(str[i]);
    }
    Serial.print("\n");;
  }

You wrote in post 1

Sensor's data is 48 bits and ending with a stop byte 0x13

Do you mean 48 byte and a 0x13 to mark end of message?

Otherwise I have problems to match that with the screenshot from post 6.

No. You are not expecting characters but binary data ...

And functions with "readUntil" usually use timeouts so you have to check whether you got everything or not. It is much better to collect data by your own and take action when the terminal character has been received. Even then it is advisable to check whether the number of data read is as expected or not.

P.S.: Based on @westfw 's post and assuming that the message length is 48 byte it might look like this (compiles but not tested);

constexpr byte validLength = 48;
constexpr byte bufferSize = 50;
byte buffer[bufferSize];
int  count = 0;

void loop() {
  if (mySerial.available()) {
    byte c = mySerial.read();
    if (c == 0x13) {
      Serial.println(" <end of message>");
      if (count == validLength) {
        Serial.println("Valid Message Received");
        //
        // Decode message buffer here !
        //
      } else {
        Serial.println("Invalid Message Received");
        Serial.print("No of Bytes Read = \t");
        Serial.println(count);
      }
      count = 0;
    } else {
      Serial.print(c, HEX);
      buffer[count] = c;
      count++;
      if (count >= bufferSize) {
        Serial.println("Buffer Overflow");
        count = 0;
      }
    }
  }
}

Avoid using String objects. They cause memory problems and program crashes. See the Arduino tutorial Serial Input Basics.

see the difference :slight_smile:

If it would be 48 bit, just set validLength to 6.

But I guess you missed to read my post 11? :wink:

That is....
But I think that 48bits is more realistic for sensor data

1. Are you saying that the MicroConverter is sending 48-bit data followed by 0x13 which is message terminating byte?

2. My understanding is that these 48-bit data must be corresponding to three analog inputs. An analog channel is converted to 12-bit data and then a 4-bit (indicating channel number) chunk is appended at the top to make it 16-bit.

3. I strongly believe that the above 48-bit data (3x (16 = 4-hex digit)) is being sent by the MicroConverter over the Serial Port as 12 UART Frames. The message must be started by a preamble byte (something like: STX = 0x02) to synchronize with the receiver and then ended by 0x13 (ETX for End-of-Text).

4. Assume Bd = 9600 (frame length 10-bit: 1-StartBit, 8-CharacterBit, No-ParityBit, and 1-StopBit), the following codes/sketch at the receiver side will put the 3-Channel data into adcData[] array.

#include<SoftWareSerial.h>
SoftwareSerial SoftSerial(2, 3);   //SRX = DPin-2, STX = DpIn-3)
char adcData[15] = {0};
#define STX 0x02
#define ETX 0x13

void setup()
{
     Serial.begin(9600);
     SoftSerial.begin(9600);
}
void loop()
{
     byte n = SoftSerial.available();
     if(n != 0)
     {
          byte y = SoftSerial.read();
          if(y == STX)
          {
               byte m = SoftSerial.readBytesUntil(ETX, adcData, 15);
               adcData[m] = '\0';  //insert null-charcater
               Serial.println(adcData);  //should show 12 charcaters on SM
               //---- now parse data to get individual channel value   
               memset(adcData, 0, 15);    //array clear
               delay(1000);
          }
     }
}

5. If the above idea works, then post what has appeared on the Serial Monitor.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.