Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 23:35 10 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Serial Port Query

Author Message
cs41
Newbie

Joined: 08/08/2016
Location: Australia
Posts: 28
Posted: 07:59am 20 Jan 2018
Copy link to clipboard 
Print this post

I am working on a program that uses a 3G modem. (SIMCOM)

I am wondering if there is a quick way / command to clear the receive buffer in MM Basic or is closing and re-opening the port the best way to do it.

Using LINE INPUT (because of variable length messages) I seem to get an accumulation of old data from the modem and think it's being held in the receive buffer.

Any ideas appreciated.

cs

 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 08:39am 20 Jan 2018
Copy link to clipboard 
Print this post

Hi cs

I try not to use LINE INPUT (or INPUT). It takes away control from your prog. Fine if you are waiting for someone to type something but no timers fire etc so for input in a process flow they are a bit primitive and blunt - you are inside a looping routine inside MMBasic and not in your prog.

I use a similar device (M590) and here are the get and the flush routines I use (and have performed without failure for 2 years on my SMS Gateway)... It isn't rocket-science but I don't get crap left in the buffer. I have removed some lines where I look for specific messages which would only confuse here.

Check the buffer and remove stuff and build up the string yourself, checking for CHR(13) to tell you have a complete line (and discarding other control characters).

Here's my "get stuff from the 3Gmodem" routine... you can pick the bones from it. I build a line in LN$ and finally any data will be in LNBUF$. If no CR was recieved within the timeout, LNBUF$ will be empty. I get one character at a time so I can recieve multiple messages without snipping off the front of the next. saves complex checking and concatenating... the MM is fast enough that this is a mere eye-twinkle and it gives perfect control over where a message ends The only way out is a nice, complete string or a timeout. Any stuff after the string will remain in the buffer until the next pass (or you flush it).


LNBuf$=""
DO
IF LOC(#GSM)<>0 THEN
A$=INPUT$(1,#GSM):C=ASC(A$)
SELECT CASE C
CASE 13
IF LN$<>"" THEN
LNBuf$=LN$
END IF
EXIT DO
CASE 0 TO 31
CASE ELSE
LN$=LN$+A$
END SELECT
END IF

IF FlagTest(TOFg) THEN EXIT DO
LOOP
ClearTOTimer

... parse your string here


Set a timeout counter before hand and then if anything came back it will be in LNBUF$. Note the timeout here is a "everything must complete" timer - it isn't reset each time we get a character - we could get unmitigate-able (its a word now) hang-ups as characters dribble in. We give it something to do and the response must come back at most 7 seconds, say.

For Flushing; I use the same SUB in the code to flush two different devices so just send it the number you opened the com port as (see my #GSM above).

e.g. FlushIO 2



SUB FlushIO(stream)
LOCAL STRING A$
DO WHILE LOC(#stream)<>0
PAUSE 50:A$=INPUT$(MIN(255,LOC(#stream)),#stream)
LOOP
END SUB


might give you some pointers but you have probably tried this. Good luck

oh, and the use of MIN() to ensure we don't get an error on the input might not work on your version of MMBasic... tbh I never had a problem but I understand there was an obscure bug that was fixed in 5.04.08

h

Edited by CaptainBoing 2018-01-21
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025