I know this is simple, but I'm missing it somehow.
szTemp[1024] = "";
sprintf((char*)szTemp, "%c%c", 0x7e, 0x00);
Only outputs ~
over UART. Any more than 1 byte doesn't work. This does work, however:
sprintf((char*)szTemp, "some test string");
Gives: some test string
Update: The XBEE module I'm communicating with takes API command frames. One of which is a data transmission frame. Without going into the structure of the frame, the bytes to be sent are:
7E 00 17 10 01 00 13 A2 00 41 66 0F 42 FF FE 00 00 74 65 73 74 20 64 61 74 61 CA
I can write to the uart by setting the value of the transmit register and waiting for it to finish:
COMTX = 0X7E;
while ((COMSTA0 & 0x40) == 0x00){}
It sounds like you're trying to construct a sequence of bytes in szTemp
.
It sounds like those bytes might not all be printable characters.
It sounds like the code that transmits the contents of szTemp
to your UART is working fine.
If you just want to construct a string of arbitrary bytes, you don't even need sprintf
. (It could probably be made to work using sprintf
, but it'll be more cumbersome and won't buy you anything.) Try this:
szTemp[0] = 0x7e;
szTemp[1] = 0x17;
szTemp[2] = 0x10;
szTemp[3] = 0x01;
szTemp[4] = 0x00;
/* now your existing code for transmitting szTemp` to UART */
Based on the examples you posted, it sounds like your existing transmit-the-string-to-the-UART code is stopping when it reaches a 0 byte, which makes sense if the string is a null-terminated C string. But obviously, if the bytes to be sent are arbitrary binary bytes which might include 0x00
, you don't want to stop at the first 0. So that code may need reworking.
I'm speculating now, but you might want something like this:
char szTemp[] = { 0x7E, 0x00, 0x17, 0x10, 0x01, 0x00, 0x13, 0xA2, 0x00,
0x41, 0x66, 0x0F, 0x42, 0xFF, 0xFE, 0x00, 0x00, 0x74,
0x65, 0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61, 0xCA };
int nch = 27;
int main()
{
int i;
for(i = 0; i < nch; i++) put_one_character(szTemp[i]);
}
void put_one_character(char c)
{
COMTX = c;
while((COMSTA0 & 0x40) == 0x00) {}
}