VB 使用串口API 怎么发送16进制
我下载了一个VB的API函数串口程序,因为需要16个以上串口,所以不能使用mscomm控件,使用API程序时,程序发送1,接收端收到31,发送A,接收端发送41,发送a,...
我下载了一个VB的API函数串口程序,因为需要16个以上串口,所以不能使用mscomm控件,使用API程序时,程序发送1,接收端收到31,发送A,接收端发送41,发送a,接收端收到61,应该是Ascii值转换接收,如果我想发送“00 5A 51 00 04 00 00 00 AF”十六进制怎么发送???我知道mscomm控件可以通过十六进制数组&H00 &H5A 等等来发送,API怎么发送???多谢啦
下面是发送函数
'向串口写数据
Function WriteCOM32(COMString As String) As Integer
On Error GoTo handelwritelpt
Dim RetBytes As Long, LenVal As Long
Dim retval As Long
If Len(COMString) > 255 Then
WriteCOM32 Left$(COMString, 255)
WriteCOM32 Right$(COMString, Len(COMString) - 255)
Exit Function
End If
For LenVal = 0 To Len(COMString) - 1
bRead(LenVal) = Mid$(COMString, LenVal + 1, 1)
Next LenVal
' bRead(LenVal) = 0
retval = WriteFile(ComNum, bRead(0), Len(COMString), RetBytes, 0)
' FlushComm
WriteCOM32 = RetBytes
handelwritelpt:
Exit Function
End Function
下面是发送按钮函数:
Private Sub BTNSend_Click()
If WriteCOM32(txt(2)) & vbCr <> Len(txt(2)) Then
MsgBox "写入错误"
Exit Sub
End If
txtRec.Text = ""
Pic.FillColor = &HFF0000
End Sub 展开
下面是发送函数
'向串口写数据
Function WriteCOM32(COMString As String) As Integer
On Error GoTo handelwritelpt
Dim RetBytes As Long, LenVal As Long
Dim retval As Long
If Len(COMString) > 255 Then
WriteCOM32 Left$(COMString, 255)
WriteCOM32 Right$(COMString, Len(COMString) - 255)
Exit Function
End If
For LenVal = 0 To Len(COMString) - 1
bRead(LenVal) = Mid$(COMString, LenVal + 1, 1)
Next LenVal
' bRead(LenVal) = 0
retval = WriteFile(ComNum, bRead(0), Len(COMString), RetBytes, 0)
' FlushComm
WriteCOM32 = RetBytes
handelwritelpt:
Exit Function
End Function
下面是发送按钮函数:
Private Sub BTNSend_Click()
If WriteCOM32(txt(2)) & vbCr <> Len(txt(2)) Then
MsgBox "写入错误"
Exit Sub
End If
txtRec.Text = ""
Pic.FillColor = &HFF0000
End Sub 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询