首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当写入串口时,速度大大降低。

当写入串口时,速度大大降低。
EN

Stack Overflow用户
提问于 2022-06-15 19:38:55
回答 1查看 128关注 0票数 2

使用游戏从控制器读取信息,但当我添加arduinoData.write(str.encode(ALL_DATA))时,它大大降低了速度,我能做什么呢?

这条线几乎是在底部加的。这是线的位置;它肯定是工作,但没有发送更新的速度,如果我不会写到串行。

代码语言:javascript
复制
import sys
import serial
import pygame
import time

from pygame.locals import *
pygame.init()

A0_value = '0'
A1_value = '0'
A2_value = '0'
A3_value = '0'
A4_value = '0'
A5_value = '0'
A6_value = '0'
B2 = '0'
B3 = '0'
B4 = '0'
B5 = '0'
global axis_data 

## 
arduinoData = serial.Serial('COM8',9600)

##

def main():
    global axis_data , button_data
    screen = pygame.display.set_mode((500, 700))
    pygame.display.set_caption("Joystick example")

    clock = pygame.time.Clock()
    joysticks = {}

    while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True  # Flag that we are done so we exit this loop.

                if event.type == pygame.JOYBUTTONDOWN:
                    print("Joystick button pressed.")
                    if event.button == 0:
                        joystick = joysticks[event.instance_id]
                        if joystick.rumble(0, 0.7, 500):
                            print(
                                "Rumble effect played on joystick {}".format(
                                    event.instance_id
                                )
                            )

                if event.type == pygame.JOYBUTTONUP:
                    print("Joystick button released.")

                # Handle hotplugging
                if event.type == pygame.JOYDEVICEADDED:
                    # This event will be generated when the program starts for every
                    # joystick, filling up the list without needing to create them manually.
                    joy = pygame.joystick.Joystick(event.device_index)
                    joysticks[joy.get_instance_id()] = joy
                    print("Joystick {} connencted".format(joy.get_instance_id()))

                if event.type == pygame.JOYDEVICEREMOVED:
                    del joysticks[event.instance_id]
                    print("Joystick {} disconnected".format(event.instance_id))




        for joystick in joysticks.values():
            axes = joystick.get_numaxes()
            #print("Number of axes: {}".format(axes))
            axis_str = [''] * axes
            button_data = ""
            axis_data = ""



            for i in range(axes):
                axis = joystick.get_axis(i)
                axis_int = int(axis*100)
                axis_str[i] = str(axis_int)
                axis_data = axis_str[i] + "," + axis_data 
                #print("Axis {} value: {}".format(i, axis)) 
            
            #print(axis_data)
            buttons = joystick.get_numbuttons()
            #print("Number of buttons: {}".format(buttons))
            button_str = ['0'] * buttons

            for i in range(buttons):
                button = joystick.get_button(i)
                button_str[i] = str(button)
                #print("Button {} value: {}".format(i, button))
                button_data = button_str[i] + "," + button_data
            
            #print(button_data)
            ALL_DATA = axis_data + button_data
            ALL_DATA = ALL_DATA[:-1] + "!"
            print(ALL_DATA)
        arduinoData.write(str.encode(ALL_DATA))

        
            
       # pygame.display.update()
        clock.tick(60)


if __name__ == "__main__":
    main()
    # If you forget this line, the program will 'hang'
    # on exit if running from IDLE.
    pygame.quit()
EN

回答 1

Stack Overflow用户

发布于 2022-06-16 01:59:18

写到9600波特的连载真的很慢。参考这个答案上的波特率发送时间,发送每个字母略多于1毫秒(大致).请注意,在60 FPS时,允许每个帧在17毫秒左右。代码将这些数据写成一个字符串,所以很可能每一个循环都需要几毫秒的时间。

一个真正容易的胜利是简单地使用一个更快的波特率。你至少可以试着用230400加速24倍。

而且,我们几乎从不将实时数据作为字符串发送。您可以使用Python将所有不同的操纵杆读数打包到二进制数据中,这将更加紧凑。特别是当你将按钮状态视为单个位,而不需要高分辨率的操纵杆位置。

最后,您的代码可以使用线程执行串行通信,并与主PyGame循环并行编写。这在"IO绑定“操作上工作得很好。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72636988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档