private void btn_send_Click(object sender, EventArgs e) { if (!sport.IsOpen) { MessageBox.Show("请先打开串口设备", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string cmd = txt_cmd.Text; if (cmd.Length != 12) { MessageBox.Show("设备命令长度错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } byte[] cmdbytes = new byte[6]; //01 03 00 01 00 01 xx xx for (int i = 0; i < 6; i++) { cmdbytes[i] = (byte)int.Parse(cmd.Substring(i * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier); } cmdbytes = BytesCheckService.GetCRC16Full(cmdbytes, true); sport.Write(cmdbytes, 0, cmdbytes.Length); }
private void ThreadSendCommand(int deviceNum) { var type = MethodBase.GetCurrentMethod().DeclaringType; if (cmds.Length <= cmd_index) { cmd_index = 0; Thread.Sleep(SLEEP_LENGTH); } LogHelper.WriteLog(type, string.Format("发送指令{0}", cmd_index)); data_type = cmd_index; cmds[cmd_index][0] = (byte)deviceNum; except_buffer_length = ((cmds[cmd_index][4] & 0xFF00) + cmds[cmd_index][5]) * 2 + 5; var cmdbytes = BytesCheckService.GetCRC16Full(cmds[cmd_index], true); if (!Sport.IsOpen) { Sport.Open(); } Sport.Write(cmdbytes, 0, cmdbytes.Length); }