serout, serin, sertxd help

tracecom

Senior Member
I am using an 08M2 and serout to send AT commands to another device. My hope is to be able to send an AT command from Pin C.4 to the external device, and then use serin on Pin C.2 to capture the OK response from the external device. I have sertxd setup to send the contents of the two registers to terminal. Here's the code. (baud = T2400_4)

Code:
serout C.4,baud,("AT",13,10) 'Send attention command+CR+LF to ESP-01.
serin [1000],C.2,baud,("AT"),b14,b15 'Discard AT and store OK at b14 and b15.
sertxd(b14,b15,13,10) 'Sends contents of b14,b15 to terminal, followed by CR,LF)
In addition, I have a separate USB UART monitoring Pin C.2, and I can see the AT command and the OK response from the external device, but the OK is not being stored at b14 and b15, based on the terminal screen showing blanks. If I don't include "AT" as a qualifier, AT is stored in b14 and b15.

What am I doing wrong? Thanks.

Thanks.
 
Last edited:

Technical

Technical Support
Staff member
The qualifier takes longer to process between bytes then a straight 'any byte' receive. You may want to consider using a setfreq m16 and T2400_16 instead (so more firmware processing can occur between each byte).

Or just use this to receive 4 bytes instead of 2

serin [1000],C.2,baud, b14,b15,b14, b15
 

tracecom

Senior Member
The qualifier takes longer to process between bytes then a straight 'any byte' receive. You may want to consider using a setfreq m16 and T2400_16 instead (so more firmware processing can occur between each byte).

Or just use this to receive 4 bytes instead of 2

serin [1000],C.2,baud, b14,b15,b14, b15
Thanks for the quick response. It wasn't the timing that was the issue, but rather that the external device was adding superfluous CRs and LFs to its responses before sending OK. I was able to determine this while trying variations on your second suggestion. Here's a snip of code that works at T2400_4.

Code:
serout C.4,baud,("AT",13,10) 'Send attention command+CR+LF to ESP-01.
serin [3000],C.2,baud,("AT",13,13,10,13,10),b14,b15 'Discard characters and store OK at b14 and b15.
sertxd(b14,b15,13,10) 'Sends contents of b14,b15 to terminal, followed by CR,LF)
 
Last edited:

lbenson

Senior Member
If all you want is to see an "OK", why not make that your qualifier, with no input bites? Still use the timeout [with an address to go to] to be able to handle the case when you get no response.
 

tracecom

Senior Member
I didn't know that it was possible to save the qualifier, or maybe you mean don't save anything, and just code ok into the sertxd line? When I first started this, I actually thought that there would be some responses that were "no" instead of "OK" but maybe not. I am planning to add a goto address, but haven't gotten there yet.

Thanks.

If all you want is to see an "OK", why not make that your qualifier, with no input bites? Still use the timeout [with an address to go to] to be able to handle the case when you get no response.
 

lbenson

Senior Member
You wouldn't save the qualifier, you would just know that you had received it if you dropped to the line below the serin rather than going to the timeout label.

Something like this should work.
Code:
symbol bTimedOut = bit0

  bTimedOut = 1
  serin [3000,someLabel],C.2,baud,("OK")
  bTimedOut = 0
someLabel:
  if bTimedOut = 1 then ' timed out
    ' ...
  else     ' received "OK" within 3 seconds
    ' ...
  endif
 
Last edited:

tracecom

Senior Member
You wouldn't save the qualifier, you would just know that you had received it if you dropped to the line below the serin rather than going to the timeout label.

Something like this should work.
Code:
symbol bTimedOut = bit0

  bTimedOut = 1
  serin [3000,someLabel],C.2,baud,("OK")
  bTimedOut = 0
someLabel:
  if bTimedOut = 1 then ' timed out
    ' ...
  else     ' received "OK" within 3 seconds
    ' ...
  endif
That works. Thanks.
 

hippy

Technical Support
Staff member
Note there are ways to avoid the "bTimedOut" flag variable and speed the code up; the "someLabel" will only be jumped to when there is a timeout ...

Code:
WaitForOK:
  serin [3000,NoOK],C.2,baud,("OK")
  Goto GotOK

NoOK:
  :
  Goto HandleLackOfOK

GotOK:
  :
  Goto WhateverNext
 

lbenson

Senior Member
I was looking to use the flag to avoid the ill-famed "goto", though one is implied in the timeout label jump, and getting out of an error condition is considered one of the more acceptable uses of "goto". And, no, I don't want to start a "goto" war, either usage seems fine and understandable to me.
 

tracecom

Senior Member
I actually didn't use the timeout flag. :rolleyes: It didn't seem like I needed it, after lbenson pointed out that qualifiers can be used without listing them all, but rather just the one that is needed. I didn't find that in the manual.

Code:
AT:
serout C.4,baud,("AT",13,10) 'Send attention command+CR+LF to ESP-01.
serin[3000,send_fail],C.3,baud,("OK")
sertxd("AT OK",13,10)
Code:
send_fail:
sertxd("SEND FAILED",13,10)
goto CWQAP2
 

rossko57

Senior Member
Beware that your code will see "OK" if the target device responds "BROKEN" - really unlikely I know, but as well to understand the limitations.
 
Top