Serial

Hi ,
I have use the below codes in my timer action. I have used the “App.SleepCurrentThread(2000)” for sleep the current thread , Because Serial port need to some time to respond the reply message to Xojo. Everything was fine… But my problem is , Because of use above code , the total application was not working at the time.
I want the particular loop only sleep.

For Glb_Step = 0 to 3

If Glb_Step = 0 Then
  a = "AT"+ EndOfLine
  AddMessage ("<" + a)
  Serial1.Write(a)
  App.SleepCurrentThread(2000)
  Glb_Data = Serial1.ReadAll()  
  TxtArea_Reply.AppendText(">"+Glb_Data)
 
  
ElseIf Glb_Step = 1 Then
  b = "AT+GCI?"+ EndOfLine
  AddMessage ("<" + b)
  Serial1.Write(b)
  App.SleepCurrentThread(2000)
  Glb_Data = Serial1.ReadAll()
  TxtArea_Reply.AppendText(">"+Glb_Data)

ElseIf Glb_Step = 2 Then
  c = "AT+COPS"+ EndOfLine
  AddMessage ("<" + c)
  Serial1.Write(c)
  App.SleepCurrentThread(2000)
  Glb_Data = Serial1.ReadAll()
  TxtArea_Reply.AppendText(">"+Glb_Data)
  
  
ElseIf Glb_Step = 3 Then
  d = "AT+CSQ"+ EndOfLine
  AddMessage ("<" + d)
  Serial1.Write(d)
  App.SleepCurrentThread(2000)
  Glb_Data = Serial1.ReadAll()
  TxtArea_Reply.AppendText(">"+Glb_Data)
  
End
Timer1.Enabled = False

Next

Help me out from this…

I think it’s better to use the DataAvailable event of the Serial object. Make Glb_Step a property instead of a local variable. In a button (or wherever you initiate this action) put

Glb_Step = 0
a = "AT"+ EndOfLine
AddMessage ("<" + a)
Serial1.Write(a)

In the DataAvailable event put the rest of your code.

Glb_Data = Serial1.ReadAll() 
TxtArea_Reply.AppendText(">"+Glb_Data)

Glb_Step = Glb_Step + 1
If Glb_Step = 1 Then
   b = "AT+GCI?"+ EndOfLine
   AddMessage ("<" + b)
   Serial1.Write(b)

ElseIf Glb_Step = 2 Then
   c = "AT+COPS"+ EndOfLine
   AddMessage ("<" + c)
   Serial1.Write(c)

ElseIf Glb_Step = 3 Then
   d = "AT+CSQ"+ EndOfLine
   AddMessage ("<" + d)
   Serial1.Write(d)

End If

Thanks TIm… Its working fine Now…

It will be great in future , If u advice how to make sleep the current event.

That is usually not the best solution. You should try to use event oriented programming instead of procedural, linear programming. And you should never sleep the main (UI) thread, which is where events are processed. You can sleep Thread objects that you create which run in parallel to the main thread, using Thread.Sleep or App.SleepCurrentThread. If the code in an event is going to take a long time (more than 0.5 seconds), you should put that code into a thread to keep the UI responsive. Otherwise, the user will think your app has hung (so will the OS, and it might kill your app if it hangs too long).

Got It… Thanks Tim.