修复串口有时打不开的bug。

This commit is contained in:
xinyang
2019-05-10 19:39:24 +08:00
parent 156e4e0861
commit bc1707961c

View File

@@ -7,33 +7,51 @@
#include <options/options.h> #include <options/options.h>
#include <log.h> #include <log.h>
using std::cout; using namespace std;
using std::cerr;
using std::clog;
using std::dec;
using std::endl;
using std::hex;
GMAngle_t aim; GMAngle_t aim;
string get_uart_dev_name(){
FILE* ls = popen("ls /dev/ttyUSB* --color=never", "r");
char name[20] = {0};
fscanf(ls, "%s", name);
return name;
}
Uart::Uart(){ Uart::Uart(){
if(wait_uart){ if(wait_uart){
while((fd = open("/dev/ttyUSB0", O_RDWR)) < 0); string name;
do{
name = get_uart_dev_name();
if(name == ""){
continue;
}
}while((fd=open(name.data(), O_RDWR)) < 0);
}else{ }else{
fd = open("/dev/ttyUSB0", O_RDWR); string name = get_uart_dev_name();
if(name == ""){
cerr<<"open port error"<<endl;
return;
}
fd = open(name.data(), O_RDWR);
if(fd < 0) { if(fd < 0) {
cerr<<"open port error"<<endl; cerr<<"open port error"<<endl;
return; return;
} }
} }
// fd = open("/dev/ttyUSB1", O_RDWR);
// if(fd < 0) {
// cerr<<"open port error"<<endl;
// return;
// }
if(set_opt(fd, 115200, 8, 'N', 1) < 0 ) if(set_opt(fd, 115200, 8, 'N', 1) < 0 )
{ {
cerr<<"set opt error"<<endl; cerr<<"set opt error"<<endl;
return; return;
} }
cout<<"uart port success"<<endl; cout << "uart port success" << endl;
buff[0] = 's'; buff[0] = 's';
buff[1] = '+'; buff[1] = '+';