Peter,
if you can't do flow control then the other alternative is to increase
the time out on the inp() from
say 2000 to 5000 to 10000. Although the numbers are big the actual
delay is quite small. I find that 5000
works with the ASI devices that don't have flow control but there are
occasional problems.
Come to think of it, that was the reason why the inp() statement had a
time out because of the single hardware
buffer that was provided with PIC. The inp() is a kind of pseudo
buffer.
I have put a few thoughts down here more of a discussion than an
actual solution which may clear up a few points, if I could use your
code as an example,
my comments are followed by !! as I can't change the colour of font.
!! This sets up a schedule to check the incoming serial stream which
is one way of doing it
!! As it stands the check will take place every 1/2 second
function init
opencom 1 9600 0
process check_serial2 every 500
endf
!! The while statement should not be used within a schedule: what
happens if it is still in this loop
!! and gets called again? The schedule itself is the loop process
function check_serial2
while key?(1) <> 1 !! I don't think this is correct as when a key is
available key?(1)=1 **
print chr$(key(1))
// print "-" <--- add this and it give t-e-s-
wend
endf
** This will print the char when there is no key available??
!! This is an alternative; when this is called if no key is available
then it does nothing and returns
!! if a key is available then it gets the string using the while
statement. There are two possible problems with this
!! 1) The while could take longer then 1/2 second and get called twice
!! 2) A key could be missed if the while returns between characters,
inp() statement would help to prevent that see **** later
function check_serial2
if keyq(1) = 1
while key?(1) = 1
print chr$(key(1))
// print "-" <--- add this and it give t-e-s-
wend
endif
endf
!! Another alternative, but because there is only a single byte buffer
calling this every 1/2 second
!! will miss some characters. At 9600 a character will be received
every 1 ms so reducing the time may
!! help
function check_serial2
if key?(1) <> 0
print chr$(key(1))
// print "-" <--- add this and it give t-e-s-
endif
endf
**** The first post was nearly the solution, I suspect that what was
happening with this is that the 1ms schedule time and the 2000 delay
did not match up and the function was being called whilst still in the
function
I have not tested the function below but the theory is that if a char
is not available then it simply returns, if a char is available then
it will attempt to get the whole string until #13 is received. The
value of 400 needs to be found by trial and error and should be set to
that it does not time out between characters. It is not really
practical to call this on a schedule as the length of time this
function takes to execute will depend on how many characters it
receives.
function check_serial
if key?(1) <> 0 then
a=inp(1,a$,400,13)
if a > 0 then
print a$
endif
endif
endf
Yet another solution: how about implementing the buffer in Basic?
Using key?() as soon as a charter is received then put it into an
array, this could be done with a tight schedule and at 1ms should be
able to catch all characters for 9600 Baud. The down side is that it
would need a circular buffer implementing which complicates matters. I
have done a similar thing with the keypad example.
I hope this was informative and there are probably a few mistakes for
which I apologise, none of the code mentioned was tested so it may not
work (theory only). As I mentioned before the next release which is
due very shortly will have software buffers implemented and so much of
the above is academic but possibly interesting.
Jim