Open "COM8:9600,N,8,1" For Output As #1
Print #1, mytext
I tried the same thing in a Basic macro in Calc but it didn't work.
However, since you can use COM controls through the UNO bridge, this got me thinking about simpler non-MS approaches and did a google on "com port vbscript" (since the programmatic approach is the same as writing a vbscript, but I'll get far more hits on vbscript than OOo Basic!). One of the early hits was http://www.activexperts.com/activcomport/ which seems to do what you want. No guarantees. You will need to try it out etc. Also read section 3 of the SDK which tells you how to call com components, but once you've done that using a COM component is really quite straight forward but unfortunately MS OS (e.g. WinXP et al) specific.
And an apology to ms777 for seeming to jump on his/her suggestion. The truth is that I've come back to this "No responses" Q a couple of times and couldn't think of an easy solution. It was his pointing out the kB article which gave me the idea, so the honours really belong to ms777
Code: Select all
Sub Main oFSO = createUnoService("com.sun.star.bridge.OleObjectFactory").createInstance("Scripting.FileSystemObject") f = oFSO.OpenTextFile("COM1:9600,N,8,1", 1) 'for Reading ' f = oFSO.OpenTextFile("COM1:9600,N,8,1", 2) 'for Writing ' f.write("abc") f.close End Sub
I would be very surprised if this i possible ...wienerschnitzel wrote:Hi,
This seems to work. It would be nice though if I could register an event if a character arrives on the port. Is there a way to achieve this?
What I meant to say is that your sample code using the scripting.FileSystemObject seems to work. I just tested it with a loopback cable.
However, I need to be able to react on characters coming in on the serial port. I don't think polling would work in my case.
A couple of posts up, somebody mentioned an activex control, but that one doesn't seem to support events either.
I'm going to start a new topic.
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
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
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
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.