串口解析显示中文
private String SerialPortReadStr()
{
try
{
String str = null;
int n = serialPort1.BytesToRead;//数据接收缓存区的字接数
byte[] buffer = new byte[n];//创建临时字节数组
serialPort1.Read(buffer, 0, n);//从缓存区读取数据
str = Encoding.GetEncoding("GB2312").GetString(buffer);
if(!IsStopReadComData)
{
UpDateLogTextBox(str);
}
if (保存串口日志ToolStripMenuItem.Checked)
{
log.FaceLog.AppendSerialLog("[" + DateTime.Now.ToString() + "]"+str);
}
return str;
}
catch (Exception ex)
{
OutputMessage("Exception:" + ex.Message);
return "error";
}
}
搜索串口
private void User_SearchSerialPort()
{
try
{
string[] ArryPort = SerialPort.GetPortNames();
cb_ComName.Items.Clear();
for (int i = 0; i < ArryPort.Length; i++)
{
cb_ComName.Items.Add(ArryPort[i]);
}
cb_ComName.SelectedIndex = 0;
OutputMessage("搜索到串口:"+string.Join(",", ArryPort));
}
catch (Exception)
{
MessageBox.Show("串口搜索失败
");
}
}