# How to stop a program HSERIN blocking

#### camerart

Joined Feb 25, 2013
2,113
Hi,
EDITED:
I'm using HSERIN an a program, but it blocks
How do I stop this please?
As this has been edited, some early replies, may look confusing.
Camerart.

Last edited:

#### ericgibbs

Joined Jan 29, 2010
10,846
hi C,
You could check for a character in the serial buffer, by using an ASM statement, prior to trying HSERIN.
E

#### camerart

Joined Feb 25, 2013
2,113
hi C,
You could check for a character in the serial buffer, by using an ASM statement, prior to trying HSERIN.
E
Hi E,
I can't 'do' ASM, and as I'm tidying up, I'll leave this till later in my project.
Thanks.
C.

#### ericgibbs

Joined Jan 29, 2010
10,846
hi C,
E

If PIR1.RCIF = 0 Then Goto getmsg
char = RCREG
............................

#### Ian Rogers

Joined Dec 12, 2012
807
You should use HSERGET.. This is non blocking but will return 0 if nothing has arrived...

#### camerart

Joined Feb 25, 2013
2,113
Hi E and I,
I found the [ getmsg ] programs, and now understand how to use it.

I tested HSERGET instead of HSERIN and it works fine, especially, as sometimes a message starts then gets garbled, and HSERGET READS it, so some DATA can be collected

