此时此刻山竹强势登陆广东,不便外出,窝在家里准备写一篇博文。今天给大家分享一些关于称重仪表对接协议的相关技术。本智能称重系统中目前已经实现如下型号的仪表的协议对接:
(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