I have an electric meter with DLMS protocol, bus RS 485 and RS 485 converter attached with my computer. I am trying to communicate with DLMS serial.
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace DLMSRS485Console
{
class Program
{
static void Main(string[] args)
{
SerialPort myPort = new SerialPort();
myPort.PortName = "COM5";
myPort.BaudRate = 300;
myPort.Parity = Parity.None;
myPort.StopBits = StopBits.One;
myPort.DataBits = 8;
myPort.Handshake = Handshake.None;
myPort.RtsEnable = true;
myPort.Open();
myPort.WriteLine("/?!\r\n");
myPort.DataReceived += new SerialDataReceivedEventHandler(myPort_DataReceived);
Console.ReadKey();
myPort.Close();
}
private static void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string data = sp.ReadExisting();
Console.Write("Bytes to read : ");
Console.WriteLine(sp.BytesToRead);
while (sp.BytesToRead > 0)
{
Console.WriteLine(data);
}
}
}
}
My problem is:
I run this code and it shows nothing.
The expected output is:
I need any response. I tried it with different ways, read many articles but the problem is not solved. I can use the software ComTestSerial to check either it respond or not. Yes it respond now I want that it send back any reply through my code.
Advance thanks and any response will really appreciated.
Update 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace DLMSRS485Console
{
class Program
{
static void Main(string[] args)
{
SerialPort myPort = new SerialPort("COM5");
//myPort.PortName = "COM5";
myPort.BaudRate = 9600;
myPort.Parity = Parity.None;
myPort.StopBits = StopBits.One;
myPort.DataBits = 8;
myPort.Handshake = Handshake.None;
myPort.RtsEnable = true;
myPort.Open();
myPort.DataReceived += new SerialDataReceivedEventHandler(myPort_DataReceived);
myPort.WriteLine("Hello\n");
Console.Write("Bytes to read : ");
int b = myPort.BytesToRead;
Console.WriteLine(b);
//myPort.WriteLine("rtfm ^^");
Console.ReadKey();
myPort.Close();
}
private static void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string data = sp.ReadExisting();
Console.Write("Bytes to read : ");
Console.WriteLine(sp.BytesToRead);
Console.WriteLine(data);
}
}
}