简体   繁体   English

如何使用C#从系统的串行端口读取数据

[英]How to read data from serial port of system using c#

I am try to create software for weighing bridge, while try read data using c# code, its showing some other data something like below mentioned 我尝试创建用于称重桥梁的软件,同时尝试使用c#代码读取数据,其显示了一些其他数据,如下所述

)0 12589 00
)0 12589 00
)0 12589 00
)0 12589 00
)0 12589 00
)0 12589 00
private void timer1_Tick(object sender, EventArgs e)
{
    string Port = GenDbUtility.GetElixirConfigValue("SERIAL_PORT", Globals.CompCode);

    //<-- This block ensures that no exceptions happen
    if (serialPort1 != null && serialPort1.IsOpen)
        serialPort1.Close();
    if (serialPort1 != null)
        serialPort1.Dispose();
    //<-- End of Block


    serialPort1 = new SerialPort("COM1");       //<-- Creates new SerialPort using the name selected in the combobox
    serialPort1.Encoding = Encoding.ASCII;
    serialPort1.BaudRate = 9660;
    serialPort1.Parity = Parity.None;
    serialPort1.StopBits = StopBits.One;
    serialPort1.DataBits = 50;
    serialPort1.Handshake = Handshake.None;
    serialPort1.RtsEnable = true;
    serialPort1.ReadBufferSize = 4096;
    serialPort1.ReceivedBytesThreshold = 100000;
    serialPort1.NewLine = "\r\n";
    serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
    serialPort1.Open();     //<-- make the comport listen
}

void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    if (serialPort1.IsOpen)
    {
        SerialPort sp = (SerialPort)sender;

        string newVal = sp.ReadExisting().ToString();
        ///string[] qty = newVal.Split(' ');
        //txtQuantity.Invoke(this.myDelegate, new Object[] { qty[3].ToString() });   
        //string[] qty = newVal.Split(' ');
        //decimal Quantity1 = Convert.ToDecimal(qty[0]);
        //decimal Quantity2 = Convert.ToDecimal(qty[1]);
        //decimal Quantity3 = Convert.ToDecimal(qty[2]);
        //decimal Quantity4 = Convert.ToDecimal(qty[3]);
        //txtQuantity.Text = Quantity3.ToString();

        //if (String.Compare(txtQuantity.Text, qty[3]) != 0)
        //{
        //    txtQuantity.Text = Convert.ToString(qty[3]);
        //    //lblweight.Text = Convert.ToString(qty[2]);
        //        //qty[2].ToString();
        //}
    }
}

The serial port does not know how long a "message" is. 串行端口不知道“消息”有多长时间。 The device you are reading from will have published some form of protocol you will need to follow for reading in the correct data. 您正在读取的设备将发布某种形式的协议,您需要遵循该协议来读取正确的数据。

You can not assume sp.ReadExisting() will have exactly one message worth's of data, it may have less than a full message and it could combine parts of two messages together and return it as one result (this is the problem you are having). 您不能假设sp.ReadExisting()将只具有一条消息的数据价值,它可能少于一条完整的消息,并且可能将两条消息的一部分合并在一起并作为结果返回(这就是您遇到的问题) 。 Go read the documentation for the weighing bridge and read only the parts out that you should be reading out. 请阅读称重桥的文档,并仅阅读应读取的零件。

com端口可以像驱动器一样打开,例如COM1:而不是例如C:我使用C#的程度不足以了解文件系统功能,但是在C / C ++中,其fopen()等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM