title: Serial Communication with bus RS 485 and protocol DLMS in C#
body: <p>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.</p>
  
  <p>Code</p>
  
  <pre><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 &gt; 0)
              {
                  Console.WriteLine(data);
              }
          }
      }
  }
  </code></pre>
  
  <p>My problem is:</p>
  
  <p><strong>I run this code and it shows nothing.</strong></p>
  
  <p>The expected output is:</p>
  
  <p>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.</p>
  
  <p>Advance thanks and any response will really appreciated.</p>
  
  <p><strong>Update 1</strong></p>
  
  <pre><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("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);
          }
      }
  }
  </code></pre>