View previous topic :: View next topic |
Author |
Message |
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
execution stops when I read a string from rs232 [solved] |
Posted: Sat Jan 18, 2014 10:45 am |
|
|
I would like to send a value to the PIC via serial port and the PIC should read it, convert to signed int16 and return it to the PC multiplied by a constant.
I start with a test program wich should echo the string back to the computer.
I use Termite, as serial port chat program.
Code: |
#include <16F886.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=8M)
#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7)
#include <input.c>
char c;
char str[10];
//----------------------------------------------------------------------------
void main() {
set_tris_a(0xFF);
set_tris_b(0x97);
set_tris_c(0x85);
//C0 C1 C2 C3 |C4 C5 C6 C7
//1 0 0 0 | 0 1 0 1
SETUP_SPI(FALSE);
while(1){
printf("\n\rnew setpoint: ");
if (kbhit()){ //wait until a character has been received
get_string(str,10);
printf("is: %s",str);
}
delay_ms(1000);
}
}
|
The problem I am facing is that after I hit enter on the pc keyboard (and thus the string is transmitted), the execution on the microcontroller goes stuck!
Last edited by webgiorgio on Tue Jan 21, 2014 2:01 pm; edited 1 time in total |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sat Jan 18, 2014 11:03 am |
|
|
The first thing I see (without my coffee this morning) if you have a string 10 characters long, you need to allocate a minimum of 10+1 characters for the buffer - the string is terminated with a \0
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Jan 18, 2014 11:19 am |
|
|
add ERRORS to Rs232 setup.
get your "string" ONE byte/char at a time
and add to the receive buffervusing a pointer or pointer var,
then look for the terminator char
either a \0 or\r from the source device in the stream.
never tie up execution with the WORTHLESS , and dangerous
get_string() function. though i deal with string passing all the time
i have never used it and never will.
BTW: A formal code safety review ought to reject any submission that uses that function, recognizing the execution danger it represents.
Last edited by asmboy on Sat Jan 18, 2014 11:20 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9240 Location: Greensville,Ontario
|
|
Posted: Sat Jan 18, 2014 11:19 am |
|
|
You need to add 'errors' to the use rs232( ...options...)
Have a look at the CCS example ex_sisr.c in the examples folder.
Unless you get 10 characters(last one being a null) you will always get 'hanged' !
also let the compiler handle the set_tris.... commands automatically. If YOU set them wrong you'll waste a lot of time trying to figure out WHY your program doesn't work quite as expected.
hth
jay |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Jan 18, 2014 3:17 pm |
|
|
One other possible problem is that the pc is transmitting CR+LF when you press the enter key. The getstring function is only testing for the CR to be received and then returns the received string. Next time you call kbhit it will see the LF still waiting in the buffer from the last string and your code interprets this as a new string waiting...
Easiest workaround is to configure the PC to just send CR instead of CR+LF. |
|
|
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
|
Posted: Mon Jan 20, 2014 10:56 am |
|
|
asmboy wrote: | never tie up execution with the WORTHLESS , and dangerous get_string() function. |
Thanks for the advice, it makes much sense.
I tried to use the ex_sisr.c. It works, but is not completelly clear to me:
1. Every time a character is recived in the port, there is an interrupt, and the character is stored at the position next_in.
2. next_in is incremented soon after I write the character in the array, so, along the rest of the program, it is actually pointing the next position of the array.
3. next_in does not be to be resetted from 31 to 0 because there is that clever "% BUFFER_SIZE"
4. What is this condition (next_in!=next_out) telling me?
5. does bgetc() stands for buffer-get-charcater?
I've now set Termite to use CR only. |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Mon Jan 20, 2014 11:07 am |
|
|
(next_in != next_out) is a test to see if there are characters in the buffer. If they are the same, the buffer is empty - if they are NOT the same, then there is data in the buffer available for use.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Jan 20, 2014 12:22 pm |
|
|
let me add:
so long as the two vars above are bytes or int8,
you can safely collapse the receive buffer by executing that
assignment shown above, without needing to turn ints off. |
|
|
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
|
Posted: Tue Jan 21, 2014 1:18 pm |
|
|
Oh yes now I understand it!
When I empty the buffer the pointer next_in is pointing to any place in the buffer (not necessarilly the position 0). So does the next_out.
It have been usefull modify the example a bit to output also the value of next_in and next_out. So that I also see that "enter" count as a character.
thank you
Follows here about making it a signed int16
http://www.ccsinfo.com/forum/viewtopic.php?p=183198#183198 |
|
|
|