maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Sending tuples via bluez? (https://talk.maemo.org/showthread.php?t=35323)

McLightning 2009-11-28 12:28

Sending tuples via bluez?
 
hi everybody
lately im working on an exciting project
but i got a problem about about sending two variables at same time via bluez module
therefore,i need to send these two variables as a tuple object , i guess

infact i found a way send two variables at same time as string but it's too laggy and sometimes causing errors
here is my solution which is so laggy:
on client:
def senderfunction():
socket.send(str(a)+' '+str(b))
on server:
a=int(incomingdata[0:3])
b=int(incomingdata[4:len(incomingdata)]

please help

McLightning 2009-11-28 12:31

Re: Sending tuples via bluez?
 
variable a and b are numbers between -99 and +99

mailwl 2009-11-28 13:11

Re: Sending tuples via bluez?
 
you need no 2*4 bytes to keep 2*1 bytes.
i don't know python, but in C it looks like:
char a, b;
short val = (a << 8) | b;
// send val via socket

--- restore back
short val; // received
char a =( val >>8 ) & 0xFF ,
b = val & 0xFF;

maibe it helps?

McLightning 2009-11-28 13:23

Re: Sending tuples via bluez?
 
thanks for reply but i dunno how can apply this to python :/
by the way welcome to our community

mailwl 2009-11-28 13:38

Re: Sending tuples via bluez?
 
it works in python for unsigned values:

a = 45
b = 51
val = (a<<8) | b
print val >> 8
print val & 0xFF

--
you my add 99 to both a, b before sending, and minus 99 after:

a = -45 + 99
b = -51 + 99
val = (a<<8) | b
print (val >> 8) - 99
print (val & 0xFF) - 99

to avoid negative values

McLightning 2009-11-28 13:40

Re: Sending tuples via bluez?
 
i tried this but it didn't work too
client:
socket.sendall('x'+str(a))
e32.ao_sleep(1)
socket.sendall('y'+str(b))
e32.ao_sleep(1) ##waits for a second

on server:
if incomingdata[0]=='x': a=int(incomingdata[1:len(incomingdata)])
if incomingdata[0]=='y': b=int(incomingdata[1:len(incomingdata)])

McLightning 2009-11-28 13:49

Re: Sending tuples via bluez?
 
Quote:

Originally Posted by mailwl (Post 397632)
it works in python for unsigned values:

a = 45
b = 51
val = (a<<8) | b
print val >> 8
print val & 0xFF

--
you my add 99 to both a, b before sending, and minus 99 after:

a = -45 + 99
b = -51 + 99
val = (a<<8) | b
print (val >> 8) - 99
print (val & 0xFF) - 99

to avoid negative values

thanx bro. i got it

ali1234 2009-11-29 10:52

Re: Sending tuples via bluez?
 
Consider using struct.pack and struct.unpack for this, it handles packing data into C style structs for you, and deals with endian issues transparently.

Possibly overkill in this case, but if you wanted to send more complex data it is very useful indeed.


All times are GMT. The time now is 05:47.

vBulletin® Version 3.8.8