Hello.
I use Arduino MEGA 2560 to send bytes for communicating with my device via UART (TX-RX). Here's my code.
byte crypt[8] = {0x00,0x03,0x03,0xE7,0x00,0x0A,0x74,0x6F};
void setup() {
Serial.begin(19200);
Serial1.begin(19200);
for (int i = 0;i<8;i++){
Serial1.write(crypt[i]);
}
}
void loop() {
if (Serial1.available()>0){
int incomingbyte = Serial1.read();
Serial.print(incomingbyte);
}
}
When I send bytes and look in the serial monitor, I will get the respond from it for example 1325 and then I got 00000000... I think because it cannot receive any data from the port. How can I prevent the '0' printing in the serial monitor ?
Thank you and sorry for my bad English.
I connect the Serial1 to some part of airconditioner based on Modbus RTU (It has protocol). I expected the bits that can show the status of it back to Serial.read(). Like I mentioned before, I got bits back but the serial monitor keeps printing the 000000.... for example 13250000000000... I only want the bits like 1325. Maybe I can use SerialEvent() ?
Edit: My guess is this answers your question but doesn’t solve your problem.
Your real problem is “how do I read and display Modbus protocol messages”.
Also, is the a/c serial outputting RS232 logic levels (up to +/- 15v), or TTL (0v to Vcc)?
If RS232 (most likely) and you have connected directly to the Arduino TX/RX pins (a common mistake) then you will be reading nonsense and you could well have damaged the Arduino.
You need a MAX232 (or similar) converter board.
Yes, please tell us that you have a converter between the Modbus RTU output and Serial1.
If you do, and are actually getting legitimate TTL signals to Serial1, then we can work on the protocol of what bytes you are sending, and what bytes the AC unit should send back.