簡體   English   中英

Python 方法不起作用我需要做什么?

[英]Python method not working what i need to do?

我為 Arduino UNO 編寫了一個游戲手柄串行驅動程序,因此,在 Arduino IDE 中坐標很好。 但是在 PySerial 最后添加 b' 和 \r\n' 。 我添加了replace() ,但沒有任何改變...有代碼: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

有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"

我解決了問題...發現了新問題...現在沒有錯誤,只有空的,什么都沒有...pyautogui想要工作:<我的系統: Xubuntu 18.04 x86_64 Python: Python 3.6.9

我修好了它!

data = data[:-5]

我在 data = data[:-5] 處替換了 data = data.replace("\r\n'", "") 並且它可以工作!!!

有所有代碼:

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM