I have 3rd party device which is sending RS485 and each packet start with 126 or 7E. When I try Serial.read(), I am Receiving decimal numbers not HEX, what I am trying to do is to convert that to hex compare it and put it in Array.(So I can analyze it later)
Sample of data retrieved "126516191692127126516191791126126516191694126126516191791126126"
What I need is something like this in array "7E 22FFAF16022161010E051A4BFA650216000000003B0118016D00940094003A0000F3
7E" (this is not above massage)
Below is my Code
#include <SoftwareSerial.h>
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
byte byteSend;
String data;
int inData[250];
byte old_byteSend ;
int start_end = 0x7E;
int index = -1;
void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);
Serial.println("SerialRemote"); // Can be ignored
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
RS485Serial.begin(115200); // set the data rate
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
//Copy input data to output
if (RS485Serial.available() > 0)
{
byteSend = RS485Serial.read();
if (byteSend == 0x7E && old_byteSend == 0x7E ) {
if ( start_end == 0 ) {
//Serial.print("Start");
index++;
inData[index] = old_byteSend;
start_end = 1;
} else if (start_end == 1) {
//Serial.print("END");
// Serial.println(index);
index++;
inData[index] = old_byteSend;
start_end = 0;
index = -1;
}
} else {
index++;
inData[index] = byteSend;
}
old_byteSend = byteSend;
}// End If RS485SerialAvailable
// Serial.println();
for (int i = 0; i < 100; i++)
{
Serial.print(inData[i]);
}
Serial.println();
}//--(end main loop )---
Also how would you remove the extra array indexs with zeros.
Sample of data retrieved "126516191692127126516191791126126516191694126126516191791126126"
How did you get the data? Did you print it using Serial.print? If so, just store the received byte in a byte array and it is done.
Your above text representation is decimal, hex is just a different representation. The number stays the same.
If you receive the character A, the decimal representation is 65 and the hexadecimal representation is 41.
And please be clever and write a space after each byte received; that way you can identify the individual bytes.
Setup a variable like readCount, increment it every time you actually read a character, then during your print loop, use the readCount as the upper bound.
sterretje:
How did you get the data? Did you print it using Serial.print? If so, just store the received byte in a byte array and it is done.
Your above text representation is decimal, hex is just a different representation. The number stays the same.
If you receive the character A, the decimal representation is 65 and the hexadecimal representation is 41.
And please be clever and write a space after each byte received; that way you can identify the individual bytes.
I am getting as decimal, but what I want is HEX so later I can go find data in there
Yes that just printed from serial received, but the problem with decimal I can not figure out the position of actual data since it moves (decimal does not print zeros behind data example: 05=> 5 Then position is moved )