Reputation: 11
I am working on a project on which i need to send data over USART to terminal. I need to display the data as the numeric value (0-255) of the char (which collected from the EEPROM
i have managed to send the char as is to the terminal (using Putty or TerMite) My problem starts where the value of the char is non-printable
That's why i will need to convert the value of the char to numeric
Example: when the data acquired from the EEPROM is 0x31 my routine will send '1' but i will need to send '049' or '49' to the terminal
void SendToSer(void) {
unsigned char Looper;
for (Looper=EEPROM_START;Looper<EEPROM_END;Looper++){
ReadEEPROM(Looper); //returns ReadResult
Write1USART((char) ReadResult); //Sends the ASCII
ClrWdt();
}
}
Thanks,
Upvotes: 0
Views: 246
Reputation: 11
Thanks , I've decided to take it the long way......
Here's what i've done is:
Manipulated the int value of the char to 3 new char (i.e.) 243 became 3 chars - 50,52,51 (the ASCII of the digits)
(maybe its long and lame but it works like a charm)
heres the script....
void ConvertToNumeric(unsigned char IsValue, unsigned int LineNumber){
unsigned int SourceInt;
ClrWdt();
if (IsValue == 1){
SourceInt = (int) ReadResult;
}else{
SourceInt = (int) LineNumber;
LineNumber++;
}
ClrWdt();
switch (SourceInt/100){
case 2 : FirstChar = 50; SourceInt = SourceInt - 200; break;
case 1 : FirstChar = 49; SourceInt = SourceInt - 100; break;
case 0: FirstChar = 48; break;
}
switch (SourceInt/10){
case 9 :SecondChar = 57; SourceInt = SourceInt - 90; break;
case 8 :SecondChar = 56; SourceInt = SourceInt - 80; break;
case 7 :SecondChar = 55; SourceInt = SourceInt - 70; break;
case 6 :SecondChar = 54; SourceInt = SourceInt - 60; break;
case 5 :SecondChar = 53; SourceInt = SourceInt - 50; break;
case 4 :SecondChar = 52; SourceInt = SourceInt - 40; break;
case 3 :SecondChar = 51; SourceInt = SourceInt - 30; break;
case 2 :SecondChar = 50; SourceInt = SourceInt - 20; break;
case 1 :SecondChar = 49; SourceInt = SourceInt - 10; break;
case 0 :SecondChar = 48; break;
}
switch (SourceInt){
case 9: ThirdChar= 57; break;
case 8: ThirdChar= 56; break;
case 7: ThirdChar= 55; break;
case 6: ThirdChar= 54; break;
case 5: ThirdChar= 53; break;
case 4: ThirdChar= 52; break;
case 3: ThirdChar= 51; break;
case 2: ThirdChar= 50; break;
case 1: ThirdChar= 49; break;
case 0: ThirdChar= 48; break;
}
ResultInChars[0] = FirstChar;
ResultInChars[1] = SecondChar;
ResultInChars[2] = ThirdChar;
ResultInChars[3] = ' ';
ResultInChars[4] = NULL;
ResultInChars[5] = NULL;
ResultInChars[6] = NULL;
}
later i have used puts1USART with an array containing 3 above chars (FirstChar, SecondChar & ThirdChar)
i've also added a "lineNumber" before every 4 values and CrLf after the forth value and it has resulted an output looks like that.....
output to terminal in Putty over Serial port
and its working......
thanks for your help
I would apriciate your suggestions, Guy
Upvotes: 0
Reputation: 438
The sprintf as jolati suggested can be a good work horse in some situations or, even better, snprintf if it is available for your version of C18. Both routines follow the standard printf formatting (ex. https://en.wikipedia.org/wiki/Printf_format_string).
#include "stdio.h"
void main() {
char buffer[80];
unsigned char len, number = 152;
// Write at most 80 bytes to our buffer
len = snprintf(buffer, 80, "sprintf string, heres a number: %d", number);
// buffer now contains our string, len is the number of bytes written
// or
len = printf(buffer, "... %d",number);
}
Upvotes: 0