0

I am trying to read data from optical port of electricity meter. I connect the probe attached to the optical port of the meter to the computer via RS232. I am sending the initial data to the electricity meter as follows. The answer should be the ID of the electricity meter.

SerialPort sp = new SerialPort("COM4");
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.DataBits = 7;
sp.Handshake = Handshake.None;
sp.DtrEnable = true;
sp.RtsEnable = true;

sp.DataReceived += SerialDataReceivedEventHandler;

sp.Open();
sp.WriteLine("/?!");
Console.Read();
sp.Close();

When I try with the ReadExisting() method, I get the following response.

SerialPort seri = (SerialPort)sender;
Console.WriteLine(seri.ReadExisting());

enter image description here

When I try with the Read() method, I get the following response.

SerialPort srlport = (SerialPort)sender;
const int bufSize = 12;
Byte[] buf = new Byte[bufSize];
Console.WriteLine("Data Received!!!");
int bytes_read = srlport.Read(buf, 0, bufSize);
Console.WriteLine("Bytes read: " + bytes_read);
Console.WriteLine("Content:\n" + String.Join(" ", buf));

enter image description here

I need to get an ID like "/MSY5<1>C520.KMY.2556" from the electricity meter. Where am I doing wrong? I would be grateful if you help. Best regards.

  • 2
    You're working with too many unknowns at once. Temporarily remove your untested code from this scenario, and replace it with a terminal emulation program such as PuTTY or TeraTerm. Test if your PC has a functioning serial connection to the meter. Manually type the request command, and see what response you get. – sawdust Sep 20 '22 at 06:08

0 Answers0