-
vb.net及C#串口基本通信
2.配置串口参数
Public Sub Serial_Port_EFS1() '设置串口参数
With SerialPort_EFS1
.BaudRate = intEFS1_Baudrate
.PortName = strEFS1_COM '串口名称
.DataBits = 8 '数据位
.StopBits = IO.Ports.StopBits.One '停止位
.Parity = IO.Ports.Parity.Odd '偶校验
'.NewLine = vbCr & vbLf
.RtsEnable = True
End With
End Sub
3.串口接收
'触发接收事件
Public Sub Sp_DataReceived_EFS(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort_EFS1.DataReceived
Me.Invoke(New EventHandler(AddressOf Sp_Receiving_EFS)) '调用接收数据函数
End Sub
'接收时间响应函数
'接收数据
(1)
Private Sub Sp_Receiving_EFS(ByVal sender As Object, ByVal e As EventArgs)
Dim StrInEFS As String = "", strCommand As String = "", strSendString As String = ""
Dim inDataLen, i As Integer
Dim strLen As UShort
Dim strHexSend As MatchCollection
Dim strList As New List(Of Byte)()
inDataLen = SerialPort_EFS1.BytesToRead() '读取缓存区字节数
Try
If inDataLen > 0 Then
Dim inBytes_EFS(inDataLen - 1) As Byte
SerialPort_EFS1.Read(inBytes_EFS, 0, inDataLen)
SerialPort_EFS1.DiscardInBuffer()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
(2)
Dim inDataLen As Integer = SerialPort_MA.BytesToRead() '读取缓存区字节数
If inDataLen > 0 Then
Dim inBytes_MA(inDataLen - 1) As Byte, bytes As Byte
SerialPort_MA.Read(inBytes_MA, 0, inDataLen)
For Each bytes In inBytes_MA
StrInMA = StrInMA + [String].Format("{0:X2} ", bytes) '显示Ascii
Next
end if
4.发送
(1)
strHexSend = Regex.Matches(strSendString, "(?i)[\da-f]{2}")
strList = New List(Of Byte)()
For Each m As Match In strHexSend
strList.Add(Byte.Parse(m.Value,
System.Globalization.NumberStyles.HexNumber))
Next
SerialPort_EFS1.Write(strList.ToArray(), 0, strList.Count)
(2)
Dim aByte_MA(7) As Byte
For i = 0 To 7
aByte_MA(i) = "&H" & (Mid("01080000AA555E94", 2 * i + 1, 2))
Next
5.获取端口加载到combobox
Dim ports As String() = SerialPort.GetPortNames()
Array.Sort(ports)
ComboBox1.Items.Clear()
Public Sub Serial_Port_EFS1() '设置串口参数
With SerialPort_EFS1
.BaudRate = intEFS1_Baudrate
.PortName = strEFS1_COM '串口名称
.DataBits = 8 '数据位
.StopBits = IO.Ports.StopBits.One '停止位
.Parity = IO.Ports.Parity.Odd '偶校验
'.NewLine = vbCr & vbLf
.RtsEnable = True
End With
End Sub
3.串口接收
'触发接收事件
Public Sub Sp_DataReceived_EFS(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort_EFS1.DataReceived
Me.Invoke(New EventHandler(AddressOf Sp_Receiving_EFS)) '调用接收数据函数
End Sub
'接收时间响应函数
'接收数据
(1)
Private Sub Sp_Receiving_EFS(ByVal sender As Object, ByVal e As EventArgs)
Dim StrInEFS As String = "", strCommand As String = "", strSendString As String = ""
Dim inDataLen, i As Integer
Dim strLen As UShort
Dim strHexSend As MatchCollection
Dim strList As New List(Of Byte)()
inDataLen = SerialPort_EFS1.BytesToRead() '读取缓存区字节数
Try
If inDataLen > 0 Then
Dim inBytes_EFS(inDataLen - 1) As Byte
SerialPort_EFS1.Read(inBytes_EFS, 0, inDataLen)
SerialPort_EFS1.DiscardInBuffer()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
(2)
Dim inDataLen As Integer = SerialPort_MA.BytesToRead() '读取缓存区字节数
If inDataLen > 0 Then
Dim inBytes_MA(inDataLen - 1) As Byte, bytes As Byte
SerialPort_MA.Read(inBytes_MA, 0, inDataLen)
For Each bytes In inBytes_MA
StrInMA = StrInMA + [String].Format("{0:X2} ", bytes) '显示Ascii
Next
end if
4.发送
(1)
strHexSend = Regex.Matches(strSendString, "(?i)[\da-f]{2}")
strList = New List(Of Byte)()
For Each m As Match In strHexSend
strList.Add(Byte.Parse(m.Value,
System.Globalization.NumberStyles.HexNumber))
Next
SerialPort_EFS1.Write(strList.ToArray(), 0, strList.Count)
(2)
Dim aByte_MA(7) As Byte
For i = 0 To 7
aByte_MA(i) = "&H" & (Mid("01080000AA555E94", 2 * i + 1, 2))
Next
5.获取端口加载到combobox
Dim ports As String() = SerialPort.GetPortNames()
Array.Sort(ports)
ComboBox1.Items.Clear()
最新更新
1319联通网络的操作次数
python字符串前加r、f、u、l 的区别
Python爬虫入门教程01:豆瓣Top电影爬取
Python爬虫入门教程02:笔趣阁小说爬取
python的变量缓存机制
线性回归
爬取腾讯视频《哈哈哈哈哈》50000条弹幕
Python基础 - 变量及常量
pandas重塑层次化索引(stack()和unstack()函数
Python爬取网易云音乐辑的图片、专辑名和
用 Access+Outlook 来采集信息
使用PowerDesigner生成Access数据库
让我们一起用开源数据库和开源框架废弃
随说秋色园从Access升迁到MSSQL过程
当爬虫被拒绝时(Access Denied)
Web API与OAuth:既生access token,何生refres
[认证 & 授权] 6. Permission Based Access Co
Access之C#连接Access
oracle 19c下载和安装教程(database和client)
MySql索引原理与使用大全
php的计数器每次都会清零
PHP基础
数据类型之对象
数据类型之布尔型、整型、浮点型和字符
php教程之数据类型之数组
php教程之PHP 常量
php教程之变量
php教程之语法
PHP简介与安装
phpMyAdmin配置安装全攻略