java地磅串口显示乱码_解决串口接收中文乱码问题

定义:

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;

}

}

///

/// 发送数据 ---此代码在发送时都是转换成十六进制进行发送

///

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 SendDataList = new List();

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 SendDataList = new List();

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值