Python Remote API: simxSetStringSignal

Report crashes, strange behaviour, or apparent bugs
Post Reply
el_Raimundo

Python Remote API: simxSetStringSignal

Post by el_Raimundo »

Hello there,
I'm using vrep 3.2.0 (rev6) and I believe there's a wrong argument type specified in Line 119 in the vrep Python remote API source code. Here's the original:

Code: Select all

c_SetStringSignal           = CFUNCTYPE(c_int32,c_int32, POINTER(c_char), POINTER(c_ubyte), c_int32, c_int32)(("simxSetStringSignal", libsimx))
where it should actually be POINTER(c_char) two times, for signal name as well as signal value, like this:

Code: Select all

c_SetStringSignal           = CFUNCTYPE(c_int32,c_int32, POINTER(c_char), POINTER(c_char) , c_int32, c_int32)(("simxSetStringSignal", libsimx))
no?

coppelia
Site Admin
Posts: 10366
Joined: 14 Dec 2012, 00:25

Re: Python Remote API: simxSetStringSignal

Post by coppelia »

Hello,

the signal name is a regular string, i.e. chars, but cannot contain embedded zeros.
The string signal value is a buffer of random values (i.e. bytes), which can also contain embedded zeros. In some languages, strings can also contain embedded zeros, and in that case, there is hardly a difference with a regular string.

Cheers

el_Raimundo

Re: Python Remote API: simxSetStringSignal

Post by el_Raimundo »

I see,
but if I leave the argument type mentioned above at POINTER(c_ubyte) the python interpreter complains:

File "/Applications/V-REP_PRO_EDU_V3_2_0_Mac/programming/remoteApiBindings/python/python/vrep.py", line 929, in simxSetStringSignal
return c_SetStringSignal(clientID, signalName, signalValue, len(signalValue), operationMode)
ctypes.ArgumentError: argument 3: <type 'exceptions.TypeError'>: expected LP_c_ubyte instance instead of str


the following minimal python program will create this error:

Code: Select all

import vrep
if __name__ == '__main__':
    clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 500, 5)
    vrep.simxSetStringSignal(clientID, 'some signal name', 'some signal value', vrep.simx_opmode_oneshot)
changing it to POINTER(c_char) resolves this, but I'm not sure if this is a valid fix.

coppelia
Site Admin
Posts: 10366
Joined: 14 Dec 2012, 00:25

Re: Python Remote API: simxSetStringSignal

Post by coppelia »

If you want to use a string as signal value, do something like:

Code: Select all

import ctypes

str_bytes = 'some signal value'
raw_ubytes = (ctypes.c_ubyte * len(str_bytes)).from_buffer_copy(str_bytes)
vrep.simxSetStringSignal(clientID, 'some signal name', raw_ubytes, vrep.simx_opmode_oneshot)
With Python3 is would be like:

Code: Select all

import ctypes

str_bytes = b'some signal value'
raw_ubytes = (ctypes.c_ubyte * len(str_bytes)).from_buffer_copy(str_bytes)
vrep.simxSetStringSignal(clientID,b'some signal name', raw_ubytes, vrep.simx_opmode_oneshot)
Cheers

el_Raimundo

Re: Python Remote API: simxSetStringSignal

Post by el_Raimundo »

thanks, it executes fine now.
I was not aware that this conversion was neccessary.

Post Reply