请多多指教。谢谢!!!!!!!!!
我其它论坛找到以下信息,不知是否有用?
本人在做一个自动测试站,是通过计算器的COM1口读取万用表,示波器等的电压数据(我采用的是万用表是Fluke45,泰克的示波器),下面什我编写地代码。大家帮忙看看有没有什么错误的地方。通过COM口读取数据到底还有哪些地方要注意,怎样设置采可以呢。请大家指点一下。
Private Sub Command1_Click()
MSComm1.PortOpen = False
End
End Sub
Private Sub Command2_Click()
Dim buffer$
retry:
buffer$ = ""
Do
DoEvents
buffer$ = buffer$ & MSComm1.Input
If (Len(buffer$) > 0) Then
Label1 = "Data being updated...."
Else
Label1 = "Please wait for a moment...."
End If
Loop Until Len(buffer$) > 10
Text1 = buffer$
GoTo retry
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 0
If (MSComm1.PortOpen = False) Then MSComm1.PortOpen = True
Text1 = ""
End Sub
答 1:
最好用 binary 方式Private Sub Command2_Click()
Dim buffer$
COMMAND2.ENABLE = 0
DO WHILE MSCOMM1.OPEN
buffer$ = ""
Do
DoEvents
buffer$ = buffer$ & MSComm1.Input
If (Len(buffer$) > 0) Then
Label1 = "Data being updated...."
Else
Label1 = "Please wait for a moment...."
End If
Loop Until Len(buffer$) > 10
Text1 = buffer$
LOOP
COMMAND2.ENABLE = -1
End Sub
答 2:
现在的关键问题是无法通过com口读取数据
有什么高见吗?第一次用VB,现在什一边学一边做。
对VB有很多概念还是很陌生,请多多指教。
答 3:
COMMSCOMM 有一个中断函数,在COM接收道数据时会产生中断。
Private Sub MSComm1_OnComm()
Dim Buffer As Variant
Dim EVMsg$
Dim ERMsg$
Select Case MSComm1.CommEvent
Case comEvReceive
Buffer = MSComm1.Input
aA = StrConv(Buffer, vbUnicode)
MyGet = MyGet & aA
'"MyGet" 就是接收到的数据
Case comEvSend
EVMsg$ = "正在发送数据"
Case comEvCTS
EVMsg$ = "Change in CTS Detected"
Case comEvDSR
EVMsg$ = "Change in DSR Detected"
Case comEvCD
EVMsg$ = "Change in CD Detected"
Case comEvRing
EVMsg$ = "The Phone is Ringing"
Case comEvEOF
EVMsg$ = "End of File Detected"
' Error messages.
Case comBreak
ERMsg$ = "Break Received"
Case comCDTO
ERMsg$ = "Carrier Detect Timeout"
Case comCTSTO
ERMsg$ = "CTS Timeout"
Case comDCB
ERMsg$ = "Error retrieving DCB"
Case comDSRTO
ERMsg$ = "DSR Timeout"
Case comFrame
ERMsg$ = "Framing Error"
Case comOverrun
ERMsg$ = "Overrun Error"
Case comRxOver
ERMsg$ = "Receive Buffer Overflow"
Case comRxParity
MSComm1.Output = MySend
' ERMsg$ = "Parity Error"
Case comTxFull
ERMsg$ = "Transmit Buffer Full"
Case Else
ERMsg$ = "Unknown error or event"
End Select
If Len(EVMsg$) Then
' sbrStatus.Panels("Status").Text = "状态:" & EVMsg$
ElseIf Len(ERMsg$) Then
' sbrStatus.Panels("Status").Text = "状态:" & ERMsg$
If RET = 2 Then
MSComm1.PortOpen = False ' Close the port and quit.
End If
End If
End Sub