Aug 24, 2012 Visual Basic Serial COM Port Tutorial-Part 1 download the source code.
This is actually a continuation of a post already started relative to serial communications. I have a legacy access 2000 database that has been in successful use for over 10 years. It uses a MSComm control to read data from a scale. Tupac greatest hits. After moving to access 2007 and windows 7 of course, the mscomm control no longer works. I found a control that is supposed to replace the mscomm control.
After replacing the mscomm control with the new control, following the suppliers instructions to the letter, it works, but when I close one of the forms containing the newly added control, Access crashes. I have worked on a solution for days and had a lot of help from other forum participants.
I have sort of come to the conclusion, that I need to take another route. So, I found a suggestion from someone that I handle serial communications with API calls. Not being very familiar with this routine, I downloaded the following code and added it to a button on a form. I assume that I would need to add some code, i.e. 'intPortID = 1' somewhere to set that variable to com1. I did that as the first line after the 'Initialze Communications line.
However, when I compile it, it says 'Sub or function not defined' on the 'lngStatus = CommOpen(intPortID, 'COM' & CStr(intPortID), 'baud=9600 parity=N data=8 stop=1')' line. What am I doing wrong? Can someone help me put this together to read data from a serial port? I don't need to write, only read.
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4 Dim lngStatus As Long Dim strError As String Dim strData As String ' Initialize Communications lngStatus = CommOpen(intPortID, 'COM' & CStr(intPortID)'baud=9600 parity=N data=8 stop=1') If lngStatus 0 Then ' Handle error. LngStatus = CommGetError(strError) MsgBox 'COM Error: ' & strError End If ' Set modem control lines. LngStatus = CommSetLine(intPortID, LINERTS, True) lngStatus = CommSetLine(intPortID, LINEDTR, True) ' Write data to serial port.
LngSize = Len(strData) lngStatus = CommWrite(intPortID, strData) If lngStatus lngSize Then ' Handle error. End If ' Read maximum of 64 bytes from serial port. LngStatus = CommRead(intPortID, strData, 64) If lngStatus 0 Then ' Process data. ElseIf lngStatus. Bruce, Again, thanks for helping me out, we are close I think. I think I am getting close, but have one hurdle that I cannot seem to solve. I have the code below.
When I run it, and send the string 'ST,+00009.28 g' it never seems to get to the exit do line, therefore stays in an endless loop. The string appears on my form, but that is as far as I get. Private Sub ReadClick Me.DataRead = ' Me.DataReadParsed = ' Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4 intPortID = 1 Dim lngStatus As Long Dim strError As String Dim strData As String Call CommClose(intPortID) ' Initialize Communications lngStatus = CommOpen(intPortID, 'COM' & CStr(intPortID), 'baud=2400 parity=E data=7 stop=1') If lngStatus 0 Then ' Handle error. LngStatus = CommGetError(strError) MsgBox 'COM Error: ' & strError End If ' Set modem control lines.
LngStatus = CommSetLine(intPortID, LINERTS, True) lngStatus = CommSetLine(intPortID, LINEDTR, True) ' Write data to serial port. LngSize = Len(strData) lngStatus = CommWrite(intPortID, strData) If lngStatus lngSize Then ' Handle error. End If ' Read maximum of 64 bytes from serial port.
Do While Right(Me.DataRead, 1) 'g' lngStatus = CommRead(intPortID, strData, 64) If lngStatus 0 Then Me.DataRead = Me.DataRead & strData If InStr(1, 'g', strData) 0 Then Exit Do End If ElseIf lngStatus 0 Then Exit Do 'End If it works, but I only get part of the string. I.e 'ST,+0000' Any Help?