大家帮我看看VB串行通信程序出什么问题了
我想把存在上位机已知数组a(i,j)中的数据发送给单片机。但是单片机的LED灯显示出数据没有正确发送过来(比如说我发生十进制的20,单片机没有正确接收到),我不知道错在哪...
我想把存在上位机已知数组a(i,j)中的数据发送给单片机。但是单片机的LED灯显示出数据没有正确发送过来(比如说我发生十进制的20,单片机没有正确接收到),我不知道错在哪里了,请大家帮我改正,或者给出正确的发送数据程序。如能成功,再追加50分。
Private Sub Command1_Click()
Dim a(i,j) As Long
Dim send As Variant
send = a(i,j)
MSComm1.Output = send
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 512
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim a As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
If MSComm1.InBufferCount Then
a= MSComm1.Input
Text2.Text = a
End If
End Select
End Sub 展开
Private Sub Command1_Click()
Dim a(i,j) As Long
Dim send As Variant
send = a(i,j)
MSComm1.Output = send
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 512
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim a As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
If MSComm1.InBufferCount Then
a= MSComm1.Input
Text2.Text = a
End If
End Select
End Sub 展开
3个回答
展开全部
1、首先dim a(i,j) as long就错了,这里需要常数,如果你需要变量可以使用Redim语句
dim a() as long
i=2
j=2
Redim a(i,j) as long
2、MSComm1.Output 支持两种数据类型,一是字符串,二是Byte数组,所以MSComm1.Output = send也是错的。
dim send(0) as byte
send(0) = 20
MSComm1.Output = send
3、由于不知道你真正的意思,我试着写一段简单的程序,如有疑问可以百度HI我。
Private a(9, 9) As Byte
Private Sub Command1_Click() '#发送a()中某一个数据
Dim send(0) As Byte
i = Int(9 * Rnd)
j = Int(9 * Rnd)
send(0) = a(i, j)
MSComm1.Output = send
End Sub
Private Sub Form_Load()
'#初始化模拟数据
For i = 0 To 9
For j = 0 To 9
a(i, j) = i * j
Next
Next
Text2 = ""
'#Mscomm设置
MSComm1.InputMode = comInputModeBinary '#设置接收模式为二进制形式
MSComm1.CommPort = 3
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 512
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim RcvByte() As Byte
Dim strHex As String
strHex = ""
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
If MSComm1.InBufferCount Then
RcvByte = MSComm1.Input '#获取接收的二进制数据
For i = LBound(RcvByte) To UBound(RcvByte)
strHex = strHex & Right("0" & CStr(Hex(RcvByte(i))), 2) & " " '#将二进制数据转换为文本显示到Text2
Next
Text2.Text = strHex
End If
End Select
End Sub
dim a() as long
i=2
j=2
Redim a(i,j) as long
2、MSComm1.Output 支持两种数据类型,一是字符串,二是Byte数组,所以MSComm1.Output = send也是错的。
dim send(0) as byte
send(0) = 20
MSComm1.Output = send
3、由于不知道你真正的意思,我试着写一段简单的程序,如有疑问可以百度HI我。
Private a(9, 9) As Byte
Private Sub Command1_Click() '#发送a()中某一个数据
Dim send(0) As Byte
i = Int(9 * Rnd)
j = Int(9 * Rnd)
send(0) = a(i, j)
MSComm1.Output = send
End Sub
Private Sub Form_Load()
'#初始化模拟数据
For i = 0 To 9
For j = 0 To 9
a(i, j) = i * j
Next
Next
Text2 = ""
'#Mscomm设置
MSComm1.InputMode = comInputModeBinary '#设置接收模式为二进制形式
MSComm1.CommPort = 3
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 512
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim RcvByte() As Byte
Dim strHex As String
strHex = ""
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
If MSComm1.InBufferCount Then
RcvByte = MSComm1.Input '#获取接收的二进制数据
For i = LBound(RcvByte) To UBound(RcvByte)
strHex = strHex & Right("0" & CStr(Hex(RcvByte(i))), 2) & " " '#将二进制数据转换为文本显示到Text2
Next
Text2.Text = strHex
End If
End Select
End Sub
光派通信
2024-09-03 广告
2024-09-03 广告
对光传输设备进行调试和维护,需要掌握以下关键点:1. **熟悉设备性能**:深入了解OTN/DWDM等光传输产品的性能特点,确保能够熟练使用设备。2. **按规范操作**:依据产品说明书和维护手册进行调试和维护,确保操作步骤正确无误。3. ...
点击进入详情页
本回答由光派通信提供
展开全部
发送的是低字节在前,高字节在后。
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim a As Long
Dim Buffer(3) As Byte
a = Val(Text1)
CopyMemory Buffer(0), a, 4
MSComm1.Output = Buffer
End Sub
Private Sub Form_Load()
Text1 = ""
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 512
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim a As Long
Dim Buffer(3) As Byte
a = Val(Text1)
CopyMemory Buffer(0), a, 4
MSComm1.Output = Buffer
End Sub
Private Sub Form_Load()
Text1 = ""
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 512
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
MSComm1.Output = send
这个用法是有问题的.
首先你要设置是按2进制发送数据还是字符串发送数据.
你要发送20 ,初始化设定MSComm1.InputMode = comInputModeBinary
dim bt(0) as byte '定义字节数组
bt(0)=a(ij)
MSComm1.Output = bt
如果要连续发送,可以这样
dim bt(3) as byte '发几个定义几个,然后赋值
bt(0)=..
bt(1)=..
bt(2)=..
bt(3)=..
MSComm1.Output = bt '一起发送
你试一下
这个用法是有问题的.
首先你要设置是按2进制发送数据还是字符串发送数据.
你要发送20 ,初始化设定MSComm1.InputMode = comInputModeBinary
dim bt(0) as byte '定义字节数组
bt(0)=a(ij)
MSComm1.Output = bt
如果要连续发送,可以这样
dim bt(3) as byte '发几个定义几个,然后赋值
bt(0)=..
bt(1)=..
bt(2)=..
bt(3)=..
MSComm1.Output = bt '一起发送
你试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询