日期:2014-05-18  浏览次数:21204 次

vb读取串口数据绘制实时曲线关键源码翻译求教,希望了解源程序通讯协议以及主要变量的计算表达式,改为用C#进行绘制
Public gDiameter(55) As Single '粒径数组
Public gDataInDiamete(55) As Long '每次从串口读取的粒径通道的数据(52个粒径通道)
Public gChannelData(65) As Long '从串口读取的强度通道数据(64个强度通道)

'CRC-16校验函数,参数待校验的数组,返回值 余数
Function CRC16(data() As Byte) As String
  Dim CRC16Lo As Byte, CRC16Hi As Byte 'CRC寄存器
  Dim CL As Byte, CH As Byte '多项式码 & H8001
  Dim SaveHi As Byte, SaveLo As Byte
  Dim i As Integer
  Dim Flag As Integer
  CRC16Lo = &HFF
  CRC16Hi = &HFF
  CL = &H1
  CH = &H80
  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

'处理串口数据
Private Sub MSComm1_OnComm()
  'Dim rs_sChannelData(55) As Long
  'Dim rs_sIntensityData(52, 64) As Long
  'Dim rs_pIntensity(65) As Long
  Dim vbReceive As Variant
  Dim i, j As Integer
  Dim L As Integer
  Dim dSum As Long
  Dim pm25t As Single
  Dim pm10t As Single
  pm25t = 0
  pm10t = 0
   
  If MSComm1.CommEvent = comEvReceive Then
  'wait (50) 'Receive the rs232 chars
   
  If MSComm1.InBufferCount >= 6764 Then
  vbReceive = ((MSComm1.Input))
  If ((vbReceive(0) = 255) And (vbReceive(1) = 1)) Then
   
  ' //接下来是52个粒径通道的生物粒子浓度
  dSum = 0
   
  gDataInDiamete(1) = -1
  gDataInDiamete(2) = -1
  gDataInDiamete(3) = -1
  gDataInDiamete(4) = (vbReceive(2 + 2 * 52 * 64) * 256 + vbReceive(2 + 2 * 52 * 64 + 1)) ' + Int(800 * Rnd + 1)
   
  dSum = dSum + gDataInDiamete(4)
   
  For i = 1 To 51
  gDataInDiamete(i + 4) = vbReceive(2 + 2 * 52 * 64 + 2 * i) * 256 + vbReceive(2 + 2 * 52 * 64 + 2 * i + 1) ' + Int(800 * Rnd + 1)
  dSum = dSum + gDataInDiamete(i + 4)
   
  Next i
   
  For i = 0 To 22
   
  pm25t = pm25t + gDataInDiamete(i + 4) * gDiameter(i + 4) * gDiameter(i + 4) * gDiameter(i + 4)
   
  Next i
   
  For i = 0 To 41