I'm a Arduino newbie. I have an Electric meter with an RS485 output. I want to get the MeterID value over the RS485 module and the Arduino. I just have document from manufacturer.
Sent by computer : 68 99 99 99 99 99 99 68 01 02 67 F3 C3 16
Return from meter : FE 68 99 99 99 99 99 99 68 81 08 67 F3 A6 9B 34 4B 33 33 6F 16
My Arduino code:
#include <SoftwareSerial.h>
#define SSerialRX 10
#define SSerialTX 11
#define SSerialTxControl 3
#define RS485Transmit HIGH
#define RS485Receive LOW
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);
byte request[] = {0x68, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x68, 0x01, 0x02, 0x67, 0xF3, 0xC3, 0x16};
int byteRead;
void setup()
{
Serial.begin(9600);
RS485Serial.begin(1200);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive);
delay(20);
Serial.println("Get Meter Address App");
}
void loop()
{
digitalWrite(SSerialTxControl, RS485Transmit);
RS485Serial.write(request, sizeof(request));
delay(2000);
digitalWrite(SSerialTxControl, RS485Receive);
delay(2000);
if (RS485Serial.available())
{
byteRead = RS485Serial.read();
Serial.println(byteRead, HEX);
}
}
But Monitor show blank after show "Get Meter Andress App". I'm using combo Arduino Uno R3, MAX485 and Meter. My code is in C# below. I used the code before and it worked fine, but on the computer I use a COM port (Baudrate 1200, data 8bits, Parity EVEN, Stopbit 1) with an RS485-R232 converter:
#Send data
dataOUT = WriteData("68 99 99 99 99 99 99 68 01 02 67 F3 C3 16");
serialPort_Meter.Write(dataOUT, 0, dataOUT.Length);
#WriteData method
private static byte[] WriteData(string hex)
{
return hex.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();
}
#Read data
private static string ReadData(SerialPort serial)
{
byte[] buffer = new byte[serial.BytesToRead];
serial.Read(buffer, 0, buffer.Length);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
sb.AppendFormat("{0:X2} ", buffer[i]);
return sb.ToString();
}