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

Application hangs
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah
Guest







PostPosted: Mon Feb 23, 2009 3:29 am     Reply with quote

Don't confuse 'strings' in C, with 'strings' over serial. Smile

Historically, the serial functions were built to handle data from 'teletype' type input devices, and handle 'lines' of text, terminated with a CR.

A 'string' is stored internally in C, with a '\0' terminating character, _but this is not sent, when it is transmitted over the serial_.

'Gets', gets a _line_ of text (with a terminating CR), and stores this as a 'string', with the terminating '\0' added. So it is really a 'get line, into string' function.

To send a string, you would print it, and then _add_ the terminating line feed.

Generally, you can't (easily) generate a '\0' character from a keyboard (ALT, numeric keypad 000, release ALT), while a CR, just involves hitting the enter key. Also, many terminal programs, won't actually send a '\0', even if you can type it...

As a general 'comment', avoid gets...
It is a 'dangerous' function to use. Since it has no limit checking, if data arrives without the CR, you can end up overwriting huge blocks of your memory, and stop everything working. Load 'input.c', and use the get_string function instead, which inclludes a length check.

As a separate comment, these functions are incredibly 'simple', and if you want to terminate on a different character, just copy the source code for get_string, and adapt it to use any character you want.

Best Wishes
sanddune008



Joined: 23 Oct 2008
Posts: 38

View user's profile Send private message

PostPosted: Mon Feb 23, 2009 3:35 am     Reply with quote

So,

eg:
Code:

typedef enum
{
 CMD1=1,
 CMD2
}Cmd;

Then to send CMD1 I need to convert them to ASCII postfix a Carriage return send it across the cable and at Tx reconvert the following back to int.

Will the ITOA() affix that for me?
Ttelmah
Guest







PostPosted: Mon Feb 23, 2009 4:33 am     Reply with quote

Not really.
It'd depend totally on what is at the other 'end' of the link.

Take a rather different example.
You pick up your mobile phone, and type a number. '1'. Nothing happens. '112113'. Nothing happens. '044182112113'. Nothing happens. To tell the phone to actually make the call, you have to hit the 'send', or 'pick-up' button on the phone.

Now the same is true with your 'CMD1'. This is a single byte of data, holding the binary value '0000000001'. You could write a program at the other end, which expects a single byte, and just send this byte, and receive it. However, in general, you will want to send more data than a single byte, so it becomes necessary to send the data in a format that allows you to work out were you are in the data as a whole.
So you could decide to use a single character (say '0', or '13'), as a 'terminator. However you then have a problem. If you send your data as raw 'bytes', then your value, _could_ have the actual value of the expected 'terminator' byte. So the solution at this point, is to only use _part_ of the range of numeric values available, and reserve the rest for 'control'. So, you decide to send your value, in 'text', as ASCII digits.
Itoa, converts a number, into it's text equivalent. So in this case, your CMD1 value, converts to the text digit '1'. Again though, you would need to attach something to separate this from the next number.

For instance, you elect to send three numbers. 0, 1, & 12. Now your receiving program, 'knows' it is to receive three numbers, and you elect to use ',' as the 'number' separator, and 'CR', as the 'all numbers complete' separator.
You then send over the serial:

0,1,12<CR>

At your receiver, you can use 'gets_string', with a limit length of say 13 characters. This allows for the possibility of the numbers being values like 100, rather than single digits. Then the reception code, extracts the digits up to the first ',' and puts these into the first number, etc..

There are 'ways' of sending data as raw bytes though. You could (for instance), have another connection line, and operate this when the data is going to start, and then release it, when the 'packet' is complete. The decision is completely up to _you_.

Hoever there does have to be some way of separating the parts of the communication, and the standard way, is using the 'C' functions like get_string, is to use the carriage return as the separator.

Atoi, only turns the text value, 'into' a numeric value, and itoa, the opposite. They do nothing about adding the control characters to provide this, separation, or removing these.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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