【发布时间】:2012-06-11 02:35:53
【问题描述】:
别问我怎么做的——我一点也没有。
以下代码会导致我的终端和我使用的任何运行时分析工具崩溃,但不会引发任何静态检查工具警告。 Valgrind、cppcheck 和 gdb 对我没什么好处。 g++ -Wall 没有给我任何有用的东西。
代码的目标是通过 USB 串行连接将字符写入 audrino。 audrino 地址作为第一个参数传递。传递一个 unsigned int 并将其强制转换为一个 unsigned char。
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <iostream>
using namespace std;
int main(int argc,char** argv){
struct termios stdio;
int tty_fd;
unsigned char ctrlChar;
unsigned int ctrlInt;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
tcgetattr(tty_fd, &stdio);
cfmakeraw(&stdio);
stdio.c_cc[VMIN] = 1;
stdio.c_cc[VTIME] = 0;
tcsetattr(tty_fd,TCSANOW, &stdio);
tcsetattr(tty_fd,TCSAFLUSH, &stdio);
fcntl(tty_fd, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
cfsetospeed(&stdio,B9600); // 9600 baud
cfsetispeed(&stdio,B9600); // 9600 baud
cout << "ready on " << argv[1] << endl;
scanf("%u", ctrlInt);
cout <<"recieved initial read.\n";
while (ctrlInt < 256){
ctrlChar = ((char) ctrlInt);
cout << ctrlInt << ctrlChar << endl;
write(tty_fd,&ctrlChar,1);
cout << "sent to audrino.\n";
scanf("%u", ctrlInt);
}
cout << "done!" << endl;
close(tty_fd);
return 0;
}
现在,如果这看起来很正常,我将提交一份错误报告。
系统规格:最新的 Arch linux 64 位,使用“g++ -g -Wall code.c”编译
【问题讨论】:
-
您是否尝试检查核心转储?
-
它在技术上成功退出,所以我不知道如何从中提取核心转储。编程 5 年,我从未见过一个程序这样做。我很茫然。
标签: c++ terminal serial-port arduino