Hi guys,
My code for my project was developed based on Pikes code in http://www.ozgrid.com/forum/showthread.php?t=168472&page=2
I tested my code with an equipment that it comes with a "Trasnfer Data" button on it (this sends the data from the serial port to a specified cell in excel), but, now, I'm trying to create a button in excel, which will do the same thing as the "Transfer data" send to command to the serial port to get data off the equipment and input into a specific cell (say B5).
Here's the code i have for the button, but i kept getting error message about the #COM1file
Code
[FONT=Verdana]Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
[/FONT]
Sub Receive_COM1()
Dim COM1file As Integer
Dim timeout As Date
Dim record As String * 11, emptyRecord As String * 11
Dim recLen As Integer
Dim inputByte As Integer
recLen = Len(record)
COM1file = FreeFile
Open "COM1:4800,E,7,2" For Random As #COM1file Len = recLen
'Monitor port for 30 seconds
timeout = Now + TimeValue("00:00:30")
Debug.Print "Started"
While Now < timeout
Get #COM1file, , record
If record <> emptyRecord Then
Debug.Print Now; "<" & record & ">"
'Display each byte
For i = 1 To recLen
inputByte = Asc(Mid(record, i, 1))
If inputByte = 0 Then
'No character in this position
ElseIf inputByte >= 32 And inputByte <= 126 Then
'Printable character
Debug.Print "<" & inputByte & "> "; Chr(inputByte)
Else
'Non-printable character
Debug.Print "<" & inputByte & ">"
End If
Next
End If
DoEvents
Sleep 200
Wend
Close #COM1file
Debug.Print "Finished"
End sub
Display More
Hope its make sense for you. Thanks.
Regards,
Xin