Python Forum

Full Version: Silly Sockets
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I feel like a heel making my first post a question but I really need help.  I'm new to python and sockets.

The purpose of this program is to read the values from a joystick (steering wheel) and send them on to a host.

When I comment out the lines connecting to the host and sending the value (lines 15,48 & 49) the script prints the value as it's supposed to.
However, when those lines aren't commented out, the length of msg (line 29) never goes above seven so the program just sits. It's as if the data is never fully read.

Wall

#!/usr/bin/python3

#
#   Script to read Logitech G27 Steering Wheel and send values to UDP host
#

import socket

msg = []
host = '192.168.0.185'
port = 5001
value = 0

mySocket = socket.socket()
mySocket.connect((host,port))

pipe = open('/dev/input/js0', 'r' , encoding = "ISO-8859-1")

def valueMap(x, in_min, in_max, out_min, out_max):
    return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min)
 
while 1:
    # For each character read from the /dev/input/js0 pipe...
    for char in pipe.read(1):
        # append the integer representation of the unicode character read to the msg list.
        msg += [ord(char)]
 
        # If the length of the msg list is 8...
        if len(msg) == 8:
            # Button event if 6th byte is 1
            if msg[6] == 1:
                if msg[4]  == 1:
                    print ('button', msg[7], 'down')
                else:
                    print ('button', msg[7], 'up')
            
            # Axis event if 6th byte is 2
            elif msg[6] == 2:
                print ('axis', msg[7], msg[5])
                if msg[5] == 0:
                    value = 226
                elif msg[5] < 128:
                    value = valueMap(msg[5], 1, 127, 227, 600)
                else:
                    value = valueMap(msg[5], 128, 255, 150, 224)
                print ('mapped value', value)

            mySocket.send(bytes(value))
            data = mySocket.recv(1024).decode()

            msg = []
Why don't you just try: to read(8) characters at once?
Yes, well, the difficulty of nubbing together a regurgitative purwell and a superaminative wennel-sprocket proved to be a stumbling block to further development until it was found that the use of anhydrous nagling pins enabled a kyptonastic boiling shim to be tankered.

You see now why I didn't just read all 8 at once? Hahaha.

I got focused on the wrong thing and another pair of eyes helped. Thank you.

Now onward and upward!