如果我理解正確的話,第一部分你的例子:
sprintf(data,"%s%s",
"\x30\x60\x60\x20\x15\x35\x35",
"\x08");
正在做你想要的。問題是,在接下來的%s
,你正在使用"\\00"
,你想服務器接收ASCII \00
(這將是0x5c,爲0x30,的0x30),而是服務器報告它接收ASCII \5c00
(這將是0x5c, 0x,35,0x43,0x30,0x30)。
我克拉斯Lindbäck同意的,因爲它聽起來像的VeriFone終端正在做正確的事情,但服務器顯示是錯誤的。爲了證明這是正確的(你可以只做一個或另一個,或者你可以一起做),我會考慮做兩件事來排除這種情況。
第一條:您可以使用LOG_PRINTF(或者打印到紙張或屏幕上,如果你喜歡)打印每個字節的值,你把它關閉之前。下面是我寫過的一個快速而骯髒的函數,當我對一次類似的問題進行故障排除時。請注意,我只關心字符串的開頭(如你所見,似乎是這樣),所以如果我用完了緩衝區空間,我不打印結尾。
void LogDump(unsigned char* input, int expectedLength)
{
#ifdef LOGSYS_FLAG
char buffer[100];
int idx, bfdx;
memset(buffer, 0, sizeof(buffer));
bfdx = 0;
for (idx = 0; idx < expectedLength && bfdx < sizeof(buffer); idx++)
{
//if it is a printable character, print as is
if (input[idx] > 31 && input[idx] < 127)
{
buffer[bfdx++] = (char) input[idx];
continue;
}
//if we are almost out of buffer space, show that we are truncating
// the results with a ~ character and break. Note we are leaving 5 bytes
// because we expand non-printable characters like "<121>"
if (bfdx + 5 > sizeof(buffer))
{
buffer[bfdx++] = '~';
break;
}
//if we make it here, then we have a non-printable character, so we'll show
// the value inside of "<>" to visually denote it is a numeric representation
sprintf(&buffer[bfdx], "<%d>", (int) input[idx]);
//advance bfdx to the next 0 in buffer. It will be at least 3...
bfdx += 3;
//... but for 2 and 3 digit numbers, it will be more.
while (buffer[bfdx] > 0)
bfdx++;
}
//I like to surround my LOG_PRINTF statements with short waits because if there
// is a crash in the program directly after this call, the LOG_PRINTF will not
// finish writing to the serial port and that can make it look like this LOG_PRINTF
// never executed which can make it look like the problem is elsewhere
SVC_WAIT(5);
LOG_PRINTF(("%s", buffer));
SVC_WAIT(5);
#endif
}
其次:嘗試爲char數組中的每個位置分配一個顯式值。如果您已經使用了我的LOG_PRINTF
建議,並且發現它並未發送您認爲應該的內容,則這將是解決此問題的一種方法,以便它能夠正確發送您想要的內容。這種方法比較繁瑣一些,但因爲你是拼寫出來的每個值,無論如何,它應該不會太大更多的開銷:
data[0] = 0x30;
//actually, I'd probably use either the decimal value: data[0] = 48;
// or I'd use the ASCII value: data[0] = '0';
// depending on what this data actually represents, either of those is
// likely to be more clear to whomever has to read the code later.
// However, that's your call to make.
data[1] = 0x60;
data[2] = 0x60;
data[3] = 0x20;
data[4] = 0x15;
data[5] = 0x35;
data[6] = 0x35;
data[7] = 0x08;
data[8] = 0x5C; // This is the '\'
data[9] = 0x48; // The first '0'
data[10]= 0x48; // The second '0'
data[11]= 0;
//for starters, you may want to stop here and see what you get on the other side
後你已經證明自己,這IS或VeriFone代碼不是導致問題,您將知道您是需要關注終端還是服務器端。
向我們展示相關的部分代碼的。 – 2015-02-10 11:32:31
也許服務器正在接收'\',但將其打印爲'\ 5c'? – 2015-02-10 11:43:35
這是我想向服務器發送與它的頭,但是當我要打印\ 00我有\ 5C00數據:\t \t \t的sprintf(數據,「%s%s%S%S%s%S% S%S%S%S%S%S%S 「 」\ X30 \ X60 \ X60 \ X20 \ X15 \ X35 \ X35「, 」\ X08「, 」\\ 00「, 」\ x0x00「,」 \ X01 \ X30 \ X30 \ X30 \ X30 \ XC0 \ X30 \ X30 \ X30 \ X30" , 「\ X97」, 「\\ 00」, 「\ X30 \ X30」, 「\ X00 \ X00 \ X01 \ X00」 「\ X02」,idTerminal,idCommercant, 「\ X20 \ X20 \ X20 \ XA4 \ XBC」); – knk 2015-02-10 13:18:34