View previous topic :: View next topic |
Author |
Message |
kevinvig7
Joined: 25 Oct 2015 Posts: 3
|
Problem with function gets(string) rs232 |
Posted: Sun Oct 25, 2015 8:36 am |
|
|
Hi I have a problem with the function gets(string).
I make a program in C sharp that send a string by serial port. The string is a hexadecimal number of 6 characters.
When I try receive this with CCS with function gets(string) in the rda interrupt the program does nothing, it ignores the received data from rs232.
Should I send a null character at the end of the string with C sharp?
Thanks |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Sun Oct 25, 2015 9:27 am |
|
|
A few things of note:
1. Never use gets() in the RDA ISR. The ISR is for receiving "1" character
2. There is almost never any need to use gets() to begin with.
3. Look at ex_sisr.c located in the examples directory of your PICC install. It will show you how to properly use the RDA interrupt. The only gotcha is the buffer size in that example needs to be a power of 2 (if you don't want this, search the forum for ex_sisr.c and see other threads on how to change it for this).
The big thing here is you need to start looking at it from an "embedded" perspective vs a desktop application perspective. The method gets() can hang in multiple ways. While this can be less problematic in a windows program (I still don't use it here either though), it is absolutely killer for an embedded program. Once you hang, your device is useless. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sun Oct 25, 2015 9:34 am |
|
|
Hi,
You are making a classic mistake with the #int rda serial interrupt handler. This interrupt is designed to receive ONE character, and one character only, which you do with the getc function. If you want to receive multiple characters, create an array and fill the array as characters are received. Have the C# program prefix the data with a 'start' character, such as '!', and append the data with 'stop' character, such as a CR of LF. Start buffering when '!' is received and stop with 'CR' or 'LF' is received.
To answer your specific question, the gets function will hang until a carriage return is detected. You could add that to your C# program. _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
|
kevinvig7
Joined: 25 Oct 2015 Posts: 3
|
|
Posted: Sun Oct 25, 2015 10:27 am |
|
|
Thanks for your answer!
Can you give me an example of a program that does this?
will have to send a string or character followed ?
Thanks! |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
|
|