[Solved] ByVal incomprehensible error

Creating a macro - Writing a Script - Using the API

[Solved] ByVal incomprehensible error

Postby migalhas » Mon Jun 14, 2010 11:37 am

Hello. This is an excerpt of a code snippet that gives me "syntax error Basic - symbol unexpected: ByVal" in the "ByVal Buffer" and I can not understand why this happens because in Microsoft Office Excel this don't give me any error and in OOo Calc yes.... I would appreciate to give me a hand here ..

Code: Select all   Expand viewCollapse view
Private Function CaptureData(ByVal cmd As String) As String
    Dim Buffer As String
    Dim Lp As Integer   
   
    Buffer = cmd
    dmy = WriteFile(hCommHandle, ByVal Buffer, LenB(Buffer), Count, ByVal 0)
    Buffer = Space(100)
    dmy = ReadFile(hCommHandle, ByVal Buffer, 100, Count, ByVal 0)
    Lp = InStr(Buffer, Chr(13)) - 1
    CaptureData = ""
    If Lp >= 2 Then
        CaptureData = Left(Buffer, Lp)
    End If

End Function
Attachments
Sem título.gif
this is the print screen of the error
Last edited by migalhas on Tue Jun 15, 2010 7:27 pm, edited 1 time in total.
OOo 3.2 installed in Windows XP
migalhas
 
Posts: 9
Joined: Fri May 28, 2010 12:59 pm

Re: ByVal incomprehensible error- NEED HELP

Postby Charlie Young » Mon Jun 14, 2010 12:30 pm

I suspect it's because you're using ByVal in the function calls (For WriteFile and ReadFile) - it's only needed in the declaration.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: ByVal incomprehensible error

Postby migalhas » Mon Jun 14, 2010 12:48 pm

yes I've noticed that, I tried this way and compiled without problems but when was running the macro gave me this error ..."argument is not optional"


Function CaptureData(ByVal cmd As String, ByVal Buffer) As String
Dim Buffer As String
Dim Lp As Integer

Buffer = cmd
dmy = WriteFile(hCommHandle, LenB(Buffer), Count)
Buffer = Space(100)
dmy = ReadFile(hCommHandle, 100, Count)
Lp = InStr(Buffer, Chr(13)) - 1
CaptureData = ""
If Lp >= 2 Then
CaptureData = Left(Buffer, Lp)
End If

End Function
Attachments
Sem título2.GIF
OOo 3.2 installed in Windows XP
migalhas
 
Posts: 9
Joined: Fri May 28, 2010 12:59 pm

Re: ByVal incomprehensible error

Postby B Marcelly » Mon Jun 14, 2010 3:57 pm

Hi,
You cannot run directly this macro, because it needs two arguments.
You have to create another macro from which you call your function with appropriate values for both arguments.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: ByVal incomprehensible error

Postby rudolfo » Mon Jun 14, 2010 5:03 pm

Not sure about the logic in your macro:
Code: Select all   Expand viewCollapse view
Function CaptureData(ByVal cmd As String, ByVal Buffer) As String
Dim Buffer As String

This means your are passing the parameter Buffer into the function and inside the function you declare a local variable with the same name Buffer. In all languages that I know, the local variable would hide/overlay the parameter variable.
And you pass it ByVal and not by reference: you won't see any change to the variable Buffer outside/after the function, anyway. In other words it behaves the same as a local variable would do, but it obfuscates your code.
So better forget about the second parameter:
Code: Select all   Expand viewCollapse view
Function CaptureData(ByVal cmd As String) As String
Dim Buffer As String
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: ByVal incomprehensible error

Postby migalhas » Mon Jun 14, 2010 5:15 pm

yes you are right. but the original code was this....

Private Function CaptureData(ByVal cmd As String) As String
Dim Buffer As String
Dim Lp As Integer


Buffer = cmd
dmy = WriteFile(hCommHandle, ByVal Buffer, LenB(Buffer), Count, ByVal 0)
Buffer = Space(100)
dmy = ReadFile(hCommHandle, ByVal Buffer, 100, Count, ByVal 0)
Lp = InStr(Buffer, Chr(13)) - 1
CaptureData = ""
If Lp >= 2 Then
CaptureData = Left(Buffer, Lp)
End If

End Function

