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