OK, reformulé pour être synchronisé sur un événement extérieur (réception RS232 au final mais clic gauche dans ce cas simple), j'obtiens ça :
Code : Tout sélectionner
; FileName "Delay&Display V2.pb"
EnableExplicit
Enumeration
#Win
#Timer
#Gadget_Display
#Gadget_Rx_LED
EndEnumeration
#LightGray = $F1F1F1
Define aCounter.a, aEchoPending.a
OpenWindow(#Win, #PB_Ignore, #PB_Ignore, 400, 260, "PureBasic Window", #PB_Window_SystemMenu)
StringGadget(#Gadget_Display, 10, 10, 310, 20, "")
TextGadget(#Gadget_Rx_LED, 10, 40, 12, 12, "", #PB_Text_Border)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_LeftClick
If aEchoPending
Debug "EchoPending !!!"
Else
aEchoPending = #True
SetGadgetColor(#Gadget_Rx_LED, #PB_Gadget_BackColor, #Red)
AddWindowTimer(#Win, #Timer, 1000)
EndIf
Case #PB_Event_Timer
If EventTimer() = #Timer
If aEchoPending
RemoveWindowTimer(#Win, #Timer)
Debug "After Delay"
aEchoPending = #False
SetGadgetText(#Gadget_Display, "Text " + aCounter)
Debug "New Value : " + aCounter
aCounter = aCounter + 1
SetGadgetColor(#Gadget_Rx_LED, #PB_Gadget_BackColor, #LightGray)
Else
Debug "MAJOR PROGRAMMING BUG !!!"
EndIf
EndIf
EndSelect
ForEver
End
Bizarrement, "
Repeat ... WindowEvent() ... Forever" n'utilise que 2 à 4% du CPU. J'aurais cru plus... Bien sûr, "WaitWindowEvent()" le fait chuter à 0.x% (selon Process Explorer)
Par ailleurs, je suis en train d'essayer de créer un Event sur réception RS232 avec ça : (je suis parti de "SerialPort.pb" dans les exemples PB et je l'ai mixé avec l'aide de "PostEvent()
"), la coloration syntaxique en plus...
Code : Tout sélectionner
; FileName SerialPortWithThread.pb
EnableExplicit
Macro HexFormat(Prefix, Value, Size)
Prefix + RSet(Hex(Value), Size, "0")
EndMacro
Enumeration Windows
#MainWindow
EndEnumeration
Enumeration Gadgets
#Gadget_Display
; #ComPortGadget
EndEnumeration
Enumeration #PB_Event_FirstCustomValue ; to not clash with internal events
#EventSerialPortRx
EndEnumeration
; Enumeration #PB_EventType_FirstCustomValue ; to not clash with internal events ; <--- une tentative avortée
; #EventTypeSerialPortRx
; EndEnumeration
Procedure ComPortThread(comID)
Protected RxDataLength
Debug "ComPortThread : RxDataLength Size = " + SizeOf(RxDataLength) + " Bytes"
Debug "ComPortThread : comID Size = " + SizeOf(comID) + " Bytes"
Debug "ComPortThread : comID = " + comID
Repeat
Delay(1) ; <--- pour tester la charge CPU
RxDataLength = AvailableSerialPortInput(comID)
Until RxDataLength
; ici, tentative de réception de la String mais sans intérêt...
PostEvent(#EventSerialPortRx) ; , #MainWindow, #ComPortGadget, #EventTypeSerialPortRx, ; <--- faisons simple
EndProcedure
Define Event, sPort.s, ComID, sRxData.s, RxDataLength, aInputBuffer.a
sPort = "COM11" ; com0com virtual port pair is COM10-COM11
ComID = OpenSerialPort(#PB_Any, sPort, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_RtsCtsHandshake, 1024, 1024)
Debug "ComID = " + SizeOf(ComID) + " Bytes"
Debug "Main : comID = " + comID
If Not(ComID)
MessageRequester("Error", "Can't open the serial port: "+sPort)
Else
If OpenWindow(#MainWindow, #PB_Ignore, #PB_Ignore, 400, 260, "PureBasic Window", #PB_Window_SystemMenu)
StringGadget(#Gadget_Display, 10, 10, 310, 20, "")
CreateThread(@ComPortThread(), comID)
Repeat
Event = WaitWindowEvent()
Select Event
Case #EventSerialPortRx
Debug "Le Thread commence une action... "
sRxData = ""
Debug "RxDataLength = " + AvailableSerialPortInput(comID)
Repeat
ReadSerialPortData(comID, @aInputBuffer, 1) ; <--- cette façon de faire n'est pas forcément définitive...
sRxData = sRxData + HexFormat("", aInputBuffer, 2) + " "
Until AvailableSerialPortInput(comID) = 0
SetGadgetText(#Gadget_Display, sRxData)
CreateThread(@ComPortThread(), comID)
EndSelect
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
End
mais j'ai pour l'instant un bug sur l'identité du port dans le Thread.
Nouvel essai, ça semble fonctionner. J'ai remplacé "
OpenSerialPort(0, sPort..." par "
OpenSerialPort(#PB_Any, sPort..." comme listé plus haut. (Un gars, un jour, m'avait dit de me méfier des ID numériques... et j'ajouterais "des copier-coller")
On progresse...