简体   繁体   中英

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

)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). 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()等

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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