![](/img/trans.png)
[英]Is it possible to convert a really large int to a string quickly in python
[英]Need to convert strint to int quickly python
我有一個程序應該從 arduino(通過串行)獲取操縱桿 position 讀數,並將它們轉換為我計算機上的鼠標移動。
這個問題只有一個...
到 integer 的字符串轉換太慢了,而且動作要花很長時間才能注冊。
我需要一種更快的方法將字符串轉換為 integer 值,或者完全跳過轉換的方法。
這是我當前的代碼:
import serial
import pyautogui
import time
ser = serial.Serial('COM3', 9600, timeout=1)
while True:
time.sleep(0.0001)
ser_bytes = ser.readline()
decoded_bytes = ser_bytes[0:len(ser_bytes)-2].decode("utf-8")
pos = decoded_bytes.split(':')
xpos = int(pos[0])
ypos = int(pos[1])
print("x:", xpos, " y:", ypos)
pyautogui.move(xpos, ypos)
注意:Output 從 arduino 有3 個值:0:0:0 第一個數字:x 第二個數字:y 第三個數字:操縱桿按鈕
也許這樣的事情會奏效? 這樣,每次調用move()
時,您都可以讀取多行輸入。 某些輸入行將被忽略,但如果您輸入的速度比使用它的速度快,這似乎是必要的。
import serial
import pyautogui
import time
ser = serial.Serial('COM3', 9600, timeout=1)
while True:
time_to_move = time.time() + 0.001
while True:
ser_bytes = ser.readline()
if time.time() >= time_to_move:
break
x_bytes, y_bytes = ser_bytes[:-2].split(b':')
x, y = int(x_bytes), int(y_bytes)
pyautogui.move(x, y)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.