怎样现实大于227的十进制数?
大家好!有个问题需要大家帮忙。单片机通过MSCOMM给上位机上传96个数,数字在0-4000不等,但使我变得上位机VB 的监控界面收不到,不知为什么?代码如下:
Private Sub MSComm1_OnComm()
Dim InPutLen As Integer
Dim i As Integer
Dim InData As String
Dim arr() As String
Select Case MSComm1.CommEvent
Case comEvReceive
InPutLen = MSComm1.InBufferCount
ReDim arr(InPutLen)
For i = LBound(arr()) To UBound(arr()) - 1
arr(i) = MSComm1.Input
Text1(i).Text = arr(i)
Next i
End Select
End Sub
请高手帮忙,多谢!! --------------------编程问答-------------------- 参考一下这个:http://download.csdn.net/source/1262066 --------------------编程问答-------------------- 没有通信协议,无法提供意见. --------------------编程问答-------------------- 参阅:串口通讯问题 --------------------编程问答-------------------- up --------------------编程问答-------------------- Private Sub MSComm1_OnComm()
Dim InPutLen As Integer
Dim i As Integer
Dim InData As String
Dim arr() As byte
Select Case MSComm1.CommEvent
Case comEvReceive
arr = MSComm1.Input
For i = 0 To UBound(arr())
Text1(i).Text = arr(i)
Next i
End Select
End Sub
注意你接收的格式,如果是字符串,你不必装入数组,如果是二进制方式,你必须装入字节数组。
--------------------编程问答-------------------- 二进制方式,字节变量最大255,大于255的,你要重新根据协议组合成你要的数据。
例如:从下位机收到15 160 ,可以组成4000(十六进制FA0),要根据你的协议来做。
--------------------编程问答-------------------- 开发串口程序必备"串口调试工具"。
网上下载一个试用一下。
然后再自己开发。 --------------------编程问答-------------------- 非常感谢各位的关心,我有把更新的程序上传,这个程序能读到每组的数据,但是数据不对,差异也很大。
我得目的是这样的:从单片机串上来96组数,每个数大小介于0-4000之间,一般为3334,那么我想做个VB界面,建立一个动态数组来显示着96组数据。
我估计这个程序在数据转换上还有很大的问题,我现在还没有彻底高清楚。我用串口精灵现实是对的,十六进制模式的。MSComm1.inputmode=comInputModeText
这样设置可以能实现吗?请个高手帮忙修改一下,不胜感激!
Option Explicit
Private Sub cleanCommand3_Click()
Dim i As Integer
For i = 0 To 95
Text1(i).Text = ""
Next i
End Sub
Private Sub EndCommand1_Click()
End
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
Else
MsgBox ("先打开串行口!")
End If
MSComm1.RThreshold = 92
MSComm1.SThreshold = 0
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeText
MSComm1.InPutLen = 1
End Sub
Private Sub Label4_Click()
End Sub
Private Sub SendCommand2_Click()
Dim outputdata As String
outputdata = Text2.Text
MSComm1.Output = outputdata
End Sub
Private Sub MSComm1_OnComm()
Dim InPutLen As Integer
Dim i As Integer
Dim arr() As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
InPutLen = MSComm1.InBufferCount
ReDim arr(InPutLen)
For i = 0 To UBound(arr()) - 1
arr(i) = Asc(MSComm1.Input)
Text1(i) = arr(i)
Next i
End Select
End Sub
Private Sub Timer1_Timer()
cleanCommand3_Click
Label3.Caption = Time
SendCommand2_Click
End Sub --------------------编程问答-------------------- 我又做了一些数据处理还是不行!哎
我的目的是这样的:单片机上传96组双字节的数,大小介于0-4400之间,于是我定义arr(196) Byte型数组来接收(二进制模式接收),再通过数据转换付给cellvol(96) integer型数组,但是实现不了,不知为什么,请指教,多谢!
我的油箱:power.battery@163.com
源代码如下:
Private Sub Form_Load()
MSComm1.CommPort = 1
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
Else
MsgBox ("先打开串行口!")
End If
MSComm1.RThreshold = 192
MSComm1.SThreshold = 0
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 0
End Sub
Private Sub Label4_Click()
End Sub
Private Sub SendCommand2_Click()
Dim outputdata As String
outputdata = Text2.Text
MSComm1.Output = outputdata
End Sub
Private Sub MSComm1_OnComm()
Dim i As Integer
Dim j As Integer
Dim arr(192) As Byte
Dim cellvol(96) As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
For i = 0 To 191
arr(i) = MSComm1.Input
Next i
For j = 0 To 95
cellvol(j) = arr(2 * j)
cellvol(j) = cellvol(j) * 256
cellvol(j) = arr(2 * i + 1)
Text1(i) = cellvol(j)
Next j
End Select
End Sub --------------------编程问答-------------------- LZ:你是否看过我在3楼提供的链接? --------------------编程问答-------------------- 这段代码我看的不太明白:
ReDim indata(1 To (ll + 1) / 2)
For ii = 0 To UBound(inbuff)
strHex = strHex & Right("0" & Hex(inbuff(ii)), 2)
Text1 = strHex
Next ii
'处理接收的数据
For ii = 1 To Len(strHex) Step 4
indata((ii + 3) / 4) = Val("&H" & Mid(strHex, ii + 2, 2) & Mid(strHex, ii, 2)) '低高字节倒序
Next
我又改进了一些,在监视框里能看到arr()数组里的数据,可是该处cellvol(j) = arr(2 * j) * 256 + arr(2 * j + 1)显示下标越界,不知为什么?
不知到这样将BYTE型移位相加后付值给 INTEGER型 行不行?
请高手指教:
源代码如下:
Private Sub Form_Load()
MSComm1.CommPort = 1
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
Else
MsgBox ("先打开串行口!")
End If
MSComm1.RThreshold = 192
MSComm1.SThreshold = 0
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InPutLen = 192
End Sub
Private Sub SendCommand2_Click()
Dim outputdata As Variant
outputdata = Text2.Text
MSComm1.Output = outputdata
End Sub
Private Sub MSComm1_OnComm()
Dim InPutLen As Integer
Dim i As Integer
Dim j As Integer
Dim arr() As Byte
Dim cellvol() As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
i = MSComm1.InBufferCount
ReDim arr(i)
arr = MSComm1.Input
For j = 0 To (95)
cellvol(j) = arr(2 * j) * 256 + arr(2 * j + 1) 该处现实下标越界
Text1(j).Text = Hex(cellvol(j))
Next j
End Select
End Sub
--------------------编程问答--------------------
Integer数据类型是16位相当于2个8位(Byte),所以必须对indata()数组确定下标的范围.
补充:VB , 基础类