Page 1 of 1

reset a string ??

Posted: Sun Mar 29, 2020 6:03 pm
by Walko
Hello,

Sorry, Im writing my very first program.
Purebasic seems to be perfect for this task but my knowledge is very limited :oops:

The idea is to create a GUI to communicate with a device connected by a serial port.
I found severals examples on this forum from infratec which I thank very much, it really helps me....but i don't understand everything.

I need to receive string on the serial port like this one : $GPVTG,100,200,300,400 #LF
and display each field in a text box and it works pretty well but I'm not sure I did it right, or that I positioned the functions correctly.

Code: Select all

Procedure ComThread(*Parameter.ThreadParameterStructure)
 
  Protected Byte.a, ReceivedBytes.i
  Protected Text$
 
  PostEvent(#ComThread_Started)
 
  *Parameter\Com = OpenSerialPort(#PB_Any, *Parameter\Com$, 115200, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)
  If *Parameter\Com
   
    WriteSerialPortString(*Parameter\Com, "hello")
   
    Repeat
      If AvailableSerialPortInput(*Parameter\Com)
        If ReadSerialPortData(*Parameter\Com, @Byte, 1) = 1
          ReceivedBytes + 1
          SetGadgetText(#Rx , Chr(Byte)) 
          
          If Byte <> #LF
            Text$ + Chr(Byte)
            SetGadgetText(#RxString , Text$)
          Else 
            SetGadgetText(#RxField1 , StringField(Text$ , 1 , ","))
            SetGadgetText(#RxField2 , StringField(Text$ , 2 , ",")) 
          EndIf
            
         PostEvent(#ComThread_ByteReceived, 0, 0, 0, ReceivedBytes)
          If Byte = #CR
            PostEvent(#ComThread_LineReceived)
            WaitSemaphore(*Parameter\Semaphore)
          Else
            *Parameter\Receive$ + Chr(Byte)
          EndIf
        EndIf
      Else
        Delay(10)
      EndIf
    Until *Parameter\Exit
   
    CloseSerialPort(*Parameter\Com)
   
  EndIf
 
  PostEvent(#ComThread_Finished)
 
EndProcedure
What i would like, is to reset the string Text& after filling all the text boxes.
I want the gadget text #RxString to display only the last received string.
How can I do that ? ( I am not sure to be clear )

Thank you for your help :)

Re: reset a string ??

Posted: Sun Mar 29, 2020 8:19 pm
by infratec
You will running into a problem: setting the text inside of the frame.
You should do this in the main eventloop.
For this is the PostEvent.

To clear it:

Code: Select all

If Byte = #CR
  PostEvent(#ComThread_LineReceived)
  WaitSemaphore(*Parameter\Semaphore)
  *Parameter\Receive$ = ""
Else
 *Parameter\Receive$ + Chr(Byte)
EndIf

Re: reset a string ??

Posted: Mon Mar 30, 2020 3:42 pm
by Walko
infratec wrote:You will running into a problem: setting the text inside of the frame.
You should do this in the main eventloop.
For this is the PostEvent.

To clear it:

Code: Select all

If Byte = #CR
  PostEvent(#ComThread_LineReceived)
  WaitSemaphore(*Parameter\Semaphore)
  *Parameter\Receive$ = ""
Else
 *Parameter\Receive$ + Chr(Byte)
EndIf
Hello,

Thank you infratec. I have so much to learn.
I moved the GUI update into the loop

I have to learn about pointer to really understand your code :lol:

Re: reset a string ??

Posted: Mon Mar 30, 2020 4:05 pm
by mk-soft
With a small trick can send a string to GUI ...

Show Example 3 from Mini Thread Control