c#遍历匹配串口(向每个串口发数据,根据返回数据确定是否为所需串口)
原文:https://blog.csdn.net/qq_22642239/article/details/81098745
查找设备采用的串口,遍历机器上所有串口,对每个串口发送数据,根据串口返回的数据判断该串口是否是目标设备所使用的串口
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace 切割1
{
public partial class Form1 : Form
{
public SerialPort ComDevice = new SerialPort();
string[] two = SerialPort.GetPortNames();
string data3 ="";
public Form1()
{
InitializeComponent();
ComDevice.DataReceived += new SerialDataReceivedEventHandler(recivedata);//绑定事件
Getname();
}
public void initport(int i) //初始化串口数据
{
if (ComDevice.IsOpen)
{
ComDevice.Close();
ComDevice.PortName = two[i];
ComDevice.BaudRate = 115200;
ComDevice.Parity = Parity.None;
ComDevice.DataBits = 8;
ComDevice.StopBits = StopBits.One;
}
else
{
ComDevice.PortName = two[i];
ComDevice.BaudRate = 115200;
ComDevice.Parity = Parity.None;
ComDevice.DataBits = 8;
ComDevice.StopBits = StopBits.One;
}
ComDevice.Open();
}
private byte[] strToHexByte(string hexString) //字符串转换为byte[]
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0) hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
return returnBytes;
}
public bool senddata() //发送数据
{
string data1 = "55 05 00 00 00 FF 01 01 00 00 00 00 5B 0D";
byte[] data2 = null;
data2 = strToHexByte(data1);
if (ComDevice.IsOpen)
{
try
{
ComDevice.Write(data2, 0, data2.Length);//发送数据
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("串口未打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
public void recivedata(object sender, SerialDataReceivedEventArgs e) //接收读取数据
{
byte[] ReDatas = new byte[ComDevice.BytesToRead];
ComDevice.Read(ReDatas, 0, ReDatas.Length);//读取数据
adddata(ReDatas);
}
public void adddata(byte[] data)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.AppendFormat("{0:x2}" + " ", data[i]);
}
data3 = sb.ToString().ToUpper();
//MessageBox.Show(data3);
}
public void Getname() //根据自己所设条件遍历匹配串口并打开该串口
{
//string data2 = "55 0A 00 00 00 FE 01 01 00 00 00 00 1E 10 10 00 00 9D 0D ";
try
{
for (int i = 0; i < two.Length; i++)
{
initport(i);
senddata();
System.Threading.Thread.Sleep(50);
if (data3.Contains("9D"))
{
//ComDevice.Close();
break;
}
ComDevice.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
//MessageBox.Show(data3);
//ComDevice.Close();
//return ComDevice.PortName;
}
private void button1_Click(object sender, EventArgs e) //串口一个按钮事件
{
string data1 = "55 05 00 00 00 FF 02 09 03 05 00 00 6C 0D";
byte[] data = strToHexByte(data1);
try
{
ComDevice.Write(data, 0, data.Length);//发送数据
}
catch (Exception ex)
{
}
}
}
}