我正在开发一个项目,我需要通过USART将数据发送到终端。 我需要将数据显示为char的数值(0-255)(从EEPROM中收集
)我设法将char发送到终端(使用Putty或TerMite) 我的问题从char的值是不可打印的
开始这就是为什么我需要将char的值转换为数字
示例:当从EEPROM获取的数据为0x31时,我的例程将发送'1',但我需要向终端发送'049'或'49'
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();
}
}
谢谢,
答案 0 :(得分:0)
jolati建议的sprintf在某些情况下可以成为一匹好工作,或者更好的是,如果它适用于您的C18版本,则可以使用snprintf。这两个例程都遵循标准的printf格式(例如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);
}
答案 1 :(得分:0)
谢谢,我已经决定好好接受......
以下我所做的是:
将char的int值操作为3个新char (即)243变为3个字符 - 50,52,51(数字的ASCII)
(也许它长而瘸,但它的作用就像一个魅力)
继承剧本......
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;
}
后来我使用了puts1USART,其数组包含3个以上的字符(FirstChar,SecondChar&amp; ThirdChar)
我还添加了一个&#34; lineNumber&#34;在每4个值之前和第四个值之后的CrLf 它导致输出看起来像......
output to terminal in Putty over Serial port
及其工作......
感谢您的帮助
我会饶有你的建议, 盖