Serial Port

Just starting out? Need help? Post your questions and find answers here.
Dano
User
User
Posts: 41
Joined: Fri Jul 16, 2004 4:20 am
Location: Edmonton, Ab, Canada

Re: Serial Port

Post by Dano »

Bernd,

Thanks again, sorry for the many questions, you have been very generous with your coding. I try to follow your code and I see how much I have yet to learn. I can sort of follow it but to write it myself
I have to work with it a lot more. It seems you are pushing the Send and Receive buttons with PostEvent, a relatively new command that I haven't seen and need to learn. I know you use it in the thread but I
guess you can set gadget and window events with it as well, which is nice. I will use your examples and try to learn from them.

Cheers,

Dan
Dano
User
User
Posts: 41
Joined: Fri Jul 16, 2004 4:20 am
Location: Edmonton, Ab, Canada

Re: Serial Port

Post by Dano »

I completed my code, I wanted the program to end by itself so at the window event #SendFinished and #ReceiveFinished I added PostEvent (#PB_Event_CloseWindow) after a delay. It works good and is a nice way to complete the ending of the program, as in event-driven programming it is not always cut and dried how to do things like that. PostEvent really helps. I was going to have a button with a countdown timer but decided not to get fancy this time. I don't think I will have any issues with the thread because it terminates the thread if it wasn't already in the #PB_Event_CloseWindow code before it exits. If that is not the correct way to exit a program please let me know as I don't want to create memory issues or threading issues. This way of programming has opened up a new way of doing things that is more elegant and more flexible. I look forward to coding more and have a few more projects in mind.

Dan
Mimo el Gato
New User
New User
Posts: 9
Joined: Fri May 16, 2014 2:40 am

Re: Serial Port

Post by Mimo el Gato »

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)
infratec
Always Here
Always Here
Posts: 7580
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Serial Port

Post by infratec »

Hi,

first of all, it is a bad idea to 'hijack' an old thread.
If someone search for a specific thing it will never find it and your problem has nothing todo with the handling of the serial port.

You problem is:

You don't check the Event.

Code: Select all

Repeat
  Event = WaitWindowEvent()
  Commande.s = "" ;initialise la chaine à envoyer
                  ;Retour.s = "" ;initialise la chaîne en entrée à nulle
  
  Select Event
    Case #PB_Event_Gadget
      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
    
  EndSelect
 
Until Event = #PB_Event_CloseWindow
Next bad thing:
Your gadgets overlaps, that's not allowed in PB.
Gadget 8 for example.

Bernd
Mimo el Gato
New User
New User
Posts: 9
Joined: Fri May 16, 2014 2:40 am

Re: Serial Port

Post by Mimo el Gato »

Good morning Bernd,
Thank You for your answer, it works.
Sorry for having "HiJacked the thread", but I did see this thread where apparently the people has a lot of experience, and not being that much experimented in PureBasic, I was thinking that the problem I met was related to my way of management of the serial port, so I searched in some thread about Serial ports, and I have also read in some forums that multiplying the threads is not good. I did not know it was forbidden to go on other's threads, sorry again.

Best regards
JFD
infratec
Always Here
Always Here
Posts: 7580
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Serial Port

Post by infratec »

Oh, it's not forbidden, but it's a bad idea.

Even for you, because some of the experts here has nothing todo with serial stuff and so they never look into this thread.
But if a thread has no reply up to now, than it's more intresting to look inside :mrgreen:

Bernd
Post Reply