Serial Port Access

Creating a macro - Writing a Script - Using the API

Serial Port Access

Postby andybuonviri » Sun Dec 09, 2007 8:04 am

Anyone know if it's possible to access a serial port from a macro within Calc? From Excel, I've used something like this:

Open "COM8:9600,N,8,1" For Output As #1
Print #1, mytext
Close #1

I tried the same thing in a Basic macro in Calc but it didn't work.
andybuonviri
 
Posts: 1
Joined: Sun Dec 09, 2007 7:59 am

Re: Serial Port Access

Postby ms777 » Sun Dec 23, 2007 2:09 pm

Hi,

it is possible, but much more complicated. You will have to implement the code from http://support.microsoft.com/kb/823179/en-us (example module1.vb) in OO, using OO's declare syntax. If yoy want to go that way let me know, if you need assistance

Good luck,

ms777
ms777
Volunteer
 
Posts: 129
Joined: Mon Oct 08, 2007 1:33 am

Re: Serial Port Access

Postby TerryE » Sun Dec 23, 2007 3:27 pm

If you read this article, it basically discusses two approaches. The first uses the MSComm Control (MSCOMM32.OCX). Do a google on "mscomm32.ocx download" for more information and avaialbility. There is a potential issue over legality / licensing. The second approach is to use some .Net controls which should be accessible through the UNO bridge, and these will require you to download the relevant .Net framework. However both are complicated solutions.

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 :-)
Ubuntu 11.04-x64 + LibreOffice 3 and MS free except the boss's Notebook which runs XP + OOo 3.3.
TerryE
Volunteer
 
Posts: 1402
Joined: Sat Oct 06, 2007 10:13 pm
Location: UK

Re: Serial Port Access

Postby ms777 » Sat Aug 30, 2008 3:02 pm

it may be possible to use the Scripting.FileSystemObject (http://msdn.microsoft.com/en-us/library/hww8txat(VS.85).aspx). As I have no device to test on my com port, this is untested, but runs without error message:
Code: Select all   Expand viewCollapse view
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

Good luck,

ms777
ms777
Volunteer
 
Posts: 129
Joined: Mon Oct 08, 2007 1:33 am

Re: Serial Port Access

Postby wienerschnitzel » Sat Jan 17, 2009 7:52 am

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?
OOo 3.0.X on Ms Windows XP + linux
wienerschnitzel
 
Posts: 25
Joined: Sat Jan 17, 2009 7:48 am

Re: Serial Port Access

Postby ms777 » Sat Jan 17, 2009 7:58 am

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?

I would be very surprised if this i possible ...
ms777
Volunteer
 
Posts: 129
Joined: Mon Oct 08, 2007 1:33 am

Re: Serial Port Access

Postby wienerschnitzel » Sat Jan 17, 2009 8:14 am

Hi ms777,

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.

cheers
OOo 3.0.X on Ms Windows XP + linux
wienerschnitzel
 
Posts: 25
Joined: Sat Jan 17, 2009 7:48 am

Re: Serial Port Access

Postby crythias » Sat Dec 18, 2010 4:51 am

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   Expand viewCollapse view
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   Expand viewCollapse view
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   Expand viewCollapse view
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.
OpenOffice 3.2.1 on Windows Vista
crythias
 
Posts: 1
Joined: Sat Dec 18, 2010 3:34 am

Re: Serial Port Access

Postby michaelgmaloy » Thu May 05, 2011 7:23 pm

everything seems to work fine
gsm amplifier for house
Is GSM Booster Useful To You?
michaelgmaloy
 
Posts: 1
Joined: Thu May 05, 2011 7:22 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 10 guests