View previous topic :: View next topic |
Author |
Message |
ROBOTICAR
Joined: 28 Aug 2007 Posts: 45
|
rs232 problem |
Posted: Sat May 28, 2011 1:43 pm |
|
|
Hi. I have a problem. How can I send and receive decimal numbers with rs232? I use getc() but this function just can receive ascii data (32 - 127).
I want to send and get number from 0 to 255 ! Can I ? How ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19480
|
|
Posted: Sat May 28, 2011 2:40 pm |
|
|
getc, receives characters. No low limit at 32.
The only reason for a limit at '127', would be if you are using 7bit, rather than 8bit comms.
Normally it is 'better' to send numbers as text, perhaps hex?. The reason is that if you use the entire range 0 to 255, how is your code going to know where numbers start/stop (character codes like 03, and 03, are commonly used for STX/ETX, and 13, for LF etc.), but they don't _have_ to be.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sat May 28, 2011 6:42 pm |
|
|
Getc() returns a byte. The byte can have any value from 0x00 to 0xFF. Putc() works the same. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sun May 29, 2011 9:59 am |
|
|
We spend our lives with decimal notation of numbers so often we see it as if it were a true number instead of just another way to represent a number.
Numbers are represented by notation. Binary notation is special since a PIC can use it directly ( within the size of registers) . If the register is a byte then binary notation of decimal notation numbers 0 to 255 can be directly manipulated by the PIC. Now a PIC register could contain other notation maybe the notation for an ascii char suppose it was ascii notation for the character '1' which in a register would if we look at it in hexadecimal notation is 31. The PIC isn't aware that a register contains notation for something other than binary so the PIC will if instructed add 1 to 31 to get 32 hexadecimal and this is ascii notation for the character '2'. This was not adding 1 decimal to 1 decimal to get 2 decimal the PIC added 1 to hexadecimal 31 to get the next char in the ascii sequence.
Now suppose you have the decimal number 123 and it is notated as three characters using ascii (hex 31 32 33 ) then the PIC needs to translate this notation to binary notation so it takes the first char hex 31 and removes the 30 to get 1 this effectively gets multiplied by 100 the next is translated multiplied by 10 and add and so on until the one byte PIC register contains decimal 123 notated in binary ( hex 7B). For big decimal numbers we need more space so we use 2 byte int16 or 4 byte int32.The reverse must be done on the way out. This is busy work so the compiler has routines for input conversion and output (ex printf) Now notation is just notation so 123 could be notated externally in binary as 01111011 or hex 7B and sent as a single byte. GETC and PUTC don't know what the char represents 7B hex could mean decimal value 123 or ascii code for '{' |
|
|
|