Page 1 of 1

[Solved] Error reading serial port on read function...

Posted: Fri Jun 18, 2010 5:50 pm
by migalhas
Hy....
I was writing a macro to open and read the serial port to a calc file, but I'm only try to open the port and read their data to a msg box when an error has ocurred saying that (see image please)...anyone could help me??

Code: Select all

Sub Main
Dim s as string
  oFSO  = createUnoService("com.sun.star.bridge.OleObjectFactory").createInstance("Scripting.FileSystemObject")

  f = oFSO.OpenTextFile("COM4:9600") 'for Reading
'  f = oFSO.OpenTextFile("c:\teste.txt", 2) 'for Writing
'  f.write("funciona")
msgbox ("Começando a leitura")
'msgbox ("Começando a escrita")
s = f.Read()

  f.close
  msgbox s

End Sub

Re: Error reading serial port on read function...

Posted: Mon Jun 21, 2010 2:10 pm
by Zizi64
WOW, it's working!

I am trying again the sending/receiving data on serial port.
I have an ASCII char controlled measuring instrument.

Code: Select all

Function Write_and_Read(OutputStr as string) as String 
Dim f as object
Dim i as integer
Dim OutputChar, InputStr as String

  oFSO  = createUnoService("com.sun.star.bridge.OleObjectFactory").createInstance("Scripting.FileSystemObject")

' WRITING:

	f = oFSO.OpenTextFile("COM1:9600,N,8,1", 2) 'for Writing
		For i= 1 to Len(OutputStr)
 			OutputChar=(Mid(OutputStr,i,1)) 
 			f.write(OutputChar) 'Sending by each character
 		next i
                OutputChar=Chr(13) 'CR
 		f.write(OutputChar)
 		OutputChar=Chr(10) 'LF
 		f.write(OutputChar)
 		
	f.close
  

  
' READING: 
 
	f = oFSO.OpenTextFile("COM1:9600,N,8,1", 1) 'for Reading
		Wait(5000) 'NEEDED a timing! (Maybe it is instrument specific value.)
		Write_and_Read  = f.read(10) '10 character will be readed (Not works if I try read 15 chars! Works if I read less than 10 chars, but the answer will be truncated)
	f.close

End function
The MEASURE command (input parameter of function) is 'ME+Chr(13)+Chr(10)';
The Answer: '0 ,288.25 ' it means: 0 (no error; 288.25 is the temperature value in Kelvin)


I am very happy!

Re: Error reading serial port on read function...

Posted: Mon Jun 21, 2010 6:10 pm
by migalhas
WOOOOOW....Really nice...Thank you Zizi...I was trying to do this almost a month and I couldn't...Now with some modifications I could do what I really wanted at 100%...Thank you very much Zizi...My error was only the expression Wait that I wasn't put it on my code... ;)

Re: Error reading serial port on read function...

Posted: Mon Jun 21, 2010 7:40 pm
by Zizi64
migalhas wrote:
sorry...I forgot one thing...my code is based on writing the data readed by the serial port every time you press a button and this button sends the information to the serial port, but the the macro always write data in the same line at same cells and I wanted it to write the information on line 1, when I press the button again it writes on line 2 etc ... until I used a button to stop the information sent and received in serial port ...

Function Write_and_Read(OutputStr as string) as String
Dim f as object
Dim i as integer
Dim linha as integer
Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object
Dim OutputChar, InputStr as String

oFSO = createUnoService("com.sun.star.bridge.OleObjectFactory").createInstance("Scripting.FileSystemObject")

' READING:
Doc = ThisComponent
Sheet = Doc.Sheets("Sheet1")
CellA = Sheet.getCellRangeByName("A1")
CellB = Sheet.getCellRangeByName("B1")
CellC = Sheet.getCellRangeByName("C1")
f = oFSO.OpenTextFile("COM4:9600", 1) 'for Reading
Wait(3500) 'NEEDED a timing! (Maybe it is instrument specific value.)
Write_and_Read = f.read(30) '10 character will be readed (Not works if I try read 15 chars! Works if I read less than 10 chars, but the answer will be truncated)


col = 1
CellA.string = Mid(Write_and_Read, 1, 7)
CellB.string = Mid(Write_and_Read, 8, 7)
CellC.string = Mid(Write_and_Read, 15, 7)
f.close

End Function
'line1', 'line2'... means: 'row1', 'row2'... in spreadshhet, or number of serial ports (number of instruments)?
You connect only one instrument to your PC?
The instrument is a slave, and your PC is a master? (Request-Answer mode?)

my suggestion:
If you use only one button, you need use an integer type global variable. Increment it when a data received. Use "Select case" command to write data separated into destination cells. Reset the variable to start value, when the incremented value >= max. value. You can use the same variable for the synchronization the questions (write the serial port) and responses (read the serial port). (First request -> First answer -> Put into First cell ...)

If you use three button: Write three functions with 3 different output cell controlling. Assign to every button one of three functions. This method does not require synchronization.

