简体   繁体   English

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

[英]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.我为 Arduino UNO 编写了一个游戏手柄串行驱动程序,因此,在 Arduino IDE 中坐标很好。 But in PySerial adding b' and \r\n' in the end.但是在 PySerial 最后添加 b' 和 \r\n' 。 I added replace() , but nothing changed... There is code: import pyautogui, sys import time import serial我添加了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

There is output:有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我解决了问题...发现了新问题...现在没有错误,只有空的,什么都没有...pyautogui想要工作:<我的系统: 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!!!我在 data = data[:-5] 处替换了 data = data.replace("\r\n'", "") 并且它可以工作!!!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何将Python对象保存到pymongo? (即我需要覆盖什么方法/返回值) - How do you save a Python object into pymongo? (i.e. what method/return value do I need to override) 将python可执行文件添加到HTML中,我需要做什么? - What do I need to do for adding python executable to HTML? 我需要对我的python代码做什么才能让它成为一个模块? - What do I need to do to my python code to get it to be a module? 什么是PasteDeploy,如果Python中的Eggs被认为已经消失,我是否需要学习它? - What is PasteDeploy and do I need to learn it if Eggs in Python are considered gone? 使用Python读取Microsoft Access数据库需要什么? - What do I need to read Microsoft Access databases using Python? 使用pyUSB在python中进行USB编程时需要什么依赖关系? - What dependencies do I need for USB programing in python with pyUSB? 什么是 Python 中的“线程本地存储”,为什么我需要它? - What is "thread local storage" in Python, and why do I need it? 我需要配置什么才能在浏览器上运行 Python? - What do I need to configure to run Python on a browser? anaconda python 需要什么路径,需要哪些环境变量? - What is the path, which environment variables do I need with anaconda python? 对于自动python部署,我需要了解/学习什么? - What do I need to know/learn for automated python deployment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM