Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Silly Sockets
#1
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 = []
Reply
#2
Why don't you just try: to read(8) characters at once?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sockets interferring with USB ? epif18 0 2,604 Mar-19-2021, 07:45 PM
Last Post: epif18
  Just learning sockets etc... floatingshed 2 2,294 May-06-2020, 09:37 PM
Last Post: floatingshed
  Quick sockets question... ptrivino 2 2,175 Sep-26-2019, 08:51 PM
Last Post: ptrivino
  Sockets and Sendmail taintedsushi 2 2,304 Sep-02-2019, 12:51 PM
Last Post: venquessa
  Script Conversion 2.7 to 3 (sockets) Pumpernickel 1 2,514 Apr-10-2019, 04:26 PM
Last Post: Pumpernickel
  What sort of things can I write to learn about sockets marienbad 2 2,664 Oct-16-2018, 04:02 PM
Last Post: buran
  unix domain sockets Skaperen 8 4,844 Sep-02-2018, 07:02 PM
Last Post: Skaperen
  file transfer with sockets sinister88 1 6,417 Nov-11-2017, 03:29 PM
Last Post: heiner55
  Trouble with sending raw sockets IronReign 2 4,170 Jun-01-2017, 08:26 AM
Last Post: camp0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020