[Solved] Get number of bytes to read from stream

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
VBandOoffice
Posts: 33
Joined: Tue Jul 31, 2018 10:11 am

[Solved] Get number of bytes to read from stream

Post by VBandOoffice »

Hi everyone,

I'm reading measurement results from a external device via TCP/IP.
This works perfect as long as I know the number of bytes to read.
Is there a way to get the number of bytes to read in advance?

The only workaround I found is, to transmit the number of result bytes prior to the result.
This is only possible if I'm able to manage the result as well.

I'm using CreateUnoService("com.sun.star.connection.Connector") for the connection.

Have a nice day,
-VBandOoffice-
Last edited by MrProgrammer on Wed Feb 28, 2024 6:49 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
LibreOffice 7.5, Windows 10, VB.net 2019, C#
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to get number of bytes to read from stream

Post by Lupp »

I'm not familiar with your situation.
However: Do you know https://api.libreoffice.org/docs/idl/re ... tream.html ?
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Jurassic Pork
Posts: 24
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: How to get number of bytes to read from stream

Post by Jurassic Pork »

Hello VbandOoffice,
what is your code ?
Friendly, J.P
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
VBandOoffice
Posts: 33
Joined: Tue Jul 31, 2018 10:11 am

Re: How to get number of bytes to read from stream

Post by VBandOoffice »

Hello Jurassic Pork,
I'm basicly using a code example from OpenOffice Macros explained:
(that's why the comments are in German language)

Code: Select all

Sub Main
  Dim oConnector                'Connection-Objekt
  Dim oConnection               'Die aufzubauende Verbindung
  Dim sConDesc As String     'Verbindungsbeschreibungen
  Dim nOrder(5) As Integer      'Container für die Anfrage an Server
  Dim nReceived() As Integer   'Container für Antwort vom Server
  Dim nBytesSended As Long      'Anzahl der gesendeten Bytes
  Dim nBytesReceived As Long    'Anzahl empfangener Bytes
  Dim i As Integer
  Dim n As Integer              'Index der Verbindungsbeschreibungen
  Dim dValue As Double          'Long ist zu klein
  Dim sResult As String         'Die Antwort als String
  
  'Verbindungsbeschreibung
  'enthält die Beschreibung der Verbindung als kommagetrennte Werteliste
  'z.B. socket,host=localhost,port=2345 für eine TCP/IP-Verbindung. 
  
  sConDesc = "socket,host=localhost,port=6315"
  
  'Objekt für den Verbindungsaufbau erzeugen
  oConnector = CreateUnoService("com.sun.star.connection.Connector")
  
  On Local Error Goto Error
  'Erstellt eine neue Verbindung zur Interprozesskommunikation.
  'Exception: NoConnectException, ConnectionSetupException
  oConnection = oConnector.connect(sConDesc)
  
  'Anfrage senden
  nOrder(0)= asc("M")
  nOrder(1)= asc("E")
  nOrder(2)= asc("A")
  nOrder(3)= asc("S")
  nOrder(4)= asc("?")
   
  nByteSended = oConnection.write(nOrder(),6)
  'Antwort empfangen
  'xray oConnection
  'Länge des Datenfeldes lesen
  nBytesReceived = oConnection.read(nReceived(),2)
  
  Dim j As Integer
  j = nReceived(0)
  
  nBytesReceived = oConnection.read(nReceived(),j)
  'xray nBytesReceived
 
  
  'Verbindung schließen
  oConnection.close()
  
  'sResult = "Anzahl empfangener Bytes: " & nBytesReceived & CHR$(10) & _
   '    "Byte 0: " & nReceived(0) & CHR$(10) & _
    '   "Byte 1: " & nReceived(1) & CHR$(10) & _
     '  "Byte 2: " & nReceived(2) & CHR$(10) & _
      ' "Byte 3: " & nReceived(3)
  
  'Byte array To string     
  For i = 0 To j-1
    sResult = sResult + chr(nReceived(i))
  next
 
  
  MsgBox sResult
  Exit Sub
  
Error:
  MsgBox "Server error."
 
End Sub
Best regards,
VBandOoffice
LibreOffice 7.5, Windows 10, VB.net 2019, C#
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to get number of bytes to read from stream

Post by Lupp »

I couldn't find a single mentioning of the service you are using as oConnector in Andrew's texts. Could you, please, point me to the source you based your macro on?
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Jurassic Pork
Posts: 24
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: How to get number of bytes to read from stream

Post by Jurassic Pork »

have a look :shock: to Interface Xconnection2 with the methods available and readSomeBytes
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
VBandOoffice
Posts: 33
Joined: Tue Jul 31, 2018 10:11 am

Re: How to get number of bytes to read from stream

Post by VBandOoffice »

Hello Lupp,
I found it in the german translation, which has moved to:
https://makromador.files.wordpress.com/ ... ktuell.pdf
Pages 279-284.
Viele Grüße aus dem sonnigen Frankenland
-VBandOoffice-

Hi Jurassic Pork,
this is a good hint - I'll try to use it and I will post code, when I'm successful.
Best regards,
-VBandOoffice-
LibreOffice 7.5, Windows 10, VB.net 2019, C#
User avatar
Jurassic Pork
Posts: 24
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: How to get number of bytes to read from stream

Post by Jurassic Pork »

Hello,
sorry but it seems that it is not possible to use Xconnection2 with Basic, see here
maybe you can use python macro with socket module to read data from your network device.
Example in a user python script file :

Code: Select all

# coding: utf-8
from __future__ import unicode_literals
import uno
import unohelper
import socket

CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
desktop = XSCRIPTCONTEXT.getDesktop()
ODOC = desktop.getCurrentComponent()
        
def testTcp(*args):
	client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	client.connect(("localhost", 6315))
	client.sendall(b"MEAS?")
	data = client.recv(1024)
	client.close()
	print('Received',repr(data))
	sheet = ODOC.CurrentController.ActiveSheet
	A2 = sheet.getCellRangeByName('A2')
	A2.setString(data.decode('utf-8'))
	
Result in the A2 cell of the active sheet.


Friendly, J.P
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
VBandOoffice
Posts: 33
Joined: Tue Jul 31, 2018 10:11 am

Re: How to get number of bytes to read from stream

Post by VBandOoffice »

Hi Jurassic Porc,
thank you for your suggestion. I've never used python script for OpenOffice.
Maybe I should do this in the future.
I'll keep your suggestion in mind.
Have a nice weekend,
-VBandOoffice-
LibreOffice 7.5, Windows 10, VB.net 2019, C#
Post Reply