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:
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?
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.
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");;
}
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);
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.