Page 1 of 1

[Solved] Get number of bytes to read from stream

Posted: Mon Jan 29, 2024 8:31 am
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-

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

Posted: Mon Jan 29, 2024 2:11 pm
by Lupp
I'm not familiar with your situation.
However: Do you know https://api.libreoffice.org/docs/idl/re ... tream.html ?

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

Posted: Mon Jan 29, 2024 2:49 pm
by Jurassic Pork
Hello VbandOoffice,
what is your code ?
Friendly, J.P

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

Posted: Mon Jan 29, 2024 4:34 pm
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

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

Posted: Mon Jan 29, 2024 5:07 pm
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?

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

Posted: Mon Jan 29, 2024 7:39 pm
by Jurassic Pork
have a look :shock: to Interface Xconnection2 with the methods available and readSomeBytes

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

Posted: Tue Jan 30, 2024 10:29 am
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-

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

Posted: Tue Jan 30, 2024 3:07 pm
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

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

Posted: Fri Feb 02, 2024 11:29 am
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-