RS232 问题 - 如何将重量读取到 PC [英] RS232 question - how to read weight to PC

查看:58
本文介绍了RS232 问题 - 如何将重量读取到 PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 RS232 连接到 PC 的秤,我发送W"来接收重量.秤在读取时始终发送重量.我如何捕捉正在读取的重量?

我可以获得任何 C# 示例代码吗?

解决方案

发送 W?听起来像是 FedEx 为企业提供的 Mettler Toledo 秤.我碰巧有一些代码可以读取这样的比例:

<预><代码>//其中 this.port 是 SerialPort 的一个实例,即//this.port = new SerialPort(//端口名称,//1200,//Parity.None,//8,//StopBits.One);//this.port.Open();protected override bool GetWeight(输出十进制权重LB,输出布尔稳定){稳定 = 假;重量LB = 0;尝试{字符串数据;this.port.Write("W\r\n");线程.睡眠(500);数据 = this.port.ReadExisting();if (data == null || data.Length < 12 || data.Substring(8, 2) != "LB"){返回假;}if (decimal.TryParse(data.Substring(1, 7), out weightLB)){稳定 = (数据[11] == '0');返回真;}}捕获(超时异常){返回假;}返回假;}

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?

解决方案

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

这篇关于RS232 问题 - 如何将重量读取到 PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