but I was doing the code in excell when I pass the code to the OOo Calc it gaves me a lot of errors and I trying to eliminate that errors got thi error about "argument is not optional"....this code in Excell doesn't give any error but in Calc gives too much errors.I don't know how to resolve that problem....
OOo 3.2 installed in Windows XP
migalhas
 
Posts: 9
Joined: Fri May 28, 2010 12:59 pm

Re: ByVal incomprehensible error

Postby rudolfo » Tue Jun 15, 2010 1:14 am

You might have seen from my signature, that VBA as it comes from Microsoft is not Basic that comes with OpenOffice. There is some kind of VBA support in a Novell version of OOo -- this is actually frequently mentioned in this forum. You might try the Novell version...

But looking at your code and particularly at the ByVal and the fact that WriteFile sounds very much like a wrapper around the Win32 API function WriteFIle I have strong doubts that the Novell version will help you. Typically passing variables byValue is only required when you use the interface layer to the C API. In other words you will have lines like
Code: Select all   Expand viewCollapse view
Declare Sub WriteFile Lib "Kernel32" ...

at the beginning of your macro file.
This Declare ... lib functionality is not supported at all in OOo Basic. I have strong doubts that your code has chances to work in OOo.
But don't give up hope. Try to explain in precise human words what your macro is supposed to do, then the chances are higher to find someone here who can give you valuable hints or even some code snippets to get the job done. Please don't assume that the people here know how to read or code Microsoft's VBA. After all this is a forum for OpenOffice.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: ByVal incomprehensible error

Postby migalhas » Tue Jun 15, 2010 10:57 am

I had assumed that the code would not work at all in OOo Basic, but I had to try, the problem is that OOo is free and the Office does not, hence my change of program for that purpose because I can not in, any way, implement the final code in Office ...

I'm trying to read the data from the PC's serial port to an Excel sheet, all through a macro, but this is becoming a little tricky, you have any idea?
OOo 3.2 installed in Windows XP
migalhas
 
Posts: 9
Joined: Fri May 28, 2010 12:59 pm

Re: ByVal incomprehensible error

Postby Zizi64 » Tue Jun 15, 2010 11:27 am

I'm trying to read the data from the PC's serial port to an Excel sheet, all through a macro, but this is becoming a little tricky, you have any idea?


Discussion of "serial port handling in OpenOffice":

viewtopic.php?f=20&t=682

I tried to send with example code. That is worked. With a terminal program running on another PC, I receive the characters sent by OpenOffice.
But an instrument, I wanted to control, did not accept the characters sent to each, it only continuous command string expected.

I tried to receive with code too. That is not worked.
Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 8543
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] ByVal incomprehensible error

Postby Zizi64 » Mon Jun 21, 2010 3:14 pm

Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.3.3; AOO4.1.6
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 8543
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] ByVal incomprehensible error

Postby rudolfo » Mon Jun 21, 2010 7:34 pm

I think we need some clarification on this thread. As far as I see the solution does only work for a specific class of devices or a special kind of communication: This is if we have a request-response model. The application writes something on the serial port and can assume that the response will come back from the device in the next 5 or 10 seconds. In the example in thread viewtopic.php?f=20&t=31686 the Wait is required to fine tune this. If the application starts reading too early the stream or filehandle that was opened with the oFSO.OpenTextFile() will either block or return EOF depending if it reads synchronously or asynchronously (overlapped is the terminology for this in the Win32 WriteFile API).
Actually "blocking" on the file handle would not be too bad as this would only block until there are some bytes to read from the serial port. But it seems that the OpenTextFile method from the Windows Scripting Host object is not tailored to wait a "long" time for data on a file handle. And I am not suprised about this, because the method name OpenTextFile tries to tell us that it wants to be used for text files and not for low level (aka serial port) file access.
If you start the reading operation too late, you might have already lost some data, because the size of the buffer that the OS uses to handle the data bytes that come in on the serial port limited.

Sure if you experiment and test a bit you can tune the delay parameter in a way that your app would work in 99% of the time. But again this is only for the request-response communication type. If you have a different device (maybe a desktop phone or a DCF radio clock) that can send data on the serial port at any time you will need to poll the port or need to use an intermediate layer that fires events (to your app) and it might well be that the OP had such a case in mind.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests