Voici un programme que j'essaye de faire pour envoyer un message via le port COM1 à un µControlleur. Voici le bug :
Une fois le programme lancé, tapez "Bonjour !" ou ce que vous voulez, puis amusez-vous avec les touches directionnelles : des caractères se tapent tout seul !!!
Voila le code source :
Code : Tout sélectionner
InitSprite() : InitKeyboard()
LoadFont(0, "Arial", 20, #PB_Font_HighQuality)
Global x = 0
Global y = 0
Global blackit
Global Dim char(20, 2)
Procedure drawBG()
Box(0, 0, 600, 200, $A7C6A1)
For b = 0 To 19
Box(30 * b + 2, 2, 28, 38, $9BBE93)
Box(30 * b + 2, 44, 28, 38, $9BBE93)
Next
EndProcedure
Procedure drawTchar()
DrawingFont(FontID(0))
For u = 0 To 19
For v = 0 To 1
DrawText(30 * u + 2 + Int((28 - TextWidth(Chr(char(u, v))))/2), 42 * v + 40 - TextHeight(Chr(char(u, v))), Chr(char(u, v)), 0, $9BBE93)
Next
Next
EndProcedure
Procedure blackIT(delay)
Repeat
blackit = ~blackit
Delay(delay)
ForEver
EndProcedure
Procedure caClignoooooote(delay)
CreateThread(@blackIT(), delay)
Repeat
FlipBuffers()
StartDrawing(ScreenOutput())
drawBG()
drawTchar()
If blackit : Box(30 * x + 2, 42 * y + 2, 28, 38, 0) : EndIf
StopDrawing()
ForEver
EndProcedure
Procedure sendData()
dat$ = ""
For l = 0 To 1
For c = 0 To 19
dat$ = dat$ + Chr(char(c, l))
Next
dat$ = dat$ + Chr(10)
Next
WriteSerialPortString(0, dat$, #PB_Ascii)
EndProcedure
lcdStart.s = " LCD Writer v:1.0.1 " + Chr(10) + "********************"
OpenSerialPort(0, "COM1", 110, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 0, 40)
WriteSerialPortString(0, lcdStart, #PB_Ascii)
CreateImage(0, 600, 84)
OpenWindow(0, 10, 10, 600, 84, "LCD Writer", #PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 600, 84, 1, 0, 0)
CreateThread(@caClignoooooote(), 500)
Repeat
event = WaitWindowEvent(10)
If GetAsyncKeyState_(#VK_RETURN)
SetWindowTitle(0, "TRANSFERT...")
sendData()
SetWindowTitle(0, "LCD Writer")
EndIf
ExamineKeyboard()
key$ = KeyboardInkey()
If key$
Debug key$
char(x, y) = Asc(key$)
If x = 19
y = (y + 1) % 2
EndIf
x = (x + 1) % 20
EndIf
If GetAsyncKeyState_(#VK_BACK)
If Not temoinB Or ElapsedMilliseconds() - ctrB >= 250
char(x, y) = 0
x = x - 1
If x = -1 : x = 19 : y = (y + 1) % 2 : EndIf
temoinB = 1
ctrB = ElapsedMilliseconds()
EndIf
Else
temoinB = 0
EndIf
If GetAsyncKeyState_(#VK_F1) And (GetAsyncKeyState_(#VK_LCONTROL) Or GetAsyncKeyState_(#VK_RCONTROL))
If Not temoinLIGHT
light.s = Chr(7)
WriteSerialPortString(0, light, #PB_Ascii)
temoinLIGHT = 1
EndIf
Else
temoinLIGHT = 0
EndIf
If GetAsyncKeyState_(#VK_RIGHT)
If Not temoinL Or ElapsedMilliseconds() - ctrR >= 250
x = (x + 1) % 20
blackit = -1
temoinL = 1
ctrR = ElapsedMilliseconds()
EndIf
Else
temoinL = 0
EndIf
If GetAsyncKeyState_(#VK_LEFT)
If Not temoinR Or ElapsedMilliseconds() - ctrL >= 250
x = x - 1
If x = -1 : x = 19 : EndIf
blackit = -1
temoinR = 1
ctrL = ElapsedMilliseconds()
EndIf
Else
temoinR = 0
EndIf
If GetAsyncKeyState_(#VK_UP) Or GetAsyncKeyState_(#VK_DOWN)
If Not temoinUD Or ElapsedMilliseconds() - ctrUD >= 250
y = (y + 1) % 2
blackit = -1
temoinUD = 1
ctrUD = ElapsedMilliseconds()
EndIf
Else
temoinUD = 0
EndIf
Until event = #PB_Event_CloseWindow
; IDE Options = PureBasic 4.30 (Windows - x86)
; CursorPosition = 81
; FirstLine = 67
; Folding = -
; EnableXP
Kris