简体   繁体   中英

Python method not working what i need to do?

I writing a gamepad serial-driver for Arduino UNO, so, in Arduino IDE coord resives good. But in PySerial adding b' and \r\n' in the end. I added replace() , but nothing changed... There is code: import pyautogui, sys import time import serial

ArduinoSerial=serial.Serial('/dev/ttyUSB0',115200)  #Specify the correct COM port
time.sleep(1)                             #delay of 1 second
a = 1
while a:
    ArduinoSerial.readline()
    print(ArduinoSerial.readline());

    data=str(ArduinoSerial.readline())
    data = data.replace("\r\n", "")
    print(data)
    (x,y)=data.split(":")           # read the x and y axis data
    (X,Y)=pyautogui.position()        #read the current cursor's position
    x=int(x)
    y=int(y)
    pyautogui.moveTo(X+x,Y-y)           #move cursor to desired position

There is output:

ilyabot@ilyabot-HP-Pavilion-dv6-Notebook-PC:~/Рабочий стол$ python3 joy_driver.py 
b'0:0\r\n'
b'0:0\r\n'
Traceback (most recent call last):
  File "joy_driver.py", line 21, in <module>
    x=int(x)
ValueError: invalid literal for int() with base 10: "b'0"

I fixed problem... And found new... There is no error now, there is only empty and nothing ... pyautogui want to work:< My System: Xubuntu 18.04 x86_64 Python: Python 3.6.9

I fixed it!

data = data[:-5]

I replaced data = data.replace("\r\n'", "") at data = data[:-5] and IT WORKS!!!

There is all code:

# Importing modules
import pyautogui, sys
import time 
import serial
# Arduino Serial-Joystick Driver
SerialConnection=serial.Serial('/dev/ttyUSB0',115200)  # Specify the your COM port
while 1:
    data = str(SerialConnection.readline()) # Read the x and y axis data
    data = data.replace("b'", "") # Eraing "b'"
    data = data[:-5] # Easing "\r\n'"
    (x,y)=data.split(":") # Adding ":" split
    print(data) # Logging
    (X,Y)=pyautogui.position()        # Read the current cursor's position
    x=int(x) # Converting str(x) to int(x)
    y=int(y) # Converting str(y) to int(y)
    pyautogui.moveTo(X-x,Y+y) # Move cursor to desired position

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM