VB.NET使用AxMSComm实现串口通信的发送读取问题?
请高手帮我看看,为什么读取串口缓冲区内容为空'打开串口1TryDimaAsShortAxMSComm1.CommPort=1NUMcom=1AxMSComm1.PortO...
请高手帮我看看,为什么读取串口缓冲区内容为空
'打开串口1
Try
Dim a As Short
AxMSComm1.CommPort = 1
NUMcom = 1
AxMSComm1.PortOpen = True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
‘发送数据
Public Sub okset_sub(ByRef a As Short, ByRef a0 As Short, ByRef a1 As Short) '设定运行细分与步进角
outdata(0) = &HFF
outdata(1) = &HAA
outdata(2) = a
outdata(3) = &H1
outdata(4) = 1
outdata(5) = 0
outdata(6) = a1
outdata(7) = &H0
AxMSComm1.Output = outdata’
End Sub
‘读取串口缓冲区值
Private Sub AxMSComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxMSComm1.OnComm
Dim BytReceived() As Byte '定义字节数组
Dim strBuff As String '定义字符串变量
Select Case AxMSComm1.CommEvent
Case MSCommLib.OnCommConstants.comEvReceive
strBuff = AxMSComm1.Input() '从输入缓冲区读入字符
BytReceived = System.Text.UnicodeEncoding.Unicode.GetBytes(strBuff)
If UBound(BytReceived) > 0 Then
TextBox1.Text = BytReceived(0)
TextBox2.Text = BytReceived(1)
TextBox3.Text = BytReceived(2)
TextBox4.Text = BytReceived(3)
TextBox5.Text = BytReceived(4)
TextBox6.Text = BytReceived(5)
End If
END SUB 展开
'打开串口1
Try
Dim a As Short
AxMSComm1.CommPort = 1
NUMcom = 1
AxMSComm1.PortOpen = True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
‘发送数据
Public Sub okset_sub(ByRef a As Short, ByRef a0 As Short, ByRef a1 As Short) '设定运行细分与步进角
outdata(0) = &HFF
outdata(1) = &HAA
outdata(2) = a
outdata(3) = &H1
outdata(4) = 1
outdata(5) = 0
outdata(6) = a1
outdata(7) = &H0
AxMSComm1.Output = outdata’
End Sub
‘读取串口缓冲区值
Private Sub AxMSComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxMSComm1.OnComm
Dim BytReceived() As Byte '定义字节数组
Dim strBuff As String '定义字符串变量
Select Case AxMSComm1.CommEvent
Case MSCommLib.OnCommConstants.comEvReceive
strBuff = AxMSComm1.Input() '从输入缓冲区读入字符
BytReceived = System.Text.UnicodeEncoding.Unicode.GetBytes(strBuff)
If UBound(BytReceived) > 0 Then
TextBox1.Text = BytReceived(0)
TextBox2.Text = BytReceived(1)
TextBox3.Text = BytReceived(2)
TextBox4.Text = BytReceived(3)
TextBox5.Text = BytReceived(4)
TextBox6.Text = BytReceived(5)
End If
END SUB 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询