2

System.io.ports Using rtsenabled Receive an Incorrect Package

 2 years ago
source link: https://www.codesd.com/item/system-io-ports-using-rtsenabled-receive-an-incorrect-package.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

System.io.ports Using rtsenabled Receive an Incorrect Package

advertisements

i am writing a method which sends a particular packet and gets an answer back from an electronic device. I am using system.io.ports with rtsEnabled = true.

The problem is that when entering the application the received packet is altered. I am monitoring what is being received using eltima and the packet received by eltima and that by the application have two bytes different.

The application is changing every byte which is double in hex (example FF or BB) to 3F. The following is what Eltima is giving:

5b 00 00 09 32 13 31 33 35 36 31 39 31 30 30 38 32 35 00 01 ff 64 01 00 bb 0f 5d --Eltima

5B 00 00 09 32 13 31 33 35 30 32 37 31 30 30 38 32 35 00 01 3F 64 01 00 3F 04 5D --MyApplication

The following is my code:

public string MakeSerialConnection(string COM, int baud, string dest)
    {
        SerialPort port = new SerialPort(COM, baud, Parity.None, 8, StopBits.One);
        try
        {
            if (!(port.IsOpen))
            {
                string destination = dest;
                //BUILD PACKET FOR SENDING
                byte[] fullPacket = BuildPacket(destination);
                port.Open();
                port.RtsEnable = false;

                port.Handshake = Handshake.None;

                //SEND PACKET TO DEVICE
                port.Write(fullPacket,0,9);

                #region RECEIVE DATA FROM SERIAL
                //MAKE PROCESS STOP FOR 5sec
                Thread.Sleep(240);

                port.RtsEnable = true;

                Thread.Sleep(1000);
                string reading = port.ReadExisting();
               // int readingint = port.ReadByte();

                    port.Close();
                    port.Dispose();
                    return reading;
                #endregion
            }
            else
            {
                return "";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (port != null)
            {
                //port.Close();
                if (port.IsOpen)
                {
                    port.Close();
                }
                port.Dispose();
            }

        }

Please help me figure out why this is happening


It you look in an ASCII code chart, you'll see the 0x3f is the character code for ?. That's the substitution character you'll get when Encoding.GetString() cannot convert a byte value to a character. Makes sense, the default SerialPort.Encoding is ASCII, 0xff is not a valid character code in that encoding.

The root problem is that you are reading strings. But the device is not sending strings, it is sending bytes. You must use SerialPort.Read() instead of ReadExisting().


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK