Private Sub CommandButton1_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Range("A1:A21").Select
Selection.ClearContents
Range("A1").Select
intPortID = Sheet1.Cells(19, 5)
' Open COM port
lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), _
"baud=9600 parity=O data=7 stop=1")
End Sub
Private Sub CommandButton2_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Dim strData As String
intPortID = Sheet1.Cells(19, 5)
strData = Sheet1.Cells(1, 2)
'Writa data
lngStatus = CommWrite(intPortID, strData)
End Sub
Private Sub CommandButton3_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Dim strData As String
Dim out_of_time As Boolean
Dim time As Single
Dim buffer As String
Dim i As Integer
For i = 1 To Sheet1.Cells(21, 5) 'Take 20 readings
buffer = ""
time = Timer()
intPortID = Sheet1.Cells(19, 5)
Do
lngStatus = CommRead(intPortID, strData, 13)
Loop Until strData = "#"
Do
lngStatus = CommRead(intPortID, strData, 13)
buffer = buffer & strData
out_of_time = Timer() > time + 10
Loop Until strData = Chr(13) Or out_of_time
If out_of_time <> False Then
buffer = "Out Of Time"
End If
buffer = Mid(buffer, 1, Len(buffer) - 2) 'Remove last two Characters (LF)&(CR)from string
Sheet1.Cells(i, 1) = buffer 'Plot Data to Sheet1 Cell 1 to 20
Next i 'Get next reading
MsgBox ("Test Complete")
Call CommClose(intPortID)
End Sub
Private Sub CommandButton4_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
intPortID = Sheet1.Cells(19, 5)
Call CommClose(intPortID)
End Sub
Private Sub CommandButton5_Click()
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Dim strData As String
Dim buffer As String
intPortID = Sheet1.Cells(19, 5)
strData = "."
'Writa data
lngStatus = CommWrite(intPortID, strData)
End Sub
Hi IanWade
I'm using the code you asked about as is with the above mod to the last part of the code to read data from an scale instrument into sheet 1.
The code works fine but need some tweaking to use less resources.
I'm still a noob with VBA but maybe this will help.
Please let me know if you can get it to use less cpu power when reading from the com port.
Bookmarks