I have a weighing machine that is connected to a computer using a serial port. It is a very old machine and we are trying to get the weights off it and save in a database.
The weight returned by the machine has some invalid characters like ?, and the weight is displayed as ??2?0, where it should have been 02220.
I understand it has something to do with encoding as results on a web search suggest. But I can not figure out what exactly am I missing.
Here is my code:
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// This method will be called when there is data waiting in the port buffer
// Read all the data waiting in the buffer
// string data = comport.ReadExisting();
// Display the text to the user in the Rich Text Box
Log(LogMsgType1.Incoming, s);
}
public void OpenThisPort()
{
bool error = false;
// If the port is open, close it
if (comport.IsOpen)
{
comport.Close();
}
else
{
comport.BaudRate = int.Parse("1200");
comport.DataBits = int.Parse("8");
comport.StopBits = StopBits.One;
comport.Parity = Parity.None;
comport.PortName = "COM1";
delStart = 0;
delLength = 9;
comport.RtsEnable = true;
comport.DtrEnable = true;
comport.Encoding = System.Text.Encoding.GetEncoding(28591);
}
How do I determine exactly which encoding will be applied?
Any idea what I’m missing here?
It may not by the encoding issue but error on hardware.
Try detecting it:
At first glance it looks like framing error, as part of the data seem to be correct. For starters make sure that your cable is good. Also you may try connecting to your device with some other application, e.g putty.
Also reading your data as bytes may be a good idea (and then you may convert it to hex before displaying). This way you will find out what is actually getting sent.