Welcome! Log In Create A New Profile

Advanced

Error Code 3 when using UART

Posted by reesy73 
Error Code 3 when using UART
October 24, 2014 10:19AM
Hello

I have been experimenting with COM port 1 on the UART. I have a PC terminal connected to the PIC through this UART comport 1, (via rs232 chip), and it receives/outputs text fine.

However after about 10 messages received it bombs out with error code 3. It is a simple program where when the word "HELLO" is received from the terminal it runs the hello world function and it works fine but only for around ten times.

I am guessing that it is something to do with the UART buffer maybe???? Any tips would be great - I was experimenting with clearing the variable string buffer etc and this made no difference. I have attached the code below.....




function set_uart1()
comopen(1,9600,80) //open com port 1, 9600 baud, buffer size 80
pps_in(?U1RX, ?PRB6) //set rx on RB6
pps_out(?PRB7, ?U1TX$) //set tx on RB7
endf

function hello()
dim j
for j = 1 to 3
comouts(1, "Hello World\r\n")
next
endf

//-----------------------------------------

function check()
dim y = 0
while y < 1
dim x, s$[80] //set variables x for buffer size and s$ string
set_uart1
x = comread(1,s$,80,1) //read incoming data on port 1 and place in s$
comouts(2, s$) //output s$ to com2
if s$ = "HELLO" then
hello
wait(10)
endif
comouts(1, "Buffer Size = ")
comouts(1, x) //output on com 1 size of buffer (variable x)
comouts(1, " \r\n")
wait(10)
comclose(1)
wend
endf
Re: Error Code 3 when using UART
October 24, 2014 11:51AM
Hello,
you are running out of stack space because the local variables, x and s$ are being created each time they go round the while loop. The solution is to move all of the 'dim' to the top of the function. Also, but this will still work in your version, it is a good idea to use () even if the function does not have any parameters so change set_uart1 to set_uart1()

function check()
dim y = 0, x, s$[80] // all local variables should be defined first
  while y < 1
    // MOVED dim x, s$[80] //set variables x for buffer size and s$ string **********
    set_uart1 // set_uart1() is better **********
    x = comread(1,s$,80,1) //read incoming data on port 1 and place in s$
    comouts(2, s$) //output s$ to com2
    if s$ = "HELLO" then
        hello
        wait(10)
    endif
    comouts(1, "Buffer Size = ")
    comouts(1, x) //output on com 1 size of buffer (variable x)
    comouts(1, " \r\n")
    wait(10)
  comclose(1)
  wend
endf
Re: Error Code 3 when using UART
October 24, 2014 12:38PM
Once again - thanks!

Easy when you know!
Sorry, only registered users may post in this forum.

Click here to login