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 support@ccsinfo.com

Input a Int on Serial (Or convert a string to int) ?

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



Joined: 13 Oct 2017
Posts: 2

View user's profile Send private message

Input a Int on Serial (Or convert a string to int) ?
PostPosted: Fri Oct 13, 2017 7:30 am     Reply with quote

I need send a number from my pc to pic, but i only know to send it as string. I tried the scanf function, but the code stop working. Tried the atoi function too, but unsuccessful.

How i concatenate (parse, convert,etc) a char to a integer number?
Or exists a function that can i get a int8 input on serial?

Code:
#int_rda
void Serial()
{
static unsigned char ch;

ch = getc(); //Example: I send the number 0
putc(ch); //OK, here shows the 0
printf("%u",ch); // Here shows the ascii code of ch, in example 0 is 48
//But i need the integer number to use on my functions.
}

//////////////////// Tried this too, but the code does not work.
#int_rda
void Serial()
{
char ch[10];
int8 pos=0;
get_string(ch,sizeof(ch));
pos=atoi(ch);
printf("\n%u",pos);
}

/////////////////EDIT

Seeing the input.c i get these lines:

Code:
int8 = get_Int8()   
int16 = get_Int16()
int32 = get_Int32()
float = get_float()

What is this? How can i use this?
Ttelmah



Joined: 11 Mar 2010
Posts: 12837

View user's profile Send private message

PostPosted: Fri Oct 13, 2017 8:30 am     Reply with quote

Lots of parts here:

First, INT_RDA, means _one character_ is waiting only. To read anything longer than a single character you need to be looking at multiple reads and putting the result together.

Now a character is a numeric code. ASCII typically. So the character 'A' is actually the number 65. You can send numbers as 'raw' data, so you could read a character from the serial, and put it directly 'into' an integer variable, and use it as a number. Alternatively, you can send the numbers in a more human readable form. The pluses are:

Human readable (obvious).
Means you can use some of the other ASCII codes as markers for what is going on (, <LF> etc..).
You can know if things go wrong (if a value that is meant to be a number arrives and is not a number there has been corruption...).

Downsides are that it is bulkier to send, and that you have to convert from the raw data to ASCII and back again.

Now the number '65' stored in an int in the computer can be sent over the serial, and you can simply read it at the other end.
To sent the actual 'text' "65", and know where you are, means you will have something like a line feed first and afterwards. Then you will have to read the multiple characters and turn these back into the binary number.

If you are sending it 'as a string', then the first part of this is done (hurrah).
the second part means you know have to read this string, and turn it back into the number.

Start by looking at the examples, and in the code library. For instance, 'ex_str.c', shows reading a string, and calculating words, and how many numbers are in it. Now if you look at 'atoi', and 'atol', these are the functions to read a numeric string, and get the number from these. There are similar ones for hex values. The 'get_int' routines you have found are for reading _raw_ binary numbers (not coded as ASCII). Not easy to generate. As another example, ex_extee.c, shows hexadecimal numbers being read from a serial connection (hex has the advantage of being more human readable than 'binary', but less bulky, and easier to convert than straight 'decimal' text). 'Ex_rs385.c', shows decimal numbers being read from a PC, to configure the address etc.. The method used will work for what you are trying to do.
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