很久以前做的串口接收程序,那时候用的是MSComm控件,可是串口只能用16以下,后来知道用API可以用16+,所以就找了个API开发的串口调试工具,说真的还不怎么看得明白,有点想不通。
问题上吧~~~~~~~~~~以前的代码
Private Sub MSComm1_OnComm()
    Dim inputbuffer As Variant
    Dim data As String
    Dim dblValue As Double
    Select Case MSComm1.CommEvent
           Case comEvReceive
            inputbuffer = MSComm1.Input()
            data = StrConv(inputbuffer, vbUnicode)
    If Len(data) = 10 Then
    If Right$(data, 1) = Chr(18) Then
    If Mid$(data, 9, 1) = Chr(13) Then
            Textcom1.Text = Left$(data, 1) & CStr(Val(Right$(data, 8)) / 100)
            dblValue = Val(Left$(data, 1) & "1") * Val(Right$(data, 8)) / 100
            Textcom1.Text = Format(dblValue, "0.00")
接下来我在API的程序了找了接收数据界面的代码,我加了个 TEXT4 上去想实现我要数据的效果,可以显示我想要的数据,但是大家仔细看之前负的数据,后面接收正的依然是负,然后我又清除重新接收,正的开始到后面负的还是正的,什么问题?经过我反复的尝试,只要清空数据重新接收,一开始是正的就是正,负就是负,中间转变不了Private Sub Timer3_Timer()
  Dim rlen&, i&
  Dim buf(0 To 511) As Byte
  Dim dblValue As Double
  rlen = sio_read(Port, buf(0), 512)
  If rlen = 0 Then Exit Sub
  intRec = intRec + rlen
  For i = 0 To rlen - 1
  If Option1.Value Then
   text1.Text = text1.Text + Chr(buf(i))   Text4 = Format((Left$(text1, 1) & "1") * CStr(Val(Right$(text1, 8)) / 100), "0.00")  Else
    text1.Text = text1.Text & IIf(buf(i) > 15, Hex(buf(i)), "0" & Hex(buf(i))) & " "
  End If
  Next
  Label1.Caption = "RX:" & intRec
End Sub清空代码里面也没有什么啊
Private Sub Command1_Click()
text1.Text = ""
intRec = 0
Label1.Caption = "RX:" & intRec
End Sub附上图片

解决方案 »

  1.   

    没明白你是用API呢?还是MsComm控件?
      

  2.   

    Private Sub Timer3_Timer()
      Dim rlen&, i&
      Dim buf(0 To 511) As Byte
      Dim dblValue As Double
      rlen = sio_read(Port, buf(0), 512)
      If rlen = 0 Then Exit Sub
      intRec = intRec + rlen
      For i = 0 To rlen - 1
      If Option1.Value Then
      text1.Text = text1.Text + Chr(buf(i))Text4 = Format((Left$(text1, 1) & "1") * CStr(Val(Right$(text1, 8)) / 100), "0.00")  Else
      text1.Text = text1.Text & IIf(buf(i) > 15, Hex(buf(i)), "0" & Hex(buf(i))) & " "
      End If
      Next
      Label1.Caption = "RX:" & intRec
    End Sub
    这段是API的,我现在要用API的啊·········
      

  3.   

    以前的代码,用MSComm控件,已经可以通讯了
    Private Sub MSComm1_OnComm()
      Dim inputbuffer As Variant
      Dim data As String
      Dim dblValue As Double
      Select Case MSComm1.CommEvent
      Case comEvReceive
      inputbuffer = MSComm1.Input()
      data = StrConv(inputbuffer, vbUnicode)
      If Len(data) = 10 Then
      If Right$(data, 1) = Chr(18) Then
      If Mid$(data, 9, 1) = Chr(13) Then
      Textcom1.Text = Left$(data, 1) & CStr(Val(Right$(data, 8)) / 100)
      dblValue = Val(Left$(data, 1) & "1") * Val(Right$(data, 8)) / 100
      Textcom1.Text = Format(dblValue, "0.00")
    。现在想用API的,下面的是用API编写的。我找了应该是在这段加:红色的是我加进去的代码
    Private Sub Timer3_Timer()
      Dim rlen&, i&
      Dim buf(0 To 511) As Byte
      Dim dblValue As Double
      rlen = sio_read(Port, buf(0), 512)
      If rlen = 0 Then Exit Sub
      intRec = intRec + rlen
      For i = 0 To rlen - 1
      If Option1.Value Then
      text1.Text = text1.Text + Chr(buf(i))Text4 = Format((Left$(text1, 1) & "1") * CStr(Val(Right$(text1, 8)) / 100), "0.00")  Else
      text1.Text = text1.Text & IIf(buf(i) > 15, Hex(buf(i)), "0" & Hex(buf(i))) & " "
      End If
      Next
      Label1.Caption = "RX:" & intRec
    End Sub
      

  4.   

    只能说明LZ在Timer3_Timer的代码存在问题,与API无关.