gpt4 book ai didi

c# - 串行端口返回无效数据

转载 作者:太空宇宙 更新时间:2023-11-03 20:19:56 25 4
gpt4 key购买 nike

我有一台使用串行端口连接到计算机的称重机。这是一台非常古老的机器,我们正试图减轻它的重量并保存在数据库中。
机器返回的重量有?等无效字符,重量显示为??2?0,本来应该是02220.

我知道它与网络搜索结果表明的编码有关。但我不知道我到底错过了什么。

这是我的代码:

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);
}

如何准确确定将应用哪种编码?知道我在这里缺少什么吗?

最佳答案

这可能不是编码问题,而是硬件错误。尝试检测它:

    #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;
}
}

乍一看像是框架错误,因为部分数据似乎是正确的。对于初学者,请确保您的电缆是好的。您也可以尝试使用其他应用程序连接到您的设备,例如腻子。

同时以字节形式读取数据可能是个好主意(然后您可以在显示之前将其转换为十六进制)。这样您就可以了解实际发送的内容。

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();
}

关于c# - 串行端口返回无效数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14253739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com