/// /// 发送开锁指令 /// /// public void OpenLock(int mbIndex, int lockIndex) { OpenPort(); string command = IntToHex_1_to_24(mbIndex) + "0500" + IntToHex_1_to_24(lockIndex) + "FF00"; //LRC int sum = 0; for (int i = 0; i < command.Length / 2; i++) { string val = command.Substring(i * 2, 2); sum += int.Parse(val, System.Globalization.NumberStyles.HexNumber); } byte result = (byte)((~sum) + 1); command += result.ToString("X2"); command = ((char)0x3A).ToString() + command + ((char)0x0D).ToString() + ((char)0x0A).ToString(); byte[] buffer = Encoding.ASCII.GetBytes(command); try { this.serialPort.Write(command); } finally { string hex_cmd = string.Empty; foreach (byte b in buffer) { hex_cmd += Convert.ToString(b, 16).ToUpper().PadLeft(2, '0'); } Com.DataCool.DotNetExpand.LogHelper.Info(command + "," + hex_cmd); if (!CurrCommandList.Contains(hex_cmd)) CurrCommandList.Add(hex_cmd); } }
不用意思以前的代码没有调试过,误导你了。你看看这段代码。写串口的是十六进制的字符 ,像你这样只需要把“H”去掉就直接发给串口就行了。serialPort.Write(“H23H33H30H47H45”.Replace("H",""));
|