c#遍历匹配串口(向每个串口发数据,根据返回数据确定是否为所需串口)
最近在公司有一个项目需要遍历串口,查找设备采用的串口,我的想法是遍历机器上所有串口,对每个串口发送数据,根据串口返回的数据判断该串口是否是目标设备所使用的串口(已有设备串口指令手册),本次采用c#实现。
代码如下:
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 = https://www.it610.com/article/strToHexByte(data1);
try
{
ComDevice.Write(data, 0, data.Length);
//发送数据}
catch (Exception ex)
{}
}}
}
【c#遍历匹配串口(向每个串口发数据,根据返回数据确定是否为所需串口)】经测试可以达到预期效果,如果有好的方法,请留言,本人小菜一枚
推荐阅读
- opencv|opencv C++模板匹配的简单实现
- JavaScript|JavaScript — 初识数组、数组字面量和方法、forEach、数组的遍历
- 笔记|C语言数据结构——二叉树的顺序存储和二叉树的遍历
- for循环遍历数组
- 正则匹配
- 集合框架(集合嵌套存储和遍历元素的案例代码实现)
- es使用与原理4|es使用与原理4 -- phrase match ,slop近似匹配,搜索推荐等等
- LeetCode-102.|LeetCode-102. 二叉树的层序遍历
- 先序遍历 中序遍历 后序遍历 层序遍历
- es7.x(6)—minimum_should_match最低匹配度