While I know this is a VERY old topic, it is one that came up on searches for serial port interfaces, and there isn't a good answer. So, I thought I'd post one.
All this information is for Windows.
I'm going to provide links to cost-free software, but they aren't GPL. Your use of the components is up to your tolerance level.
Note: I'm building my interface in OOO Base. The concepts should be comparable if you use it for spreadsheet, etc.
First: the testing environment.
http://www.aggsoft.com/com-port-emulator.htm is a neat little program that sends data to a com port -- so if you create a text file with data you expect to receive from your data collection device, you can set the params to make your interface think it's talking to your data collection device, without having the expensive(?) device in your hands.
Next:
http://com0com.sourceforge.net/ This creates loopback nullmodem so you can program against the data being sent from the first device. For instance, COM5<->COM6, where COM6 might be the port being sent to from the com port emulator, and COM5 would be the COM port you're building the interface against (listening to).
Both of the above have much more features than this purpose, but if you've set these up, you now have an environment against which you can test your interface.
To the programming part:
Get Port.dll from
http://www.b-kainka.de/download.htm and set it in Windows/System32 (?) I suppose you could use IO.dll from
http://www.geekhideout.com/iodll.shtml but I didn't try it.
Code: Select all
Declare Sub OPENCOM Lib "Port.dll" (ByVal A$)
Declare Function OPENCOM Lib "Port.dll" (ByVal A$) As Integer
Declare Sub CLOSECOM Lib "Port.dll" ()
Declare Function READBYTE Lib "Port.dll" () As Integer
Rem Port.dll is obtained from http://www.b-kainka.de/elepcfaq.htm (Near the bottom, Port.zip)
Rem the reference file for programming is located here: http://www.b-kainka.de/referenz.txt
Sub Main
Dim strByte as string
Dim strE as string
Dim oForm As object
Call OPENCOM ("COM5:4800,N,8,1")
strByte = READBYTE()
Do While strByte = -1 'wait until valid data comes
strByte = READBYTE()
Loop
strByte=Chr$(strByte)
strE = READBYTE()
Do While strE <> 10 'get data until end of line. This is an ASCII code number (BYTE) so change for your own end of data line.
Rem It assumes that there will be no errors on READBYTE, so if you want to detect for (-1), you should do it here.
strByte = strByte & chr$(strE)
strE = READBYTE()
Loop
CALL CLOSECOM() 'MUST CLOSE when completed loop
To make it react to an event, you can
Code: Select all
Select Case strByte
Case "OK"
'stuff to do if you see an OK string
REM Check if you're connected to the data
if isNull( ThisDatabaseDocument.CurrentController.ActiveConnection ) then
ThisDatabaseDocument.CurrentController.connect()
end if
oForm = ThisDatabaseDocument.FormDocuments.getByName("PrintMe") 'title of the Document of type Form in "Forms"
oForm.open() 'open form
oForm = oForm.Component.DrawPage.Forms.getByName("Form") 'title of the form (internally. Might be "MainForm". Check the Form Navigator.)
oForm.Filter = "PhoneNumber LIKE " + "'" + strByte + "'" ' example to set a filter on a form
oForm.ApplyFilter = True 'Apply the filter
oForm.Reload 'Reload the form to reflect the filter change
End Select
End Sub
So what about writing to the port?
Code: Select all
Declare Sub OPENCOM Lib "Port.dll" (ByVal A$)
Declare Function OPENCOM Lib "Port.dll" (ByVal A$) As Integer
Declare Sub SENDBYTE Lib "Port" (ByVal B%)
Sub Main
Dim I
Dim strSendstring as string
Call OPENCOM ("COM5:4800,N,8,1")
strSendstring = "Hello, World!" ' I don't know if you'll need it, but you can add + Chr(10) + Chr(13) to provide line feeds/carriage returns
For I = 1 to Len(strSendstring)
Call SENDBYTE(Asc(Mid(strSendstring, I, 1)))
Next I
CALL CLOSECOM() 'MUST MUST MUST close or next OPENCOM will crash your app!
End Sub
To keep listening or keep sending, you can wrap stuff in a bigger loop. You might want to test versus a button press or value change to be able to exit your loop without having to end-task on OOO.
Hope it helps. If you need more instruction, use Google Translate against the documentation if you can't read German. I may not ever return to answer any questions about this, but maybe someone will be helped by this being in one place.