Set the encoding method in the serial port initialization:
mycomm.Encoding = System.Text.Encoding.GetEncoding("UTF-8");//Choose UTF-8 or GB2312 according to the actual situation
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing.Drawing2D;
using SerialCommDemo.Properties;
using System.Threading;
namespace SerialCommDemo
{
public partial class Form1 : Form
{
//public string fileName = "配置.txt";
public Form1()
{
InitializeComponent();
}
public Boolean SendByte(byte b)
{
if (!mycomm.IsOpen)
{
MessageBox.Show("串口没有打开,请打开串口!");
return false;
}
byte[] package = { b };
mycomm.Write(package, 0, package.Length);//向串口发送一包的数据
return false;
}
public Boolean SendArr(byte[] package)
{
if (!mycomm.IsOpen)
{
MessageBox.Show("串口没有打开,请打开串口!");
return false;
}
mycomm.Write(package, 0, package.Length);//向串口发送一包(18字节)的数据
return false;
}
public byte[] hexConvertToByteArray(String str)
{
str = str.Replace(" ", "");
byte[] b = new byte[str.Length / 2];
for (int i = 0; i < str.Length; i = i + 2)
{
b[i / 2] = (byte)Convert.ToInt32(str.Substring(i, 2), 16); //将十六进制“10”转换为十进制i
}
return b;
}
private void button1_Click(object sender, EventArgs e)
{
//this.SendArr(hexConvertToByteArray(textBoxSend.Text));//十六进制
mycomm.Write(textBoxSend.Text); //字符
}
private void comm_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
Thread.Sleep(1000);//这个延时非常重要 等整条数据接收完
if (mycomm.BytesToRead > 0)
{
//因为要访问ui资源,所以需要使用invoke方式同步ui。
this.Invoke((EventHandler)(delegate
{
//byte[] bytes = new byte[mycomm.BytesToRead];
//mycomm.Read(bytes, 0, bytes.Length);
//textBoxRx.Text = "" + System.Text.Encoding.Default.GetString(bytes);
textBoxRx.Text=(mycomm.ReadExisting()); //汉字正常了 字符模式
//十六进制
//byte[] data = new byte[mycomm.BytesToRead]; //定义缓冲区,因为串口事件触发时有可能收到不止一个字节
//mycomm.Read(data, 0, data.Length);
//foreach (byte Member in data) //遍历用法
//{
// string str = Convert.ToString(Member, 16).ToUpper();
// textBoxRx.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");
//}
}));
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (mycomm.IsOpen)
{
mycomm.Close();
btnOpen.Text = "打开串口";
uartStatus();//(false);
}
else
{
try
{
mycomm.PortName = comboBoxPortSelect.Text;
mycomm.ReadTimeout = 32;
mycomm.Encoding = System.Text.Encoding.GetEncoding("UTF-8");//根据实际情况选择UTF-8还是GB2312
mycomm.Open();
btnOpen.Text = "关闭串口";//按钮打开
uartStatus();//(true);
}
catch
{
btnOpen.Text = "打开串口";
MessageBox.Show("没有发现串口或串口已被占用!");
uartStatus();//(false);
}
}
}
private void comboBoxPortSelect_SelectedIndexChanged(object sender, EventArgs e)
{
mycomm.Close();
mycomm.PortName = comboBoxPortSelect.Text;
btnOpen_Click(null,null); //重新打开
}
private void uartStatus()
{
if (mycomm.IsOpen)
{
Settings.Default.comIndex = comboBoxPortSelect.SelectedIndex; //保存上次成功打开的端口
Properties.Settings.Default.Save();//使用Save方法保存更改
pictureBox1.Image = Properties.Resources.LED_OnR;
}
else
pictureBox1.Image = Properties.Resources.LED_OffR;
}
private void Form1_Load(object sender, EventArgs e)
{
textBoxInterval.Text = Settings.Default.interval;
string[] ports = SerialPort.GetPortNames();
Array.Sort(ports);
comboBoxPortSelect.Items.AddRange(ports);
if (Settings.Default.comIndex >= ports.Length)
{
//默认选中第一个
comboBoxPortSelect.SelectedIndex = 0;
}
else
comboBoxPortSelect.SelectedIndex = Settings.Default.comIndex;
// btnOpen_Click(null, null);//打开本计算机的串口
//添加事件注册
mycomm.DataReceived += comm_DataReceived;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (mycomm.IsOpen)
{
//timer1.Interval = Convert.ToInt32(this.textBoxInterval.Text);//设置timer1的timer1_Tick实践执行周期为N毫秒
this.SendArr(hexConvertToByteArray(textBoxSend.Text));
}
else
{
timer1.Enabled = false;
}
}
private void buttonAuto_Click(object sender, EventArgs e)
{
buttonAuto.Enabled = false;
timer1.Interval = Convert.ToInt32(this.textBoxInterval.Text);//设置timer1的timer1_Tick实践执行周期为N毫秒
timer1.Enabled = true;//设置为truetimer1_Tick实践就会执行,开始计时
}
private void buttonDoor1_Click(object sender, EventArgs e)
{
textBoxSend.Text = "{\"cmd\":\"control\",\"smartId\":\"6359\",\"smartChannel\":\"1\",\"sensorId\":\"6361\",\"sensorChannel\":\"1\",\"para\":\"1\"}";
button1_Click(null, null);
}
private void buttonDoor2_Click(object sender, EventArgs e)
{
textBoxSend.Text = "{\"cmd\":\"control\",\"smartId\":\"6359\",\"smartChannel\":\"1\",\"sensorId\":\"6362\",\"sensorChannel\":\"3\",\"para\":\"1\"}";
button1_Click(null,null);
}
private void buttonUPSOpen_Click(object sender, EventArgs e)
{
textBoxSend.Text = "{\"cmd\":\"control\",\"smartId\":\"6358\",\"smartChannel\":\"1\",\"sensorId\":\"6437\",\"sensorChannel\":\"1\",\"para\":\"1\"}";
button1_Click(null, null);
}
private void buttonUPSClose_Click(object sender, EventArgs e)
{
textBoxSend.Text = "{\"cmd\":\"control\",\"smartId\":\"6358\",\"smartChannel\":\"0\",\"sensorId\":\"6437\",\"sensorChannel\":\"0\",\"para\":\"0\"}";
button1_Click(null, null);
}
}
}