Modbus通讯协议学习
了解了它,会使你对串口通信有一个清晰的认识!通用消息帧ASCII消息帧(在消息中的每个8Bit 字节都作为两个ASCII字符发送) 十六进制,ASCII字符0...9,A...F 消息中的每个ASCII字符都是一个十六进制字符组成每个字节的位1个起始位n个数据位,最小的有效位先发送1个奇偶校验位,无校验则无1个停止位(有校验时),2个Bit(无校验时)错误检测域LRC(纵向冗长检测) RTU 消息帧8位二进制,十六进制数0...9,A...F 消息中的每个8位域都是一个两个十六进制字符组成每个字节的位1个起始位8个数据位,最小的有效位先发送1个奇偶校验位,无校验则无1个停止位(有校验时),2个Bit(无校验时)错误检测域CRC(循环冗长检测) CRC校验
(/view/1664507.htm) public static string CRCCheck(string val)
{
val = val.TrimEnd(' ');
string[] spva = val.Split(' ');
byte[] bufData = new byte[spva.Length + 2];
bufData = ToBytesCRC(val);
ushort CRC = 0xffff;
ushort POLYNOMIAL = 0xa001;
for (int i = 0; i < bufData.Length - 2; i++)
{
CRC ^= bufData[i];
for (int j = 0; j < 8; j++)
{
if ((CRC & 0x0001) != 0)
{
CRC >>= 1;
CRC ^= POLYNOMIAL;
}
else
{
CRC >>= 1;
}
}
}
return
Maticsoft.DBUtility.HLConvert.ToHex(System.BitConverter .GetBytes(CRC));
}
/// <summary>
/// 例如把如下字符串转换成字节数组
/// AA AA AA AA 0A 00 68 00 06 03 04 54 21 28 22 E5 F3 16 BB BB BB BB 转换为字节数组
/// </summary>
/// <param name="hex">十六进制字符串
</param>
/// <returns></returns>
public static byte[] ToBytesCRC(string hex)
{
string[] temp = hex.Split(' ');
byte[] b = new byte[temp.Length + 2];
for (int i = 0; i < temp.Length; i++)
{
b[i] = Convert.ToByte(temp[i], 16);
}
return b;
}
/// <summary>
/// 将字节数据转换为十六进制字符串,中间用“ ”分割如:AA AA AA AA 0A 00 68 00 06 03 04 54 21 28 22
E5 F3 16 BB BB BB BB
/// </summary>
/// <param name="vars">要转换的字节数组</param>
/// <returns></returns>
public static String ToHex(byte[] vars)
{
return
BitConverter.ToString(vars).Replace('-', ' ').Trim();
}
CS校验(累加和)
public static string CSCheck(string str)
{
if (str.Length == 0) return "";
else str = str.Trim();
byte[] sss = ToBytes(str);
int n = 0;
for (int i = 0; i < sss.Length; i++)
{
n += sss[i];
}
return ToHex(n);
}
/// <summary>
/// AB CD 12 3B 转换为字节数组
/// </summary>
/// <param name="hex">十六进制字符串
</param>
/// <returns></returns>
public static byte[] ToBytes(string hex)
{
string[] temp = hex.Split(' ');
byte[] b = new byte[temp.Length];
for (int i = 0; i < temp.Length; i++)
{
if (temp[i].Length > 0)
b[i] = Convert.ToByte(temp[i], 16);
}
return b;
}