Maybe you need write SUB(s), not FUNCTION(s).

(and read: post by rudolfo » 2010 Jun 21, 7:34 pm in
http://user.services.openoffice.org/en/ ... 20&t=31536)

Re: [Solved] Error reading serial port on read function...

Posted: Sat Sep 04, 2010 3:01 pm
by ForrestErickson
Dear migalhas and Zizi64.
Thanks for this interesting information.

I am also trying to use the serial port using
createUnoService("com.sun.star.bridge.OleObjectFactory").createInstance("Scripting.FileSystemObject")

I am factory testing a serial port device by sending it some text and checking that it was echoed back. So I need to read and write and most important and I need a way to recover if the device under test does not respond with in a second or so.

My current read routine simply hangs waiting for the device under test and I have to reload the test.
My guess is that there is some sort of time out for the serial port but I have no idea how to set it and how to detect an error due to time out.

Suggestions would be appreciated.

Re: [Solved] Error reading serial port on read function...

Posted: Sat Sep 04, 2010 5:17 pm
by Zizi64
Hi, ForrestErickson,

I think, the OpenOffice in not the best device for that task.
Maybe is possible to create a working OOBasic macro, but the object oriented software development tools (any language: for example I am using Pascal, Delphi, or the freeware Lazarus project for creating similar tasks), with event controlled features, are more efficient than OpenOffice.

Why you are want to use the OOo for this task? Are you want to create automatically a "Test Report" for serial port devices?

Re: [Solved] Error reading serial port on read function...

Posted: Sat Sep 04, 2010 5:59 pm
by ForrestErickson
Dear Tibor Kovacs,
Thanks for replying.

Regarding why, "Why you are want to use the OOo for this task", I thought with a name like basic it would be easy to program (from my days programing in the old DOS BASICA) It never occurred to me that the serial port would be so impossibly hard to use.

Unfortunately, We already have invested quit a bit of effort into our OO.org solution which works but slowly and error prone buy shelling out to other programs.

I have searched around hoping to find an open source software instrument control / factory test project with out success. So we started with what we had, OO.org basic/macros.

Thanks again,
Forrest Erickson

Re: [Solved] Error reading serial port on read function...

Posted: Wed Aug 10, 2011 1:17 pm
by ForrestErickson
May we see the syntax for async_read and async_write?
Perhaps I should have been asking. Where are the "methods" defined for the serial port setup.
Any idea on setting a time out which can allow programing with exception catching?

Re: [Solved] Error reading serial port on read function...

Posted: Wed Aug 10, 2011 8:41 pm
by rudolfo
Note that carla23 is doing this on Mac OSX with a BSD Unix based core operating system. On Unix everything is a file, the serial port is just another file with a file descriptor. Unix support blocking and async writing and reading from files (including serial devices). For MS WIndows this is completly different. Serial ports have their own API that you need to use when accessing data on the serial port. The write command and the read command of Basic (or the one of the Scripting Filesystem Object) are following the old DOS convention of special files COM1, PRT, etc. DOS and asynchronous are a contradiction. In other words with Basic async I/O is not possible.
I don't think that the devs have any motiviation to extend OOo Basic with async I/O, because if you need asynchronous I/O you can always swap to one of the other "adult" macro languages like Python or Java that support this.

Re: [Solved] Error reading serial port on read function...

Posted: Thu Aug 11, 2011 3:22 pm
by ForrestErickson
Thanks for helping me understand what the limitation are against which I am bumping.

Regarding, "if you need asynchronous I/O you can always swap to one of the other "adult" macro languages like Python or Java that support this."

I have developed some Python scripts to write and read back from the serial port using Python, IDLE and the serial python library pySerial. But then I could not figure out how to get them to work as macros from OO.org Basic. I could call them, but they would error out on the "import pySerial" command. I assume it is because I some how have to add pySerial to the OO.org Python installation or provide a path name. Is it even possible to "import pySerial" to the Python integrated with OO.org basic?

Re: [Solved] Error reading serial port on read function...

Posted: Thu Aug 11, 2011 11:04 pm
by rudolfo
ForrestErickson wrote:...But then I could not figure out how to get them to work as macros from OO.org Basic.
I guess you mean calling them from OpenOffice.org. Calling a pyhton macro from within the code of a Basic macro doesn't make much sense.
Actually if you are saying that you get an error in the line "import pySerial", this error comes from the python interpreter and it surely indicates that you could successfully start the interpreter. Or in other words: you could run a python macro.

So as you guessed your problem is that the python runtime that is included in OOo doesn't have the pySerial module or library. It shouldn't be too difficult to locate the python library directory within the OOo installation directory. Just search for .py or .pyc files. If the major version of the python interpreter of OOo on the one installed directly in the OS is the same copying the files of the pySerial module into the python directory of the OOo installation has good chances to work.
You can search this forum for python and modules. I remember two or three threads discussing this constellation of two coexisting python installations. If the search doesn't help, maybe browse through the posts of the user hanya.

Re: [Solved] Error reading serial port on read function...

Posted: Mon Sep 17, 2018 6:58 pm
by mrusydi
Hello, right now, i'm on same project, read data from a COM port - RS232, but still not working.

i used php_serial.class.php (i get from php-serial-master) did not get any data from hyper terminal

i try to this code (forgot where i get it from internet) below too, but it still not working at all, would you mind to share the code that you use? FYI, data that we want to get is from scaling devices which has a modification cable that cross between cable number 3 and 5 if i'm not mistaken

Code: Select all

<?php

$cmd_str = "MODE COM3: BAUD=9600 PARITY=N DATA=8 STOP=1 XON=OFF TO=OFF OCTS=OFF ODSR=OFF IDSR=OFF RTS=OFF DTR=OFF";
$output = array();
exec($cmd_str, $output, $result);

set_time_limit(0);
 
$serial_port = fopen("COM3","rn");
$hasil = fgets($serial_port,9600);
fflush($serial_port);
fclose($serial_port);
  print("<pre>");
print_r($cmd_str);
print("\n");
print_r($output);
print("\n");
print_r($hasil);
print("\n");
print("</pre>");
exit;
 
$serial_port = fopen("COM4","wb");
$a = chr(1);
$hasil = fwrite($serial_port,$a);
fclose($serial_port);

?>

Re: [Solved] Error reading serial port on read function...

Posted: Tue Sep 18, 2018 6:54 am
by Zizi64
Using a serial port inside the AOO/LO: it is a dead project today. Use a standalone software application for send and receive data. And then you can - if you are an expert - get the received data from the application as DDE data, or by a function calling ... or by other methods.

Re: [Solved] Error reading serial port on read function...

Posted: Thu Jan 10, 2019 11:56 pm
by edgar.blaustein
Could one of the generous experts on this forum help with a difficulty in connecting to a modem from a CALC macro? (This is my last difficulty in porting a couple of Windows EXCEL based office programs to UBUNTU-OO).

The following is a slightly simplified version of code submitted to this forum by migalhas and zizi, at the top of this thread.

*****************
Option explicit
Sub Write_to_modem()
Dim OutputStr as string
Dim f as object
Dim i as integer
Dim OutputChar
Dim oFSO
OutputStr = " ATDT0123456789" & Chr(13) & Chr(10)

oFSO =
createUnoService("com.sun.star.bridge.OleObjectFactory").createInstance("Scripting.FileSystemObject")

f = oFSO.OpenTextFile("COM3:9600,N,8,1", 2) 'correct settings for my modem
' but this version crashes
' with "unexpected exception"

'f = oFSO.OpenTextFile("COM1:9600,N,8,1", 2) 'as written in this forum,
'wrong port number,
but does not crash

For i= 1 to Len(OutputStr)
OutputChar=(Mid(OutputStr,i,1))
f.write(OutputChar) 'Sending by each character
next i
f.close

End Sub

************************

This macro crashes at the .OpenTextFile command, with and "unexpected exception" error message.


I have tried different settings for the modem, including changing from my modem (COM3) to a non existant COM1 modem.

The following is a snippet of the VBA macro that I want to replace. This code works correctly.

********************
REM VBA macro that operates correctly
Sub DialNumber()

Dim PhoneNumber as String
Dim CommPort As String
Dim bModemCommand(256) As Byte, ModemCommand As String
Dim OpenPort As Long
Dim i As Integer
PhoneNumber = "0123456789"
' Open the communications port for read/write (&HC0000000).
' Must specify existing file (3).
OpenPort = CreateFile("COM3", &HC0000000, 0, 0, 3, 0, 0)

ModemCommand = "ATDT" & PhoneNumber & vbCrLf
' Pack the string in a Byte array.
For i = 0 To Len(ModemCommand) - 1
bModemCommand(i) = Asc(Mid(ModemCommand, i + 1, 1))
Next

' Write the string to the Com port.
RetVal = WriteFile(OpenPort, bModemCommand(0), _
Len(ModemCommand), RetBytes, 0)

End Sub

*******************************

Related question, in case this problem turns out to be insoluble: can anyone indicate a Ubuntu compatible stand alone dialer, that could be chained to, in order to replace this code?

Many thanks for your aid.

Re: [Solved] Error reading serial port on read function...

Posted: Fri Jan 11, 2019 8:20 am
by Zizi64
I can repeat it only:

Using a serial port (communicate over a serial line/protocoll) inside the AOO/LO: it is a dead project today. Use a standalone software application for send and seceive data. And then you can - if you are an expert - get the received data from the application as DDE data, or by a function calling ... or by other methods.

My code will not work with the recent versions of the AOO or LO. This code worked only with an old version of the OOo.

Re: [Solved] Error reading serial port on read function...

Posted: Fri Jan 11, 2019 12:00 pm
by edgar.blaustein
Thank you Zizi pour your rapid response.

As you suggest, I will look for external software to solve this problem.