2016-04-26 37 views
0

所以我使用Pygame爲正交編碼器計數器創建一個顯示。我正在使用EZText在顯示屏上提示用戶輸入,在那裏我可以更改最大計數並通過UART將更新值發送給負責進行實際計數的微控制器。這裏是我的代碼(遺憾的長度,我標誌着與用戶輸入朝下方交易的一部分):Raspberry Pi發送相同的數據?

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()?

回答

0

你不能只是做txtbx.value = ""

+0

對不起,我剛剛編輯我的帖子。我做了一些更多的診斷(包括你的建議),看起來txtbx.value被正確更新,但是發送的串行數據不正確。 – cjswish