Praveen Kumar

Praveen Kumar

  • 283
  • 6.6k
  • 2.3m

Not receiving exact same message that I sent using SerialPort Class

Apr 28 2022 9:59 AM

C# Console Application

I am using SerialPort class to send/receive message using serial prot communication. Currently I am using Virtual COM Port software to create virtual COM ports in my machine.

What I wanted to achive like follwing:

If I send : Hello Then I should receive: Hello

Problems

I am not able to receive same messge. It is decode into some random any alphabets or special characters.

Instead of Hello I am getting Hiiii or H..... or H@@@@. I sending any random chars

 

You may see the result in the follwing screenshot. Here I am sending Hello but receiving some different chars.

 

//# Configuration
                serialPort = new SerialPort()
                {
                    PortName = "COM1",
                    ReadTimeout = 1500,
                    BaudRate = 9600,
                    DataBits = 8,
                    StopBits = StopBits.One,
                    Parity = Parity.None,

                    Handshake = configuration.Handshake,
                    DtrEnable = true, //Default is false
                    RtsEnable = true,  //Default is false
                    NewLine = Environment.NewLine,
                    //Encoding = Encoding.UTF8;
                };

                //Add Event Listener
                serialPort.DataReceived += new SerialDataReceivedEventHandler(Barcode_Received);


                if (!serialPort.IsOpen)
                {
                    serialPort.Open();
                }
                Console.WriteLine("Connection Established!");
                Console.ReadLine();
                serialPort.Close();

 

Event

private void Barcode_Received(object sender, SerialDataReceivedEventArgs e)
{
     //#Reading barcode data which is comming from Instrument/Device/Beckman
       var serialPort = (SerialPort)sender;
       string data = serialPort.ReadExisting();
}

                

I ahve also tried serialPort.Read(...) instead of ReadExisting() but provides same data