/// <summary> /// 判斷是否為16進制字符串 /// </summary> /// <param name="hexString"></param> /// <returns></returns> public static bool IsHexString(string hexString) { //十六進制發送時,發送框數據進行十六進制數據正則校驗 if (Regex.IsMatch(hexString, "^[0-9A-Fa-f]+$")) { //校驗成功 return true; } //校驗失敗 return false; }
發送數據
/// <summary> /// 發送數據 /// </summary> private void SendData() { if (this.chk_HexSend.Checked) { //去掉空格 01 04 50 string temp = this.rtb_Send.Text.Replace(" ", ""); try { byte[] b = HexHelper.HexStringToBytes(temp); serialPort.Write(b, 0, b.Length); TotalSendNum += b.Length; } catch (Exception ex) { this.tssl_Status.Text = "發送失敗:" + ex.Message; IsOpen = false; } } else { try { byte[] b = encoding.GetBytes(this.rtb_Send.Text); serialPort.Write(b, 0, b.Length); TotalSendNum += b.Length; } catch (Exception ex) { this.tssl_Status.Text = "發送失敗:" + ex.Message; IsOpen = false; } } }