Menu

Reading multiple lines from HSerial

Help
Peter
2017-03-12
2017-03-12
  • Peter

    Peter - 2017-03-12

    I'm trying to read the following from the serial port (this is copied from the terminal screen):

    +CMT: "+4915781973824",,"2017/03/12,19:17:51+01"
    abcdefghijklmnop-elo.me/ud364
    

    I can read the first line using HSerGetString tempStr, I assumed to get the next line (starting abc) I just call HSerGetString tempStr again, however it only returns this:

    abV+Kkelo.me/ud364
    

    Any idea what I'm doing wrong?
    If I shorten the first line so the data is:

    +CMT: "+4915781973824",,"2017"
    abcdefghijklmnop-elo.me/ud364
    

    The second HSerGetString tempStr returns the whole of the second line correctly.
    Cut down code is below:

    #chip 16F1765,32
    
    'Configure serial
    #define USART_BAUD_RATE 115200
    #define USART_BLOCKING
    
    Do
        tempStr = ""
    
        'Read a line from the serial
        HSerGetString tempStr
    
       If instr(tempStr,"+CMT:") Then
          'SMS Received
          'Not interested in the first line
          'Read the second line from the serial
          HSerGetString tempStr
          'Output on the GLCD
          GLCDCLS
          GLCDPrint(0,0,"SMS received ")
          GLCDPrint(0,10,tempStr)
          'Also output back to the serial
          HSerPrint tempStr
          HSerPrintCRLF
        End if
    
    Loop
    
     
  • Peter

    Peter - 2017-03-12

    The data I receive on the serial port is actually this, with a couple of extra lines at the start:

    +CIEV: "MESSAGE",1
    
    +CMT: "+4915781973824",,"2017/03/12,19:17:51+01"
    abcdefghijklmnop
    

    I have got around this for the moment by changing my code to detect the "+CIEV" line and then skip:

        If instr(tempStr,"+CIEV: ") Then
          'SMS Received
          'Skip 2 lines and read the third
          HSerGetString tempStr
          HSerGetString tempStr
          HSerGetString tempStr
          'Output on the GLCD
          GLCDCLS
          GLCDPrint(0,0,"SMS received ")
          GLCDPrint(0,10,tempStr)
         'Also output back to the serial
          HSerPrint tempStr
          HSerPrintCRLF
        End if
    

    I'm not sure why the original didn't work. I think it must be something to do with the length of the line it is reading.

     
  • mmotte

    mmotte - 2017-03-13

    Peter,
    I think you are hitting the stringsize limit. Look in the help file for variables and you will see "40 bytes for devices with more RAM than 367 bytes."

    HSerGetString was never meant to grab GPS strings nor SMS messages.

    HSerGetString is looking only for a CR (13) to exit the subroutine so you have to be careful to have one at the end of the string.

    HSerGetString throws away any chars less than 32(space) or greater than 126(~) , so they will not be stored in the string array.

    I have never messed with longer string lengths but the help file does say "'To define the default string size as the follows constant.
    #define STRINGSIZE 24" so maybe you could increase the size.

    Also it says the string can not cross pages of memory, i am new to 16F17XX so i don't know about that.

    In the "contributors forum" i shared some code about getting values from a GPS without reading the whole GPS statement. Stored the comma delimited values in separate strings.

    Br
    Mmotte

     

    Last edit: mmotte 2017-03-13
    • Peter

      Peter - 2017-03-13

      There is definitely a CR on the end of the lines, Some of the input is being spoofed in Bray's Terminal and I've typed in a #013 to make sure.

      I'll try experimenting with STRINGSIZE to see if that makes a difference. I might also swap to a different chip (the 16F1765 doesn't really have enough RAM for the GLCD anyway).

      I saw your GPS code, however it would need quite a bit of modification for what I need because I'm not waiting for a single command, I'm reading the command and branching to the code to deal with that particular command (which varies in length...). It's a possibility though.

      William Roth posted a serial buffer code which I tried but the interrupt didn't seem to be occurring on the HW serial.

       
  • William Roth

    William Roth - 2017-03-15

    William Roth posted a serial buffer code which I tried but the interrupt didn't seem to be occurring on the HW serial.

    @Peter

    I am guessing that you are referring to the code I posted at the link below.

    https://sourceforge.net/p/gcbasic/discussion/projects%26guides/thread/f8b609c0/

    I tested that code pretty well and the interrupt worked as expected during testing. However, I do have problems with Bray's Terminal corrupting data from time to time, so I mostly use TerraTerm instead.

    Perhaps you can post the source code where the Hserial interrupt did not seem to work so that I can have a look. If there is problem with the code I posted, it needs to be corrected. If there is a problem with your code, then I will be glad to help sort it out,

    William

     

    Last edit: William Roth 2017-03-15

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.