reset a string ??

Just starting out? Need help? Post your questions and find answers here.
Walko
User
User
Posts: 11
Joined: Thu Mar 26, 2020 6:08 pm

reset a string ??

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

Re: reset a string ??

Post 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
Walko
User
User
Posts: 11
Joined: Thu Mar 26, 2020 6:08 pm

Re: reset a string ??

Post 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:
User avatar
mk-soft
Always Here
Always Here
Posts: 6252
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: reset a string ??

Post by mk-soft »

With a small trick can send a string to GUI ...

Show Example 3 from Mini Thread Control
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply