hserin and variable length strings problem.

ferrymanr

Member
I have an application that drives two stepper motors and responds to various commands via hserin. The serial comms needs to run in background to avoid too much interrruption of the stepper drive. Until now I have been receiving a fixed length command string consisting of command letter or letters usually followed by ASCII numbers for setting data. I have been using the hserptr value to branch into the hserin handling routine when the string received is the correct length.
The system has worked well for a couple of years but I now need to adapt the routine to handle data from another device that arrives in variable length strings up to about 16 characters ending in a 'CR'. My problem is how to determine when to call hserin and handle the parsing of the string. Using 'if hserptr = string_length then' in my main loop won't work. I need to detect the final 'CR' and only then parse and process the string.
Command strings are received at variable intervals which could be anything from 500mS to 5 days or more apart!
Any suggestions?
Thanks
Richard
 

hippy

Ex-Staff (retired)
You probably need to use background receive and have to track incoming data to determine when the CR is received. By noting where you started from and where the CR is you can handle the message detected.

This (untested) should show every line received that ends in CR on the terminal, but it doesn't handle receive buffer wrap-round. The "If hSerPtr > ptr" is only valid if there hasn't been buffer wrap-round.

Code:
PowerOnReset:
  HSerSetup B9600_8, %001

Main:
  Do
   Gosub CheckForCommand
  Loop

CheckForCommand:
  If hSerPtr > ptr Then
    If @ptrInc = CR Then
      Gosub HandleCommand
    End If
  End If
  Return

HandleCommand:
  SerTxd( "Received : " )
  Do
    Get startPtr, b0
    startPtr = startPtr + 1
    SerTxd( b0 )
  Loop Until b0 = CR
  SerTxd( LF )
  Return
 

ferrymanr

Member
The suggestion from nick12ab works well. Hippy's method works about 25% of the time, missing about 75% of the commands..
Thanks both for your help.
 
Top