-1

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);
        }
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Salman Mushtaq
  • 341
  • 4
  • 23
  • what do you want to send as reply? – Mong Zhu Dec 06 '16 at 10:47
  • after `ReadExisting()` `sp.BytesToRead` will be `0`. So your while-loop will not be entered. Please remove the while-loop. It is a trap for ever if you should ever get in there – Mong Zhu Dec 06 '16 at 10:49
  • Subscribe to event before sending something (communications are slow, but still). What is "any response"? Set breakpoint on the first line in `DataReceived` event. Nothing? Ensure you are setting com-port correctly and sending correct bytes/text (rtfm ^^). What are you trying to send now: `"/?!\r\n"` may be not what you expect (`\n` **is** already `\xd\xa`). You can use serial-sniffers to intercept and log traffic (do it for `ComTestSerial` as well, maybe there are some important details). You are not setting timeouts, etc. – Sinatr Dec 06 '16 at 10:58
  • @Sinatr, I want to initiate communication with meter. So, this is initial command to start communication. AS DLMS talks as binary string, so I need any data even garbage. To ensure there is communication. – Salman Mushtaq Dec 06 '16 at 11:31
  • And what time out I can set for this? – Salman Mushtaq Dec 06 '16 at 11:33

1 Answers1

1

Problem
I run this code and it shows nothing.

After this line:

string data = sp.ReadExisting();

BytesToRead will be 0. So you will never enter your while-loop to display the read data. Remove the while-loop and just display the data:

private static void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;

    Console.Write("Bytes to read : ");
    Console.WriteLine(sp.BytesToRead);

    string data = sp.ReadExisting();
    Console.WriteLine(data);
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • I can remove the while loop, but still no data. Why DataReceived handler is not trigger. – Salman Mushtaq Dec 06 '16 at 11:29
  • might have a lot of reasons. may be device does not send or com port configuration is wrong? – Mong Zhu Dec 06 '16 at 11:37
  • I can debug control never goes to event handler, then what is the solution of this problem? Any idea – Salman Mushtaq Dec 06 '16 at 11:39
  • When I debug the value for myPort is System.IO.Ports.SerialPort , I have no idea is this fine or here the COM5 comes? – Salman Mushtaq Dec 06 '16 at 11:46
  • How did you find out that the device sends you something? with software ComTestSerial? Did you observe the COM5-Port? Please switch these lines and make first the event `myPort.DataReceived += new SerialDataReceivedEventHandler(myPort_DataReceived);` and then write to the port: `myPort.WriteLine("/?!\r\n");` – Mong Zhu Dec 06 '16 at 12:14
  • Yes through the software, Yes I can Update my code. Please look. – Salman Mushtaq Dec 06 '16 at 12:27
  • how is your device supposed to respond to the "Hello\n"? we had this already... You need a device specification that tells you the communication protocol. Your code looks fine – Mong Zhu Dec 06 '16 at 13:26
  • Protocol is DLMS and bus or medium is RS 485 – Salman Mushtaq Dec 06 '16 at 13:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129924/discussion-between-mong-zhu-and-salman-mushtaq). – Mong Zhu Dec 06 '16 at 15:09