调试系统环境:Ubuntu 17.04 + Clion 17.1
今天在调试Ubuntu 系统通过串口发送16进制数据时,我发现了一个很神奇的问题现象。
先看看下面的代码:
//char *data = "hello world dhs!";
char data[9] = {0x11,0x22,0x33,0x11,0x22,0x33,0x11,0x22,0x33};
//data[0] = 0xAA;
//int datalen = strlen(data);
//send data
//while (1)
{
for (int i = 0; i < 100; i++)
{
SendLen = PortSend(fd, data, 9);
if (SendLen > 0) {
printf("No %d send %d data.\n", i, SendLen);
} else {
printf("Error: send failed.\n");
}
sleep(1);
}
}
然后在Clion 中进行编译,发现编译很完美,并能够通过 xgcom(一款Linux下的带GUI的串口调试助手)接收到16进制数据。
再看看以下代码:
//char *data = "hello world dhs!";
char data[2] = {0x11,0x22,0x33,0x11,0x22,0x33,0x11,0x22,0x88};
//data[0] = 0xAA;
//int datalen = strlen(data);
//send data
//while (1)
{
for (int i = 0; i < 100; i++)
{
SendLen = PortSend(fd, data, 2);
if (SendLen > 0) {
printf("No %d send %d data.\n", i, SendLen);
} else {
printf("Error: send failed.\n");
}
sleep(1);
}
}
然后再Clion中编译源代码时,就发现报错了,错误信息如下:
/home/dhs/桌面/serial_dhs/serail (cpp)/serial.cpp:305:66: error: narrowing conversion of ‘136’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
char data[9] = {0x11,0x22,0x33,0x11,0x22,0x33,0x11,0x22,0x88};
^
CMakeFiles/serail.dir/build.make:62: recipe for target 'CMakeFiles/serail.dir/serial.cpp.o' failed
make[3]: *** [CMakeFiles/serail.dir/serial.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/serail.dir/all' failed
make[2]: *** [CMakeFiles/serail.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/serail.dir/rule' failed
make[1]: *** [CMakeFiles/serail.dir/rule] Error 2
Makefile:118: recipe for target 'serail' failed
make: *** [serail] Error 2经过我多次测试,终于发现了一个很神奇的错误现象。
当发送的16进制数据包中有数据大于255时,便会编译报错。

最低0.47元/天 解锁文章
8619

被折叠的 条评论
为什么被折叠?



