Reading Serial Port Data in VBA

srathjen

New Member
Joined
May 2, 2016
Messages
2
I'm having trouble getting code to read the incoming data send by a torque reader I'm working with. It sends a RS232 data stream which our current wedgelink software can be set up to translate it into key presses and then exported to excel. I would like to get the data into a string then cut the string up into an array of arrays to be able to use the data directly with excel. There is a ton of stuff how to do it in other languages but I'm limited to VBA right now.

This link https://dl.dropboxusercontent.com/u/37968177/modCOMM.bas has the base code someone wrote where i can open and close the port but im not getting any data or able to hold it open until end of the data feed. I'm still new at this and know I'm missing something.

Serial Port Communication in Excel (VBA) | Electronics Open Source was the first place i found this code. Any ideas?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Sub OpenPort_Click()


Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Dim strData As String
Dim datFeed As String: datFeed = ""
Dim i As Integer

intPortID = 1


' Open COM port
lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), "baud=9600 parity=N data=8 stop=1")


'cycle through Com ports 1-8 until found or not there
If lngStatus <> 0 Then
For i = 2 To 8
Call CommClose(intPortID)
intPortID = i
lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), "baud=9600 parity=N data=8 stop=1")
If lngStatus = 0 Then
Exit For
End If
Next i
End If



If lngStatus <> 0 Then
MsgBox "Error: lngStatus = " & lngStatus
' this should get the strData untill end of file.
Else
MsgBox "Com Port Open"
Do While CommRead(intPortID, strData, 64) <> EOF
datFeed = datFeed + strData
Loop
' this is blank, no data colected, help
MsgBox datFeed
End If

Call CommClose(intPortID)

End Sub


This is my call button. it needs help to hold open the comm port while our device is sending data. any help would be much appreciated, thanks
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top