CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

execution stops when I read a string from rs232 [solved]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
webgiorgio



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

execution stops when I read a string from rs232 [solved]
PostPosted: Sat Jan 18, 2014 10:45 am     Reply with quote

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!
Shocked


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

View user's profile Send private message

PostPosted: Sat Jan 18, 2014 11:03 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Sat Jan 18, 2014 11:19 am     Reply with quote

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.

Very Happy Very Happy Very Happy


Last edited by asmboy on Sat Jan 18, 2014 11:20 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jan 18, 2014 11:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jan 18, 2014 3:17 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 20, 2014 10:56 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jan 20, 2014 11:07 am     Reply with quote

(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

View user's profile Send private message AIM Address

PostPosted: Mon Jan 20, 2014 12:22 pm     Reply with quote

let me add:

Code:
next_out=next_in;


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

View user's profile Send private message

PostPosted: Tue Jan 21, 2014 1:18 pm     Reply with quote

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

Very Happy

Follows here about making it a signed int16
http://www.ccsinfo.com/forum/viewtopic.php?p=183198#183198
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group