Reputation: 1342
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?
Upvotes: 1
Views: 3673
Reputation: 841
I faced the same problem. Try changing DataBits to 7 and Parity to Parity.Even, then it should work.
Upvotes: 0
Reputation: 5373
It may not by the encoding issue but error on hardware. Try detecting it:
#region comPort_ErrorReceived
/// <summary>
/// This method will be called when there's data waiting in the buffer
/// and error occured.
/// DisplayData is a custom method used for logging
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void comPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
SerialError sr = e.EventType;
switch (sr)
{
case SerialError.Frame:
DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
+ " the hardware detected a framing error.\n", 45);
break;
case SerialError.Overrun:
DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
+ " a character-buffer overrun has occurred. The next character is lost.\n", 46);
break;
case SerialError.RXOver:
DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
+ " an input buffer overflow has occured. There is either no room in the input buffer,"
+ " or a character was received after the End-Of-File (EOF) character.\n", 47);
break;
case SerialError.RXParity:
DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
+ " the hardware detected a parity error.\n", 48);
break;
case SerialError.TXFull:
DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
+ " the application tried to transmit a character, but the output buffer was full.\n", 49);
break;
default:
DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
+ " an unknown error occurred.\n", 50);
break;
}
}
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.
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int btr = comPort.BytesToRead;
byte[] comBuffer = new byte[btr];
comPort.Read(comBuffer, 0, btr);
Console.WriteLine(ByteToHex(comBuffer));
}
private string ByteToHex(byte[] comByte)
{
StringBuilder builder = new StringBuilder(comByte.Length * 3);
foreach (byte data in comByte)
builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
return builder.ToString().ToUpper();
}
Upvotes: 5