VB通过串口读数据,出现不稳定情况,帮忙分析下

发布于 2021-11-19 19:12:21 字数 2859 浏览 880 评论 1

Private Sub Timer1_Timer() '读存储区数据
Dim CRC() As Byte
Dim FGetData As String
Dim aa() As Byte
Dim s As String
Dim str As String
Dim i As Integer
Dim bb, cc As String
ReDim aa(5) As Byte '定义动态数组
  aa(0) = add
  aa(1) = order
  aa(2) = h_add
  aa(3) = l_add
  aa(4) = h_num
  aa(5) = l_num
  CRC = CRC16(aa)
  str = CRC
  s = ""
  For i = 1 To LenB(str)
  s = s + Hex(AscB(MidB(str, i, 1)))
  Next i
  bb = Right(s, 2)
  cc = Mid(s, 1, 2)
  If Len(s) < 4 Then
  cc = Mid(s, 1, 1)
  End If
 ReDim Preserve aa(0 To 7) As Byte
  aa(6) = Val("&H" & bb)
  aa(7) = Val("&H" & cc)
 MSComm1.OutBufferCount = 0 '清空输出寄存器
 MSComm1.Output = aa
  FGetData = ReceiveData
  Text5.Text = FGetData
  p11 = Val("&H" & Mid(FGetData, 7, 4))
  p12 = Val("&H" & Mid(FGetData, 11, 4))
  p13 = Val("&H" & Mid(FGetData, 15, 4))
  p14 = Val("&H" & Mid(FGetData, 19, 4))
'For c_num = 0 To 10 Step 1
'm_data(c_num) = p12
'Next
'p12 = sortdata(m_data())

End Sub

Function CRC16(Data() As Byte) As String
  Dim CRC16Lo As Byte, CRC16Hi As Byte 'CRC寄存器
  Dim CL As Byte, CH As Byte '多项式码&HA001
  Dim SaveHi As Byte, SaveLo As Byte
  Dim i As Integer
  Dim Flag As Integer
  CRC16Lo = &HFF
  CRC16Hi = &HFF
  CL = &H1
  CH = &HA0
  For i = 0 To UBound(Data)
  CRC16Lo = CRC16Lo Xor Data(i) '每一个数据与CRC寄存器进行异或
  For Flag = 0 To 7
  SaveHi = CRC16Hi
  SaveLo = CRC16Lo
  CRC16Hi = CRC16Hi  2 '高位右移一位
  CRC16Lo = CRC16Lo  2 '低位右移一位
  If ((SaveHi And &H1) = &H1) Then '如果高位字节最后一位为1
  CRC16Lo = CRC16Lo Or &H80 '则低位字节右移后前面补1
  End If '否则自动补0
  If ((SaveLo And &H1) = &H1) Then '如果LSB为1,则与多项式码进行异或
  CRC16Hi = CRC16Hi Xor CH
  CRC16Lo = CRC16Lo Xor CL
  End If
  Next Flag
  Next i
  Dim ReturnData(1) As Byte
  ReturnData(0) = CRC16Hi 'CRC高位
  ReturnData(1) = CRC16Lo 'CRC低位
  CRC16 = ReturnData
  End Function
  Function ReceiveData() As String '返回存储器区数据
  Dim FGetData As String
  Dim t1 As Long
  Dim av As Variant
  Dim i As Integer
  Dim ReDataLen As Integer
    
  FGetData = ""
  t1 = GetTickCount() '取时间,做延时用
  Do '循环等待接收数据
  DoEvents
  If Form1.MSComm1.InBufferCount > 0 Then '串口有数据了
  ReDataLen = Form1.MSComm1.InBufferCount '取数据长度
  av = Form1.MSComm1.Input '将串口数据取出来
  For i = 0 To ReDataLen - 1
  FGetData = FGetData & Right("00" & Hex(av(i)), 2)
  Next i
  End If
  If Len(FGetData) >= 6 Then
    
  If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 + 8 Then
  ReceiveData = FGetData
  Exit Function
  End If
  End If
  If GetTickCount - t1 > 300 And c_num <= 20 Then '2秒没收完就不收了
  c_num = c_num + 1
  ReceiveData = Form1.Text5.Text
  Else
  ReceiveData = "0"
  c_num = 0
  Exit Function
  End If
  Loop

End Function

这是我写的读取数据源码,帮我看看什么问题,让读取数据不稳定。现在读数据时,有时会出现变成0,然后又会变成正确的数据。我用的是modbus协议

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2021-11-19 20:52:33

 可能读取数据被你清空了,改下应该就好了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文