指令长度
byte : 8位 00; short:16位:00 00;int:32位 00 00 00 00 long 64位 00 00 00 00 00 00 00 00
指令编写
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms);
bw.Write((byte)0x05); 05
bw.Write((short)(1)); 01 00
bw.Write((byte)0x05); 05
byte [] datas = ms.ToArray(); 即 05 01 00 05
可用 socket 发送datas
comStr.Append(" " + 1.ToString("x2")); x2:转为小写16进制
comStr.Append(" " + 2.ToString("x2"));
comStr.Append(" " + 3.ToString("x2"));
comStr.Append(" " + 4.ToString("x2"));
StringToHexByte(comStr);
字符串转16进制数组
public static byte[] StringToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if (hexString.Length % 2 != 0)
hexString += " ";
var returnBytes = new byte[hexString.Length / 2];
for (var i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}