/// <summary> /// Add CRC checkbox changed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addCRCcbx_CheckedChanged(object sender, EventArgs e) { String sendText = sendtbx.Text; if (sendText == null || sendText == "") { addCRCcbx.Checked = false; return; } if (addCRCcbx.Checked) { //Add 2 bytes CRC to the end of the data Byte[] senddata = IController.Hex2Bytes(sendText); Byte[] crcbytes = BitConverter.GetBytes(CRC16.Compute(senddata)); sendText += "-" + BitConverter.ToString(crcbytes, 1, 1); sendText += "-" + BitConverter.ToString(crcbytes, 0, 1); } else { //Delete 2 bytes CRC to the end of the data if (sendText.Length >= 6) { sendText = sendText.Substring(0, sendText.Length - 6); } } sendtbx.Text = sendText; }
/// <summary> /// Send data /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void sendbtn_Click(object sender, EventArgs e) { String sendText = sendtbx.Text; bool flag = false; if (sendText == null) { return; } //set select index to the end sendtbx.SelectionStart = sendtbx.TextLength; if (sendHexRadiobtn.Checked) { //If hex radio checked //send bytes to serial port Byte[] bytes = IController.Hex2Bytes(sendText); sendbtn.Enabled = false;//wait return flag = controller.SendDataToCom(bytes); sendbtn.Enabled = true; sendBytesCount += bytes.Length; } else { //send String to serial port sendbtn.Enabled = false;//wait return flag = controller.SendDataToCom(sendText); sendbtn.Enabled = true; sendBytesCount += sendText.Length; } if (flag) { statuslabel.Text = "Send OK !"; } else { statuslabel.Text = "Send failed !"; } //update status bar toolStripStatusTx.Text = "Sent: " + sendBytesCount.ToString(); }