Hi,
my name is Daniel, and Arduino beginner.
I am learning one by one.
Anyway, using the example, I am trying to receive certain bytes properly over the serial terminal. The code is following.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(38400);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Standby");
// set the data rate for the SoftwareSerial port
mySerial.begin(38400);
byte message[3] = {0x81, 0x01, 0x01};
mySerial.write(message, 3);
}
void loop() { // run over and over
int byteRead = mySerial.read();
int x = byteRead;
if ( (x>='0' && x<='9') ? (x-'0') : ( (x>='A' && x<= 'F') ? ( (x-'A')+10):(0) ) )
{
Serial.write(x);
}
if (mySerial.available()) {
Serial.print(x,HEX);
delay(10);
}
}
So using this, I am connected to the serial slave device and can receive data in bytes, print the byte in Hex format. So far so good until this.
Now the slave device always send data with header 0x2E and I want to the serial monitor to print data like
2E23000FA
2E360000FFFFF8
...
..
. (Keep going)
So print received data in a new line with the header(2E).
It looks like your data is being sent as text. Try the second example in Serial Input Basics. In my example it treats the newline character as the end marker.
For testing it would be a good idea to add the following extra line into the example so it prints the Ascii value of every character received
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
Serial.write( rc); Serial.print(' '); // <----- extra code
It's not clear from your Original Post whether the start of your messages is represented by the two characters "2E" or by the decimal value 46 which is the Ascii code for a decimal point. If it is the latter then try my example with '.' as the end-marker.
In either case if you need more help provide a sample of the output.
If I understand, the slave device is sending raw bytes to mySerial, then you want to print the byte values in hex to serial monitor, and print a new line when you receive the raw byte 0x2E.
If this is the case, then this loop procedure should work (but is not tested).
// if you want to print a single character between consecutive bytes, change byteSeparator from 0 to your separator
// for example, if you want a comma, change to
// const byte byteSeparator = ',';
// if you want a space, change to
// const byte byteSeparator = ' ';
// 0 -> does not print a separator between consecutive bytes
const byte byteSeparator = 0;
void loop() {
// check if at least 1 byte is available in the serial buffer
if (mySerial.available() > 0)
{
// 1 or more bytes available
// read the first byte
byte x = mySerial.read();
if (x == 0x2E)
{
// the byte is 0x2E, print a new line followed by 2E
Serial.println();
Serial.print("2E");
}
else
{
// the byte is not 0x2E
// print a byte separator if requested
if (byteSeparator != 0)
Serial.write(byteSeparator);
if (x < 16)
{
// if the byte is < 16, Serial.print will print a single hex digit, so print a '0' digit before, to print always 2 hex digits per byte
Serial.print('0');
Serial.print(x, HEX);
}
else
// byte > 16, Serial.print will print 2 hex digits
Serial.print(x, HEX);
}
}
}