VB采用SerialPort控件串口通信中,接受数据怎么实现?
这是写了的部分PrivateSubSerialPort1_DataReceived(ByValsenderAsObject,ByValeAsSystem.IO.Ports...
这是写了的部分
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim n As Integer
Dim rxdata(2) As Byte
SerialPort1.PortName = "COM9"
n = Me.SerialPort1.BytesToRead '接收到的字节数
If (n > 0) Then ReDim rxdata(n - 1)
Me.SerialPort1.Read(rxdata, 0, n)
SerialPort1.Close()
End Sub 展开
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim n As Integer
Dim rxdata(2) As Byte
SerialPort1.PortName = "COM9"
n = Me.SerialPort1.BytesToRead '接收到的字节数
If (n > 0) Then ReDim rxdata(n - 1)
Me.SerialPort1.Read(rxdata, 0, n)
SerialPort1.Close()
End Sub 展开
展开全部
在Form_Load()内写入以下代码,进行串口初始化
MSComm1.CommPort =9 '读取com口号
If MSComm1.PortOpen = True Then '如果串口打开先关闭后再进行其他操作
MSComm1.PortOpen = False
End If
MSComm1.PortOpen = True '打开串口9
在退出按钮中写入
MSComm1.PortOpen = False '以在退出程序时关闭串口
双击串口控件图标,在其处理程序中写入以下代码
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent '事件发生
Case 2
MSComm1.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm1.Input '读入到缓冲区,strBuff 中存放的就是串口接收的数据
End Select
End Sub
满意请及时采纳,谢谢
MSComm1.CommPort =9 '读取com口号
If MSComm1.PortOpen = True Then '如果串口打开先关闭后再进行其他操作
MSComm1.PortOpen = False
End If
MSComm1.PortOpen = True '打开串口9
在退出按钮中写入
MSComm1.PortOpen = False '以在退出程序时关闭串口
双击串口控件图标,在其处理程序中写入以下代码
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent '事件发生
Case 2
MSComm1.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm1.Input '读入到缓冲区,strBuff 中存放的就是串口接收的数据
End Select
End Sub
满意请及时采纳,谢谢
追问
非常谢谢您这么详细的答案,但是我的VS 2012上是没有MScomm控件的,只能采用SerialPort进行控制啊!
追答
找找看,我机器里没安装VS2012
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询