At the moment I'm checking using the Simulator and at this point;
--------------------------------------------------------------------------------------------
Hserget char
If char = "$" Then 'start str1(0) = char str2(0) = char rxi = 1 Else Goto skip1 Endif ------------------------------------------------------------------------ if I mess up the beginning of the message which starts with [$ ] then it goes to [ skip1 ] but leaves the rest of the message blocking the [ Hardware UART sim ]
Will this be the same LIVE (I'll be LIVE testing later.)
Thanks,
C.

#### ericgibbs

Joined Jan 29, 2010
10,846
hi C,
As your serial inputs are not synchronised, recall what we had to do sometime ago.
If the RX buffer gets buffer gets data and it is not Read, then the OERR flag is set, if you do not test for this flag, when you read the buffer it could be 'old' rubbish data.
We used the following routine.

get_neo:
If RCSTA.OERR = 1 Then 'if over run error then flush RXD buffer
RCSTA.CREN = 0
RCSTA.CREN = 1
char = RCREG
char = RCREG
PIR1.RCIF = 0
Endif

sync1: 'wait for a $start of string If PIR1.RCIF = 0 Then Goto sync1 char = RCREG If char <> 0x24 Then Goto sync1 '$'
str1(1) = char
rxi = 2

#### camerart

Joined Feb 25, 2013
2,113
hi C,
As your serial inputs are not synchronised, recall what we had to do sometime ago.
If the RX buffer gets buffer gets data and it is not Read, then the OERR flag is set, if you do not test for this flag, when you read the buffer it could be 'old' rubbish data.
We used the following routine.

get_neo:
If RCSTA.OERR = 1 Then 'if over run error then flush RXD buffer
RCSTA.CREN = 0
RCSTA.CREN = 1
char = RCREG
char = RCREG
PIR1.RCIF = 0
Endif

sync1: 'wait for a $start of string If PIR1.RCIF = 0 Then Goto sync1 char = RCREG If char <> 0x24 Then Goto sync1 '$'
str1(1) = char
rxi = 2
Hi E,
We changed the [ get_neo ] to [ get_data ] where some changes were made, here is what is used at the moment:
I notice there isn't [ PIR1.RCIF ] in the later CODE.

Is there a reason for the second [ char = RCREG ] marked with XXXXXXXXXXXXXX
C.
Code:
------------------------------------------------------------------------------------
get_data:
'Await GPS RXD -----------------GPS RMC ----------------------
''''''PASTE: $GNRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A? '''OR PASTE:$REMOTE,1100,1200,1300,1400,1500,1600,?

If RCSTA.OERR = 1 Then  'if over run error then flush RXD buffer
RCSTA.CREN = 0
char = RCREG
char = RCREG  'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Hserout "RX Err!", CrLf
Goto skip1
Endif
'Hserin char
'HSERGET TEST
Hserget char

C

#### Attachments

• 67.6 KB Views: 2
Last edited:

#### camerart

Joined Feb 25, 2013
2,113
Hi,
I've tried the simulator where #15 is true.

LIVE tests:
Using HSERGET does keep the program flowing allowing the other modules (Compass etc) are serviced, but doesn't appear to process the CHARS so I think HSERIN has to be used. Any comments?

Using HSERIN, as shown in the simulator the UART blocks.

IMPORTANT NOTE:
In the program I use a DATA SWITCH to switch between the GPS and 'BASE/REMOTE', each INPUT switches to the other as each is READ
C

Last edited:

#### camerart

Joined Feb 25, 2013
2,113
Hi,
Reading #14 again, Am I correct that [ On UART RX ] would need a TIMER? If so then I don't have it set, would it be better if I did?.

Here is the bottom of the MAIN LOOP, showing the GOTO and the SUBROUTINE.
C.

#### Attachments

• 3.6 KB Views: 11

#### ericgibbs

Joined Jan 29, 2010
10,846
hi C,
You are Exiting a GOTO call to subroutine with a RETURN.
Its the Program thats hanging not the UART
E

get_data:
If RCSTA.OERR = 1 Then 'if over run error then flush RXD buffer
RCSTA.CREN = 0
char = RCREG '1
char = RCREG '2
PIR1.RCIF = 0 'clear the RCIF flag, which had been set by the earlier err data.
Hserout "RX Err!", CrLf
Goto skip1 ' ?????????????????????????????????
Endif
......................................................................
skip1:

For x = 0 To 79
str1(x) = 0
Next x

For x = 0 To 79
str2(x) = 0
Next x

Return '???????????????????????????????

#### camerart

Joined Feb 25, 2013
2,113
hi C,
You are Exiting a GOTO call to subroutine with a RETURN.
Its the Program thats hanging not the UART
E

get_data:
If RCSTA.OERR = 1 Then 'if over run error then flush RXD buffer
RCSTA.CREN = 0
char = RCREG '1
char = RCREG '2
PIR1.RCIF = 0 'clear the RCIF flag, which had been set by the earlier err data.
Hserout "RX Err!", CrLf
Goto skip1 ' ?????????????????????????????????
Endif
......................................................................
skip1:

For x = 0 To 79
str1(x) = 0
Next x

For x = 0 To 79
str2(x) = 0
Next x

Return '???????????????????????????????
Hi E,
The RETURN is returning to the earlier [ Gosub get_data ] See #17
C.

Last edited:

#### camerart

Joined Feb 25, 2013
2,113
Hi,
As each test using the fantastic simulator takes quite a while, I quite often loose track of what I'm testing (Even with notes) So I tend to test LIVE for this.

Today I woke up with a FLOWCHART in my head, but while I can 'see' it, I can't tell if it would work as a program.

Using the example in #17, I understand how each element works (Almost!) Here is what I want to try, and I think I can write it.
-------------------------------------------------------------------------------------------------
Use the [ If RCSTA.OERR = 1 Then ] section at the beginning.

[ If char = "\$" Then ] start LOOP of this until>
[ If str1(1) = "R" Then ] section, then look for that sentence [ ? ]
OR
[ If str1(5) = "C" or "R" Then ] section, then look for that sentence [ ? ]

(Now that sentence is in STR1 or 2) switch [ DATASWITCH ] to look for the alternate type of sentence, while processing this sentence. (This would hopefully save going though all of the approx 10x NMEA sentences when checking the GPS)

If either type of sentence are not RECEIVED then use an INTERRUPT to switch the DATASWITCH until one or the other sentence is RECEIVED.

I hope this is clear, I'll keep checking, while I attempt to write this, if someone would check the flow please?

EDIT: MARKED BOLD
C.
----------------------------------------------------------------------------------------------------

Last edited: