闪电侠16集预告,假性尖锐湿疣症状,龙将猜拳模拟器
首次使用c#编写与com口有关的程序,期间遇到了很多问题,写下自己的经验总结,如有错漏,欢迎批评指正!
1、新建一个串口类( serialport类)
1 //create a serial port for bluetooth 2 serialport bluetoothconnection = new serialport();
2、串口端口号搜索:
string[] ports = serialport.getportnames();
or (int i = 0; i < ports.length; i++)
{
string name = ports[i];
combobox.items.add(name);//显示在消息框里面
}
3、读数据、显示数据:
byte[] data = new byte[length]; bluetoothconnection.read(data,0,length); for (int i = 0; i < length; i++) { bluetoothreceiveddata += string.format("data[{0}] = {1}\r\n", i, data[i]);//"+="表示接收数据事件发生时,触发"+="后面的语句 }
4、写数据:
byte[] head = new byte[8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };//随便写的一组数据,里面的数据无意义 bluetoothconnection.write(head, 0, head.length);
5、常用的端口设置和方法:
bluetoothconnection.open();//打开蓝牙串口 bluetoothconnection.readtimeout=10000;//设置或获取数据超时之前的毫秒数 bluetoothconnection.datareceived;//蓝牙串口接收了数据 bluetoothconnection.baudrate;//设置串口的波特率 bluetoothconnection.bytestoread;//蓝牙所收到的缓冲区里数据的数据长度 bluetoothconnection.readbyte();//从串口输入缓冲区里读一个字节
bluetoothconnection.close();//关闭蓝牙串口
串口最基本的功能就是实现通信,简单来说就是读和写,就像大家熟知的那样,把大象装进冰箱只需要三步:打开蓝牙串口,操作和处理数据,关闭蓝牙串口。
建议在手机上下载一个蓝牙助手,可以清楚具体地看到数据收发的结果。
另外,如果要设置蓝牙模块的波特率、密码等,需要在电脑上下载串口软件。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论