Author Topic: how to get data out of the buffer using Input$(1)?  (Read 12436 times)

Leonard

  • Newbie
  • *
  • Posts: 2
  • I'm a llama!
    • View Profile
how to get data out of the buffer using Input$(1)?
« on: October 20, 2014, 10:42:21 AM »
I am using A$ = Input$(1) function to read serial data from a card reader
then, SETLCD command to display, but The problem is that when testing A$, it only shows the previous value.

How do I update the string variable without swiping another card?

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:how to get data out of the buffer using Input$(1)?
« Reply #1 on: October 20, 2014, 11:16:39 AM »
I am not quite sure what do you mean by "update the string variable without swiping another card"

A$ = INPUT$(1) lets the program read the CR-terminated string in the serial buffer. Presumably when you swipe the card the card reader sent the CR-terminated string to the PLC's COMM1 buffer. A$ = INPUT$(1) is then used to read the string.

Once it is read the string is removed from the buffer but will stay in A$.  You can then display the string using SETLCD on the LCD and you can process the data in the A$ e.g. using MID$, VAL, HEXVAL etc to convert to integers.

A$ data will not change unless you change it by re-assigning it.  If you continuously execute A$ = INPUT$(1) and there is nothing in the serial buffer then A$ will get a blank string. So the way to write the program is to read the data into a temporary string and then if there is data, store it into another string:

E.g.

B$ = INPUT$(1)
IF LEN(B$) <> 0  
    A$ = B$
ELSE
    RETURN
ENDIF

SETLCD 1,1, A$





Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Leonard

  • Newbie
  • *
  • Posts: 2
  • I'm a llama!
    • View Profile
Re:how to get data out of the buffer using Input$(1)?
« Reply #2 on: October 21, 2014, 06:42:31 AM »
Still having problems.

data is stored in the buffer only and is not in A$ or B$

The next time I read from COM1, the new data is again stored in the buffer, but the previous data now populates in A$ and B$

(Simulator gives the same results.)

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:how to get data out of the buffer using Input$(1)?
« Reply #3 on: October 21, 2014, 09:14:15 AM »
Note that the serial buffer can buffer up to 256 bytes of data. If you have multiple strings in the serial buffer, then INPUT$(1) will read the oldest data first and if there is new CR-terminated string in the buffer it will only be read in the next INPUT$(1) command.

To ensure that you read out only the latest string (discard old strings in the buffer), you can try the following:


FOR I = 1 to 10
    B$ = INPUT$(1)
    IF LEN(B$) = 0
         EXIT
    ELSE
        A$ = B$
    ENDIF
NEXT

' When execution reaches here A$ will contain the last non-blank string from the serial buffer.
 

 
Email: support@triplc.com
Tel: 1-877-TRI-PLCS