Menu

Hsergetstring

wn
2017-12-20
2017-12-20
  • wn

    wn - 2017-12-20

    Is there any way to enforce a timeout on HSerGetString as well as Ser1Receive? I don't want the code to just wait until a CR is sent in an endless loop.

     
  • Moto Geek

    Moto Geek - 2017-12-20

    what if you added a timer "timeout" along with hserreceive. If after a certain amount of time went by and there is nothing in the rx then carry on... or something of that nature?

     

    Last edit: Moto Geek 2017-12-20
  • mmotte

    mmotte - 2017-12-20

    wn,
    It would help if we had a hint of what you are trying to accomplish. HSerGetString has a rather specific mission of getting input from a terminal. the CR indicates the end of the input.

    Of course there are other ways, just not as easy as a "one liner".

    One good example of buffering input is by Thomas Henry and i refer to it often.
    https://sourceforge.net/p/gcbasic/discussion/629990/thread/b8141e3b/

    Another can be found in the GCB Help(cow question mark tool)> Example programs > serial/rs232 buffer ring.

    As MotoGeek indicates you could build your own.
    A caveat of that is that you do not want to use " #define USART_BLOCKING " because it will wait in an endless loop for a byte input.

    Also be aware of the following.

    'When Not using USART_BLOCKING the return value is defined by
    #define DefaultUsartReturnValue = 255
    

    So the way to get around this is check for data in the Rx buffer(only one byte buffer) before calling HSerReceive by:

    IF USARTHasData then HSerReceive(InChar)
    

    So back to the first question, what are we trying to receive?

    BR
    GL
    mike

     
  • wn

    wn - 2017-12-20

    I am using two different USART ports on a mega238pb. I need to be able to "switch" between them based on which one is receiving communication. I could use the USARThasData to determine when to enter the Hsergetstring line possibly? I just cannot tie up and wait on one particular port. The data stream to both ports is the same. It will be a string terminated by a CR. I can make the terminating character anything I need it to be. I will check on the USARThasdata if that is an accessible function.

    Thanks.

     
  • mmotte

    mmotte - 2017-12-20

    I think you are on to the solution. Just so the two ports aren't getting data simultaneously.

    'AVR Usart has data flags setup
        If Bit(RXC0) Then
          USARTHasData = "RXC0 = On"
        End If
        If NoBit(RXC0) Then
          USARTHasData = "RXC = On"
        End If
        If Bit(RXC1) Then
          USART2HasData = "RXC1 = On"
        End If
        If Bit(RXC2) Then
          USART3HasData = "RXC2 = On"
        End If
        If Bit(RXC3) Then
          USART4HasData = "RXC3 = On"
        End If
    

    This shows the 4 flags fo data availability on an avr and is part of the usart.h library wwhich is automatically included when you use any of the functions like HSerReceive or HSerGetString.

    GL
    Mike

     

Log in to post a comment.