C# 为什么我会从C中的串行端口得到一些奇怪的值#

C# 为什么我会从C中的串行端口得到一些奇怪的值#,c#,.net,serial-port,C#,.net,Serial Port,我正在做一个程序,它可以处理天平上的数据。电子秤通过USB串行端口连接。 我正在使用此问题中的代码:。 因此,最重要的部分是工作。我从秤上得到的重量是一根绳子 我的问题是,当电子秤向计算机发送重量值(10/s)时,我会得到以下值,例如: 13,0g;13,0g;12,9g,8g;12,7g;2,6g;12,5g;12,4g,3g;2,2g;12,1g 您可以看到,某些值已损坏,有时缺少第一个字符,有时缺少前两个字符。。。 当我尝试使用天平制造商的其他程序读取数值时,其工作正常,所有数值都是正确的

我正在做一个程序,它可以处理天平上的数据。电子秤通过USB串行端口连接。 我正在使用此问题中的代码:。 因此,最重要的部分是工作。我从秤上得到的重量是一根绳子

我的问题是,当电子秤向计算机发送重量值(10/s)时,我会得到以下值,例如:

13,0g;13,0g;12,9g,8g;12,7g;2,6g;12,5g;12,4g,3g;2,2g;12,1g

您可以看到,某些值已损坏,有时缺少第一个字符,有时缺少前两个字符。。。 当我尝试使用天平制造商的其他程序读取数值时,其工作正常,所有数值都是正确的

那么问题是什么呢

我尝试了不同的波特率和其他设置,没有任何帮助。 希望有人能帮助我

下面是我用来从链接页面的串行端口读取数据的代码

using System;
using System.IO.Ports;          //<-- necessary to use "SerialPort"
using System.Windows.Forms;

namespace ComPortTests
{
    public partial class Form1 : Form
    {
        private SerialPort _serialPort;         //<-- declares a SerialPort Variable to be used throughout the form
        private const int BaudRate = 9600;      //<-- BaudRate Constant. 9600 seems to be the scale-units default value
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] portNames = SerialPort.GetPortNames();     //<-- Reads all available comPorts
            foreach (var portName in portNames)
            {
                comboBox1.Items.Add(portName);                  //<-- Adds Ports to combobox
            }
            comboBox1.SelectedIndex = 0;                        //<-- Selects first entry (convenience purposes)
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //<-- This block ensures that no exceptions happen
            if(_serialPort != null && _serialPort.IsOpen)
                _serialPort.Close();
            if (_serialPort != null)
                _serialPort.Dispose();
            //<-- End of Block

            _serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One);       //<-- Creates new SerialPort using the name selected in the combobox
            _serialPort.DataReceived += SerialPortOnDataReceived;       //<-- this event happens everytime when new data is received by the ComPort
            _serialPort.Open();     //<-- make the comport listen
            textBox1.Text = "Listening on " + _serialPort.PortName + "...\r\n";
        }

        private delegate void Closure();
        private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
        {
            if (InvokeRequired)     //<-- Makes sure the function is invoked to work properly in the UI-Thread
                BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));     //<-- Function invokes itself
            else
            {
                int dataLength = _serialPort.BytesToRead;
                byte[] data = new byte[dataLength];
                int nbrDataRead = _serialPort.Read(data, 0, dataLength);
                if (nbrDataRead == 0)
                    return;
                string str = System.Text.Encoding.UTF8.GetString(data);
                textBox1.Text = str.ToString();
            }
        }
}
使用系统;

使用System.IO.Ports// 我只需要添加以下行来缓冲缓存文件中的值

现在它工作得很好!谢谢

        File.AppendAllText("buffer1.txt", str);

        string strnew = File.ReadLines("buffer1.txt").Last();

        textBox5.Text = strnew;

你能描述一下你是如何从这个代码到问题中以分号分隔的列表的吗?“消息”开头缺少数据的一个可能原因是,您在一次读取中读取了“下一条”消息的一些数据,但最终以某种方式将其丢弃。这将导致下一次读取缺少一些初始字符。这些值在文本框中一个接一个地显示…“这将导致下一次读取缺少一些初始字符”那么我必须如何防止这种情况?使用停止位?对等流量控制?您必须存储正在丢弃的数据。在
str
中查找分号,并保存分号后的所有内容,以便在下一次读取之前进行预结束。什么换行符?这是第一次在这个问题的任何地方提到断线。