Good evening,
I am meeting the following problem with a connection to an Arduino uno (the same comes with another MCU board based on 8051, but it appears only with PureBasic). I have catched this program on a german site, and I am working on it to understand fully the serial comms with PureBasic.
I want to send simple orders to an MCU, in the occurrence "F" for forward, "B" for backwards, "R" for Right and "L" for Left and "S" for stop. The MCU receives the orders, execute and acknowledges with a clear text corresponding, this works. BUT When I start the program, the first order and answer set ar repeated twice, whichever order is sent. This defect does not appear after, it comes only at the first order transmission.
I join the program I am trying to understand:
Code: Select all
Index$ = "Ce programme permet de tester la communication avec un microcontrôleur via une liaison RS232. Le µC doit lire l'ordre envoyé et retourner un message. les paramètres de communication sont fixés à 9600-8-N-1. Attention: Un CR est envoyé mais pas de LF!. L'ordre envoyé peut être constitué d'une ou plusieurs lettres majuscules ou minuscules, de chiffres ou une combinaison des deux comme par exemple 12AaB3"
Index2$ = "Envoi de l'ordre au µC"
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
PortDefaut$ = "COM1"
CompilerElse
PortDefaut$ = "/dev/ttyACM0"
CompilerEndIf
OpenSerialPort(0, PortDefaut$, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
OpenWindow(0,0,0,462,620,"Test d'envoi d'ordres à un µC par ligne RS232",#PB_Window_SystemMenu | #PB_Window_ScreenCentered |#PB_Window_TitleBar)
EditorGadget(0,8,500,200,100,#PB_String_ReadOnly) ;Place where appears the order sent
TextGadget(1,8,180,200,30,"Send order")
ButtonGadget(2,8,220,200,30,"Send order 1")
ButtonGadget(3,8,270,200,30,"Send order 2")
ButtonGadget(4,8,320,200,30,"Send order 3")
ButtonGadget(5,8,370,200,30,"Send order 4")
ButtonGadget(6,8,420,200,30,"Send order 5")
TextGadget(7,8,10,450,150,Index$)
TextGadget(8,250,180,150,100,Index2$)
EditorGadget(9,250,220,200,30)
SetGadgetText(9,"F") ;Order for "FORWARD", Answer should be "FORWARD"
EditorGadget(10,250,270,200,30)
SetGadgetText(10,"B") ;Order for "BACKWARD", Answer should be "BACKWARD"
EditorGadget(11,250,320,200,30)
SetGadgetText(11,"L") ;Order for "LEFT", Answer should be "LEFT"
EditorGadget(12,250,370,200,30)
SetGadgetText(12,"R") ;Order for "RIGHT", Answer should be "RIGHT"
EditorGadget(13,250,420,200,30)
SetGadgetText(13,"S") ;Order for "STOP", Answer should be "STOPPED"
EditorGadget(14,250,500,200,100,#PB_String_ReadOnly) ;Place where appears the MCU answer
TextGadget(15,8,480,200,20,"Envoi")
TextGadget(16,250,480,200,20,"Retour")
Repeat
Event = WaitWindowEvent()
Commande.s = "" ;initialise la chaine à envoyer
;Retour.s = "" ;initialise la chaîne en entrée à nulle
Select EventGadget()
Case 2 : Commande.s = GetGadgetText(9)
Gosub Envoi
Case 3 : Commande.s = GetGadgetText(10)
Gosub Envoi
Case 4 : Commande.s = GetGadgetText(11)
Gosub Envoi
Case 5 : Commande.s = GetGadgetText(12)
Gosub Envoi
Case 6 : Commande.s = GetGadgetText(13)
Gosub Envoi
EndSelect
Until Event = #PB_Event_CloseWindow
CloseSerialPort(0)
End
;Send the order and wait for the answer message
Envoi:
;ClearGadgetItems(0)
ClearGadgetItems(14)
WriteSerialPortString(0,Commande.s)
Delay(100)
AddGadgetItem(0,-1,Commande.s) ; Displays the transmitted order
While AvailableSerialPortInput(0)>0
If ReadSerialPortData(0,@Byte,1)
Retour.s = Retour.s + Chr(Byte)
EndIf
Wend
;Retour.s = Left(Retour.s,Len(Retour.s)-2)
AddGadgetItem(14,-1,Retour.s) ; Displays the answer message
Return
I would highly appreciate to understand what I am missing, The program works fine, but the only bug I see is that the very first order I send is duplicated, either its answer. From the 2nd order on, I click, all works normally. I can see on the arduino serial leds that the 1st order is allways received and answered twice.
This works fine with other terminal programs (GTKterm etc).
Thanx for your informations
Best regards
PS: I work on Linux Mint Cinnamon 17 64 bits, the compiler is PB5.24b1 (but it was the same on 5.22)