0
私はPygameを使用して、直交エンコーダカウンタ用のディスプレイを作成しています。私はEZTextを使用してディスプレイ上のユーザー入力を促し、最大カウントを変更し、UART経由で実際のカウントを担当するマイクロコントローラに更新値を送信します。私はEZTextプロンプト使用して値を入力すると、入力された値がに正しく保存されラズベリーパイは同じデータを送信していますか?
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)
:ここに私のコードです(長さのため申し訳ありませんが、私は底部に向かって、ユーザー入力を扱う部分をマーク) newMax変数。私が初めてマイコンにデータを送るとき、私は正しい値を得ます。その後のRasPiの入力では、newMax変数は正しく調整されますが、最初のときと同じ値がマイクロコントローラに送信されます。 ser.write()を間違って使用していますか?
申し訳ありません、私は自分の投稿を編集しました。私はもう少し(あなたの推薦を含む)診断し、txtbx.valueが正しく更新されているように見えますが、送信されるシリアルデータが間違っています。 – cjswish