首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

COM数据接收有关问题

2012-02-03 
COM数据接收问题最近做个电子秤的代码,要接收到重量然后计算价钱。我用vb6.0 添加了段代码,但是mscomm控件

COM数据接收问题
最近做个电子秤的代码,要接收到重量然后计算价钱。我用vb6.0 添加了段代码,
但是mscomm控件在使用的过程中出了点问题。如果直接运行程序 很大几率接收不到返
回的重量,但是如果把断点设在oncomm事件上就肯定可以接收到。请问如何稳定的接收
到数据 ,代码如下:

初始化并发送命令:

VB code
bytOutput(0) = &H57        With WeightComm            .CommPort = Val(sPriceComputingComPort)            .RThreshold = 1            .Settings = "9600,N,8,1"            .InputMode = comInputModeText            .PortOpen = True            .output = bytOutput        End With


触发事件:
VB code
sWeight = ""    Select Case WeightComm.CommEvent        Case comEvReceive            WeightComm.InputLen = 0            buffer = WeightComm.input            BytesReceived() = buffer            For i = 0 To UBound(BytesReceived)                If i = 2 Or i = 4 Or i = 6 Or i = 8 Or i = 10 Or i = 12 Then                    sWeight = sWeight & Chr(BytesReceived(i))                End If            Next i        Case comEvSend    End Select        WeightComm.OutBufferCount = 0    WeightComm.InBufferCount = 0    WeightComm.PortOpen = False        sItemWeight = sWeight


[解决办法]
是否应该有等待
[解决办法]
你这个应该是连续发送模式的,你先用串口调试精灵看下能不能正常收到数据,如果能说明问题还是出在代码上。
给你接受部分的串口调试精灵源码供你参考
VB code
'===================================================================================='                通信触发事件'====================================================================================Private Sub MSComm_OnComm()                                                             On Error GoTo Err    Select Case MSComm.CommEvent                                                           Case comEvReceive            If ChkHexReceive.Value = 1 Then                Call hexReceive                                                            Else                Call textReceive                                                            End If                    Case comEvSend                                                                        If ChkHexSend.Value = 1 Then            Else                Call textSend                                                                  End If                    Case Else    End SelectErr:  End Sub'===================================================================================='               文本接受'====================================================================================Private Sub textReceive()On Error GoTo Err    InputSignal = MSComm.Input    ReceiveCount = ReceiveCount + LenB(StrConv(InputSignal, vbFromUnicode))            If DisplaySwitch = False Then                                                         TxtReceive.Text = TxtReceive.Text & InputSignal                                   TxtReceive.SelStart = Len(TxtReceive.Text)                                    End If    TxtRXCount.Text = "RX:" & ReceiveCount                                                 If ChkAutoClear.Value = 1 Then                                                        If ReceiveCount >= 65535 Then            TxtReceive.Text = ""        End If    End IfErr:End Sub '===================================================================================='       十六进制接受'====================================================================================Private Sub hexReceive() On Error GoTo Err    Dim ReceiveArr() As Byte                                                            Dim receiveData As String                                                           Dim Counter As Integer                                                             Dim i As Integer                                                                        If (MSComm.InBufferCount > 0) Then        Counter = MSComm.InBufferCount                                                      receiveData = ""                                                                           ReceiveArr = MSComm.Input                                                          For i = 0 To (Counter - 1) Step 1                                                      If (ReceiveArr(i) < 16) Then                receiveData = receiveData & "0" + Hex(ReceiveArr(i)) & Space(1)                 Else                receiveData = receiveData & Hex(ReceiveArr(i)) & Space(1)                      End If        Next i                TxtReceive.Text = TxtReceive.Text + receiveData                                    TxtReceive.SelStart = Len(TxtReceive.Text)                                      End If        ReceiveCount = ReceiveCount + Counter                                               TxtRXCount.Text = "RX:" & ReceiveCount                                             If ChkAutoClear.Value = 1 Then                                                         If ReceiveCount >= 65535 Then            TxtReceive.Text = ""        End If    End IfErr:         End Sub 

热点排行