Menu

Software serial TIMEOUT

Help
Gigi
2020-04-22
2020-05-04
  • Gigi

    Gigi - 2020-04-22

    I modified an example of the serial software routines to be able to insert a well-defined and calculated timeout time according to the clock with a very simple script, from some tests it seems tests everything seems to work. I wanted your opinion.
    Also I tried to use a function to pass a value that is not considered in the manual, is all this working?

    ' CALL function
    if ser_tout(300) = "r" then ser1print "Ok"  ' legge seriale (300mS timeout)
    ' ---
    function ser_tout( RxTimeOut as word)  ' time out in mS
    #script
      If PIC Then
        SerTime = int (chipMHZ * 100 / 5) ' (8Mhz)160 = 1mS circa
      end if
    #endscript
    
     Do
       Repeat serTime  'large inner loop to get delays of some seconds
         If ! SER1_RXPIN  Then  'if falling edge of startbit detected
          ser_tout = Ser1Receive              '... get byte
          Exit Sub                           'exit sub after byte is stored
         End If
       End Repeat
       RxTimeOut --                        'count down the delayloop
     Loop While RxTimeOut                    'exit if is down to 0
    
    end function
    
     
    • Anobium

      Anobium - 2020-05-03

      @Gigi. With respect to this code. I will explain.

      The function works as designed but as scripts are essentially all run at the start of compilation the script is not executed as you have it.

      So, changed to conditional test as follows:

      ' CALL function
      if ser_tout(300) = "r" then ser1print "Ok"  ' legge seriale (300mS timeout)
      ' ---
      function ser_tout( _RxTimeOut as word) as byte ' parameter as time out in mS returns ... a default of 0....when times out
                  Dim _SerTime as Word
                  #Ifdef PIC Then
                      SerTime = int (chipMHZ * 100 / 5) ' (8Mhz)160 = 1mS circa
                    #endif
      
                  #Ifdef AVR Then
                      SerTime =  Stuff??
                    #endif
       Do
         Repeat serTime  'large inner loop to get delays of some seconds
           If SER1_RXPIN = 0  Then  'if falling edge of startbit detected
            ser_tout = Ser1Receive              '... get byte
            Exit Function                       'exit sub after byte is stored
           End If
         End Repeat
         _RxTimeOut --                        'count down the delayloop
       Loop While _RxTimeOut                    'exit if is down to 0
      
       ser_tout = 0 'or the default value when it times out
      end function
      

      ~~~

       

      Last edit: Anobium 2020-05-03
  • Gigi

    Gigi - 2020-04-22

    The initialization of the ser_tout variable is missing.
    Better this way:

    function ser_tout( RxTimeOut as word)  ' time out in mS
    #script
      If PIC Then
        SerTime = int (chipMHZ * 100 / 5) ' (8Mhz)160 = 1mS circa
      end if
    #endscript
    ser_tout=0
     Do
       Repeat serTime  'large inner loop to get delays of some seconds
         If ! SER1_RXPIN  Then  'if falling edge of startbit detected
          ser_tout = Ser1Receive              '... get byte
          Exit Sub                           'exit sub after byte is stored
         End If
       End Repeat
       RxTimeOut --                        'count down the delayloop
     Loop While RxTimeOut                    'exit if is down to 0
    
    end function
    
     
    • Anobium

      Anobium - 2020-05-03

      @Gigi. Regarding this code. Look at this guide https://github.com/Anobium/Great-Cow-BASIC-Help/blob/master/source/DeveloperGuide.adoc

      I have copied the code down from my previous post.

      ' CALL function
      if ser_tout(300) = "r" then ser1print "Ok"  ' legge seriale (300mS timeout)
      ' ---
      function ser_tout( _RxTimeOut as word) as byte ' parameter as time out in mS returns ... a default of 0....when times out
                  Dim _SerTime as Word
                  #Ifdef PIC Then
                      SerTime = int (chipMHZ * 100 / 5) ' (8Mhz)160 = 1mS circa
                    #endif
      
                  #Ifdef AVR Then
                      SerTime =  Stuff??
                    #endif
       Do
         Repeat serTime  'large inner loop to get delays of some seconds
           If SER1_RXPIN = 0  Then  'if falling edge of startbit detected
            ser_tout = Ser1Receive              '... get byte
            Exit Function                       'exit sub after byte is stored
           End If
         End Repeat
         _RxTimeOut --                        'count down the delayloop
       Loop While _RxTimeOut                    'exit if is down to 0
      
       ser_tout = 0 'or the default value when it times out
      end function
      
       
  • Gigi

    Gigi - 2020-05-02

    I fixed the routine, the function does not pass any value, you have to define it first. I have done many tests and it seems to work well.

    #define RxTimeOut 500   'time_out 500 mS
    #define Rx_ser PORTB.6   'define pin serial
    
    ' CALL function
    if ser_tout = "r" then ser1print "Ok"  'read seriale con TimeOut 
    
    function ser_timeout  ' time out in mS
    #script
      If PIC Then
        SerTout = int (chipMHZ * 400 / 5) ' (8Mhz)640 = 1mS circa
      end if
    #endscript
    ser_timeout= 0
    repeat RxTimeOut
       Repeat SerTout  'large inner loop to get delays of some seconds
          if Rx_ser = 0 then
            ser_timeout= Ser1Receive '... get byte
            Exit function
          end if
       End Repeat
    End Repeat                 '
    
    End function
    
     
  • Anobium

    Anobium - 2020-05-03

    @Gigi. Which library as this looks good code.

     
  • Anobium

    Anobium - 2020-05-03

    And @Gigi....

    Surely this function would be called.. SerNReceiveTimedOut where N is port.

    This is hardcoded to Ser1Receive.

    May need a little more thiking, and, I am not sure what happens with
    AVRs?
    The default value of a timed out value is 0?

     
  • Gigi

    Gigi - 2020-05-04

    @Anobium
    Thanks for the help to better understand, now the function passes a value correctly, the script has been removed I simply put a definition to get the delay calculation.

    Note that the name of the polling pin was originally incorrect.

    Yes, if no data is received it returns zero, this could be a problem, it would be to return the flag but it further complicates the routine.

    I have done many tests and everything seems to be working well.

    ' CALL function
    if ser_timeout(500) = "r" then ser1print "Ok"  'read seriale  TimeOut 
    ' ----
    function ser_timeout( _RxTimeOut as word) as byte  ' time out in mS
    #define SerTout chipMHZ * 400 / 5     ' (8Mhz)640 = 1mS circa
    
    Repeat _RxTimeOut
       Repeat SerTout  'large inner loop to get delays of some seconds
          if SER1_RXPORT.SER1_RXPIN = 0 then
            ser_timeout= Ser1Receive   '... get byte
            Exit function
          end if
       End Repeat
    End repeat                '
    ser_timeout= 0
    
    End function
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.