VB中发送16进制数据包?

VB中发送16进制数据包?,第1张

发送16进制数据包跟发送String类型数据包样

或者也转换成2进制发送

主要接收端对数据包处理

winsock1.sendata

"abcde"

Doevents

'里加句

winsock1.sendata

"fghij"

代码如下:

Private Sub Form_Load()

MSComm1.Settings = "9600,N,8,1" '设置通信口参数

MSComm1.InBufferSize = 40 '设置MSComm1接收缓冲区为40字节

MSComm1.OutBufferSize = 2 '设置MSComm1发送缓冲区为2字节

MSComm1.InputMode = comInputModeBinary '设置接收数据模式为二进制形式

MSComm1.InBufferCount = 0 '清除接收缓冲区

MSComm1.OutBufferCount = 0 '清除发送缓冲区

'MSComm1.RThreshold = 1 '设置接收一个字节产生OnComm事件

MSComm1.CommPort = 5

MSComm1.PortOpen = True '打开通信口

End Sub

Private Sub Command2_Click()

Dim data() As Byte '串口发送字节

ReDim data(7)

data(0) = &H81

data(1) = &H81

data(2) = &H52

data(3) = &H00

data(4) = &H00

data(5) = &H00

data(6) = &H53

data(7) = &H00

MSComm1.Output = data

接收有几种办法:1.延时接收

Timer1.Enabled = True

t1_c0:

If t1_flag0 = True Then GoTo t1_c1

DoEvents

GoTo t1_c0

t1_c1:

t1_flag0 = False

Dim Bintput() As Byte

Dim Binputa As Variant

Binputa = MSComm1.Input ' 从接收队列中读入字符串

Bintput() = Binputa

Dim i As Integer

For i = 0 To UBound(Bintput)

If Len(Hex(Bintput(i))) = 1 Then

strData = strData &"0" &Hex(Bintput(i))

Else

strData = strData &Hex(Bintput(i))

End If

Next

Text3.Text = strData


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9477249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存