解决串口接收中文乱码问题
定义:
SerialPort ComDevice = new SerialPort();
在开启串口前 设置前后文本转换的字符编码
代码:ComDevice.Encoding = System.Text.Encoding.GetEncoding("GB2312");//此行非常重要 可解决接收中文乱码问题
接收代码:
#region 串口相关操作 SerialPort ComDevice = new SerialPort(); private void GetComList() { //获取可用串口列表 string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { cbbComList.Properties.Items.Add(port); } if (cbbComList.Properties.Items.Count > 0) { cbbComList.SelectedIndex = 0; cbbComList.Enabled = true; } } private void btnComOpen_Click(object sender, EventArgs e) { if (btnComOpen.Tag.ToString() == "0") { ComDevice.PortName = cbbComList.SelectedItem.ToString(); ComDevice.BaudRate = 115200; ComDevice.Parity = (Parity)0; ComDevice.DataBits = 8; ComDevice.StopBits = (StopBits)1; ComDevice.Encoding = System.Text.Encoding.GetEncoding("GB2312");//此行非常重要 可解决接收中文乱码问题 try { ComDevice.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } btnComOpen.Text = "关 闭"; btnComOpen.Tag = "1"; picComStatus.Image = Properties.Resources.green; ComDevice.DataReceived += new SerialDataReceivedEventHandler(ComDevice_DataReceived); } else { try { ComDevice.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } btnComOpen.Text = "打 开"; btnComOpen.Tag = "0"; picComStatus.Image = Properties.Resources.red; } } /// <summary> /// 发送数据 ---此代码在发送时都是转换成十六进制进行发送 /// </summary> private void Send(string cmd, bool HexCmd) { if (cmd == null) return; if (cmd.Length > 0) { if (ComDevice.IsOpen == true) { byte[] SendBytes = null; string SendData = cmd; if (HexCmd == true) { //16进制发送 try { //剔除所有空格 SendData = SendData.Replace(" ", ""); if (SendData.Length % 2 == 1) {//奇数个字符 SendData = SendData.Remove(SendData.Length - 1, 1);//去除末位字符 } List<string> SendDataList = new List<string>(); for (int i = 0; i < SendData.Length; i = i + 2) { SendDataList.Add(SendData.Substring(i, 2)); } SendBytes = new byte[SendDataList.Count]; for (int j = 0; j < SendBytes.Length; j++) { SendBytes[j] = (byte)(Convert.ToInt32(SendDataList[j], 16)); } } catch { XtraMessageBox.Show("请输入正确的16进制数据!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312"); byte[] bytes = chs.GetBytes(cmd); string str = ""; for (int i = 0; i < bytes.Length; i++) { str += string.Format("{0:X2}", bytes[i]); } List<string> SendDataList = new List<string>(); for (int i = 0; i < str.Length; i = i + 2) { SendDataList.Add(str.Substring(i, 2)); } SendDataList.Add("0D"); SendBytes = new byte[SendDataList.Count]; for (int j = 0; j < SendBytes.Length; j++) { SendBytes[j] = (byte)(Convert.ToInt32(SendDataList[j], 16)); } } ComDevice.Write(SendBytes, 0, SendBytes.Length);//发送数据 } else { XtraMessageBox.Show("请打开串口!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } #region 接收数据 private void ComDevice_DataReceived(object sender, SerialDataReceivedEventArgs e) { UpdateRecevie(ComDevice.ReadExisting()); } public delegate void UpdateString(object NewData); public void UpdateRecevie(object NewData) { if (this.InvokeRequired)//等待异步 { UpdateString _myInvoke = new UpdateString(UpdateRecevie); this.Invoke(_myInvoke, new object[] { NewData }); } else { txtComReceive.AppendText(NewData.ToString()); txtComReceive.SelectionStart = txtComReceive.Text.Length - 1; txtComReceive.ScrollToCaret(); } } #endregion