如果我理解正确的话,第一部分你的例子:
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