儀錶 Q&A update:2005/03/28

編 號:03  
Q: Visual Basic 如何讀取 MA-SM2 顯示值?
A:

Private Sub 通訊埠開啟_Click()
 If MSComm1.PortOpen = False Then
  MSComm1.InputMode = comInputModeBinary
  '
  MSComm1.CommPort = 7
  MSComm1.Settings = "19200,n,8,2"    '儀錶出廠值
  MSComm1.PortOpen = True
 End If
End Sub

Private Sub 通訊埠關閉_Click()
 MSComm1.PortOpen = False
End Sub

Private Sub 傳送資料_Click()
 Dim SendData(7) As Byte
 '
 SendData(0) = &H1   '站號
 SendData(1) = &H3   'read Function
 SendData(2) = &H0
 SendData(3) = &H48   '目前顯示值
 SendData(4) = &H0
 SendData(5) = &H1
 SendData(6) = &H4   'crc low
 SendData(7) = &H1C   'crc high
 '
 MSComm1.Output = SendData    '傳送資料
End Sub

Private Sub 接收資料_Click()
 Dim ReceiveData() As Byte
 '
 If MSComm1.InBufferCount > 0 Then
  ReceiveData = MSComm1.Input
  '
  顯示值.Text = CStr((ReceiveData(3) * 256) + ReceiveData(4))
 End If
End Sub

範例下載(2KB)