Active Topics

 


Reply
Thread Tools
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#1
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
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#2
variable a and b are numbers between -99 and +99

Last edited by McLightning; 2009-11-28 at 12:34.
 
Posts: 8 | Thanked: 0 times | Joined on Nov 2009
#3
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?
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#4
thanks for reply but i dunno how can apply this to python :/
by the way welcome to our community
 
Posts: 8 | Thanked: 0 times | Joined on Nov 2009
#5
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

Last edited by mailwl; 2009-11-28 at 13:42.
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#6
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)])
 
Posts: 308 | Thanked: 62 times | Joined on Jun 2009 @ Turkiye
#7
Originally Posted by mailwl View Post
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
 
Posts: 15 | Thanked: 48 times | Joined on Nov 2009 @ United Kingdom
#8
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.
 
Reply


 
Forum Jump


All times are GMT. The time now is 15:40.