浅谈多种称重仪的协议对接

    此时此刻山竹强势登陆广东,不便外出,窝在家里准备写一篇博文。今天给大家分享一些关于称重仪表对接协议的相关技术。本智能称重系统中目前已经实现如下型号的仪表的协议对接:
(1).上海耀华XK3190-A6
(2).上海耀华XK3190-A9
(3).上海耀华A27
(4).杭州衡天HT9800
(5).宁波柯力D2002
(6).宁波柯力D2008
(7).梅特勒托利多
(8).金钟XK3102D
(9).北京能克科技
(10).南方衡器XK8142-07
(11).杭州顶松DS822-D6
基本实现原理是通过读取串口数据,根据协议中约定的数组长度解析接收到的数组,通过注册事件的方式供外部程序调用,下面列举几种协议的解析方法。
(A).上海耀华XK3190

      /// <summary>
        /// 耀华TF0称重数据
        /// </summary>
        /// <param name="byteFrame">帧数据</param>
        private void GetWithYHTF0(byte[] byteFrame)
        {
            if (byteFrame == null || byteFrame.Length==0) 
            {
                return;
            }
            //对接收到的数据进行校验
            byte byteVerif = (byte)(byteFrame[1] ^ byteFrame[2] ^ byteFrame[3] ^ byteFrame[4] ^ byteFrame[5] ^ byteFrame[6] ^ byteFrame[7] ^ byteFrame[8]);
            //校验高位
            byte verifHigh = (byte)((byteVerif & 0xf0) >> 4);
            //校验低位
            byte verifLow = (byte)(byteVerif & 0x0f);

            if (verifHigh > 9)
                verifHigh = (byte)(verifHigh + 0x37);
            else
                verifHigh = (byte)(verifHigh + 0x30);

            if (verifLow > 9)
                verifLow = (byte)(verifLow + 0x37);
            else
                verifLow = (byte)(verifLow + 0x30);

            if (byteFrame[9] == verifHigh && byteFrame[10] == verifLow)
            {
                List<byte> listDigit = new List<byte>() { (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x33, (byte)0x34, (byte)0x35, (byte)0x36, (byte)0x37, (byte)0x38, (byte)0x39 };

                StringBuilder sbDigit = new StringBuilder();
                //获取称重数据
                for (int i = 2; i < 8; i++)
                {
                    if (!listDigit.Contains(byteFrame[i]))
                        byteFrame[i] = (byte)0x30;

                    sbDigit.Append(byteFrame[i] - 0x30);
                }
                //小数点位置
                int dotPos = byteFrame[8] - 0x30;
                int exponent = -dotPos;
                double weightValue = Convert.ToInt32(sbDigit.ToString()) * Math.Pow(10, exponent);
                //负数处理
                if (byteFrame[1] == 0x2D)
                    weightValue = -weightValue;
                //注册外部事件
                if (this.OnShowWeight != null)
                    this.OnShowWeight(this.deviceNo, weightValue);
            }
        }

(B).杭州衡天
 

 /// <summary>
        /// 衡天TF0称重数据
        /// </summary>
        /// <param name="byteFrame">帧数据</param>
        private void GetWithHTTF0(byte[] byteFrame)
        {
            //获取称重数据
            int num0 = this.BCDToInt(byteFrame[2]);
            int num1 = this.BCDToInt(byteFrame[3]);
            int num2 = this.BCDToInt(byteFrame[4]);

            double weightValue = num2 * 10000 + num1 * 100 + num0;

            byte byteState = (byte)(byteFrame[1] & 0x07);
            switch (byteState)
            {
                case 2:
                    weightValue /= 10;
                    break;
                case 3:
                    weightValue /= 100;
                    break;
                case 4:
                    weightValue /= 1000;
                    break;
                case 5:
                    weightValue /= 10000;
                    break;
            }

            byte byteValue = (byte)(byteFrame[1] & 0x20);
            if (byteValue == 0x20)
                weightValue = -weightValue;

            if (this.OnShowWeight != null)
                this.OnShowWeight(this.deviceNo, weightValue);
        }

如需其他型号仪表对接协议相关代码请发邮件到:yafyr@163.com 或者微信:15602907863

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yafyr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值