Raspberry Pi发送相同的数据?

时间:2016-04-26 18:57:01

标签: python serial-port raspberry-pi pygame uart

所以我使用Pygame为正交编码器计数器创建一个显示器。我正在使用EZText在显示屏上提示用户输入,我可以通过UART更改最大计数并通过UART将更新值发送到负责进行实际计数的微控制器。这是我的代码(抱歉长度,我标记了处理用户输入底部的部分):

import sys, serial, string, pygame, eztext
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS

windowWidth = 1680
windowHeight = 1050
maxCount New = 32767
posDataStr = " "
rect1 = (1000,350,290,75)
rect2 = (1000,555,290,75)

black = (0,0,0)
white = (248,248,255_

pygame.init()
infoObject = pygame.display.Info()
surface = pygame.display.set_mode((windowWidth,windowHeight), pygame.FULLSCREEN)
background = pygame.image.load("/home/pi/MyProjects/background.png")
myFont = pygame.font.SysFont("monospace",96,1)
surface.blit(background, (0,0))
pygame.mouse.set_visible(0)    #hide mouse pointer

def quitGame():
    pygame.quit()
    sys.exit()

DEVICE = "/dev/ttyAMA0"
ser = serial.Serial(DEVICE, 19200)

#draw Max Count rectangles
pygame.draw.rect(surface, (black), (995,550,300,85))
pygame.draw.rect(surface, (white), (1000,555,290,75))

# draw current count background (black) rectangle (since it doesn't need to update)
pygame.draw.rect(surface, (black), (995,345,300,85))

#draw maxCountNew as text
maxCountText = myFont.render(str(maxCountNew), 1, (black))
surface.blit(maxCountText, (1000,545))

pygame.display.update()

while True:

    posData = [0,0,0,0,0,0,0]

    i = ser.read()
    if i == '*':
        for x in range (0,6):
            posData[x] = ser.read()
            if posData[x] == ',' or posData[x] == '\00'
                del posData[x:]
                posDataStr = ''.join(posData)
                break

    #draw posDataStr rectangles
    pygame.draw.rect(surface, (white), (1000,350,290,75))

    #draw currentCountStr as text
    currentCountText = myFont.render(posDataStr,1,(black))
    surface.blit(currentCountText, (1000,340))

    for event in GAME_EVENTS.get():

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_ESCAPE:
                quitGame()

###################################################################################################
#THIS IS THE PROBLEMATIC SECTION (I think)


            if event.key == pygame.K_r:
                txtbx = eztext.Input(maxlength=5,color=black,prompt='Enter New Resolution: ')

                while True:

                    events = pygame.event.get()

                    #blit txtbx on the screen
                    txtbx.draw(surface)

                    #update txtbx
                    txtbx.update(events)

                    #refresh the display
                    pygame.display.flip()

                    if event.key == pygame.K_SPACE:
                        break
                    if len(txtbx.value) > 4:  #once 5 digits are entered
                        break

                newMax = txtbx.value

                #write new position to MCU
                ser.write(newMax.encode())
                print newMax.encode()      //for diagnostic purposes

                #max count is resolution - 1
                maxCountNewInt = int(newMax) - 1

                #convert back to a string
                maxCountNew = str(maxCountNewInt)

                #refresh the display
                surface.blit(background, (0,0))
                pygame.draw.rect(surface,(white),(1000,555,290,75))
                maxCountText = myFont.render(maxCountNew, 1, (black)) #display new max count txt
                surface.blit(maxCountText, (1000,545))
                pygame.display.update()

    pygame.display.update(rect1)
    pygame.display.update(rect2)

当我使用EZText提示输入值时,输入的值将正确保存到newMax变量中。我第一次将数据发送到微控制器时,得到了正确的值。在RasPi上的后续输入上,newMax变量被正确调整,但与第一次相同的值被发送到微控制器。我是否错误地使用了ser.write()?

1 个答案:

答案 0 :(得分:0)

你不能只做txtbx.value = ""吗?