RS232 question - how to read weight to PC

我是研究僧i 提交于 2019-12-21 23:13:40

问题


I have a scale that connect to PC through RS232, I send "W" to receive the weight. The scale sends the weight all the time as it's read. How do I catch the weight that is being read?

Can i get any C# sample code?


回答1:


Sending a W? Sounds like the Mettler Toledo scale that FedEx gives businesses. I happen to have some code that reads from such a scale:


// where this.port is an instance of SerialPort, ie
// this.port = new SerialPort(
//  portName,
//  1200,
//  Parity.None,
//  8,
//  StopBits.One);
//  this.port.Open();

protected override bool GetWeight(out decimal weightLB, out bool stable)
{
    stable = false;
    weightLB = 0;

    try
    {
        string data;

        this.port.Write("W\r\n");
        Thread.Sleep(500);
        data = this.port.ReadExisting();

        if (data == null || data.Length < 12 || data.Substring(8, 2) != "LB")
        {
            return false;
        }

        if (decimal.TryParse(data.Substring(1, 7), out weightLB))
        {
            stable = (data[11] == '0');

            return true;
        }
    }
    catch (TimeoutException)
    {
        return false;
    }

    return false;
}



回答2:


You need to use SerialPort component of .NET. The full description and examples are available on MSDN site: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx



来源:https://stackoverflow.com/questions/2038387/rs232-question-how-to-read-weight-to-pc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!