PDA

View Full Version : Modifying COMport Code



Ajust
07-03-2019, 12:54 PM
Hello All,

I am trying to modify the code attached. Currently I can receive data from the COM port using this program. The problem is that all of the information goes into the A15 cell. I am taking torque readings from a calibration bench that outputs numbers 3-4 digits long. An example would be 95.63. After each torque reading I would like a new cell to populate with the data. For instance, the first reading would populate A15. The second would populate A16. The third would populate A17 and so forth. How would I go about modifying the code so that each reading is received in its own cell?

Thanks for all the help this forum is awesome!

24555

Paul_Hossler
07-03-2019, 01:09 PM
This is a guess since I can't test. I added to C15 and beyond since that was what the code said, not the ...



For instance, the first reading would populate A15. The second would populate A16. The third would populate A17 and so forth

A15 etc. and since col A had a lot of stuff in it




Public Sub Connect()
Dim strSettings As String, strData As String
Dim intPortID As Integer
Dim lngStatus As Long
Dim rData As Range ' <<<<<<<<<<<<<<<<<<<<<<<<<<<

strData = ""
strSettings = ""
Sheet1.Range("C15").Value = ""

With Sheet1

Set rData = .Range("C15") ' <<<<<<<<<<<<<<<<<<<<<<<<<<<

If .Range("A63").Value = True Then
strSettings = "baud=" & Left(.Range("D5").Value, Len(.Range("D5").Value) - 3) & " parity=" & Left(.Range("D7").Value, 1) & " data=" & .Range("D6").Value & " stop=" & .Range("D8").Value
intPortID = Mid(.Range("D2").Value, 4, Len(.Range("D2").Value) - 3)

lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), strSettings)

If lngStatus = 0 Then
MsgBox "Connection to " & .Range("D2").Value & " Established!"
.Range("A64").Value = 1
Do While .Range("A64").Value = 1
lngStatus = CommRead(intPortID, strData, 1)
If lngStatus > 0 Then
' .Range("C15").Value = .Range("C15").Value & strData
rData.Value = strData ' <<<<<<<<<<<<<<<<<<<<<
Set rData = rData.offset(1, 0) ' <<<<<<<<<<<<<<<<<<<<<
End If
'Sleep 500
DoEvents
Loop
Else
lngStatus = CommGetError(strError)
MsgBox "Connection Failed. " & strError
End If
Else
MsgBox "Please Ensure All COM Setting Are Set And COM Port Selected."
End If
End With

End Sub

Ajust
07-15-2019, 09:52 AM
Thank you very much this is working! How would I go about setting up a polling time per cell? Say I want to poll for 500ms before moving into a new cell, how would I do this?

mc720519
09-09-2019, 05:03 AM
I am trying to use your code to send command to serial port. however, when I send the command,
I always get IngStatus = 0, that is <> Len(strData), and then get the message "can not send Data"
How should I revise your code ?
Thanks in advance