PicoMite serial Input


Author Message
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5994
Posted: 09:10am 06 Jan 2025      

Hi Mark,

When I cannot use INPUT for keyboard, I use something based on below:

 'this replaces INPUT txt$ and has a time-out
Sub get_txt_timeout
 Local a$,b$,t

 b$="" : t=Timer                     'timer start

 Do
   a$=Inkey$
   If a$<>"" Then b$=b$+a$:t=Timer:Print a$;
 Loop Until a$=Chr$(13) Or (Timer-t>5000) Or a$=Chr$(10)

 If a$=Chr$(13) Or a$=Chr$(10) Then
   txt$=b$
   new_cmd=1
 Else
   new_cmd=0
 End If

End Sub


You can also add delete (control-H) detection and trim off the leftmost character from b$. Above routine (on terminal screen) seems to delete with control-H, but it only adds a control-H to the b$ string, in stead of removing a character from it.
This has a timeout, and checks if the strin is terminated correctly. The esssence you need is the DO-LOOP.


For serial port I read character for character until I hit a delimiter (i.e. <CR>).

-or- below code is similar and adds a (some) character(s) to a string in a loop, and when for a defined period nothing is received (string length not changed), it returns. This works fine for MODBUS where messages are sent in compact strings without any dead time between characters (actually a dead time is defined as a message separator).

'modbus defines
 looptime=50             'ms
 timeout=1000            'ms
 loops=timeout/looptime  'number of loops in 1000ms

       'waiting for a response
       old_a = 0 : success = 0
       For i= 1 To loops                 '20x50ms = 1 sec

         Pause looptime                  'minimum time for a message to get in
         If Loc(1) Then e$=e$+Input$(Loc(1),#1)  'get answer

         'check if we have data, and no new data came in for 50ms (loop time)
         If old_a > 0 And Len(e$) = old_a Then success = 1 : Exit For

         old_a=Len(e$)

       Next


Volhout
Edited 2025-01-06 21:23 by Volhout