0
我想通過串行從Ubuntu 12.04到使用libserial ATxmega板進行通信。我遇到的問題是我的代碼無法運行,除非我先運行cutecom。除非我先運行cutecom,否則我的程序會凍結而不輸出任何內容。我嘗試了幾件事情,比如讓我sudo訪問ttyUSB0,將我的用戶添加到對話組中。像su一樣運行它。我也嘗試添加VTime和VMin,但都沒有運氣。這裏是我的代碼,程序沒有離開,它似乎永遠不會進入while(ros :: ok())循環。我正在使用ros,但它在這裏應該沒有效果。我試圖以二分之一的比例與董事會來回溝通。LibSerial C++代碼不運行沒有Cutecom
#include "ros/ros.h"
#include <SerialStream.h>
#include <iostream>
#include <math.h>
using namespace LibSerial;
SerialStream mySerial;
void processSonar(char read[]);
char motor[] = {'s','3','6','0','0'};
unsigned int writeServo = 3600;
int main(int argc, char ** argv)
{
ros::init(argc, argv, "mySerial");
ros::NodeHandle n;
std::string serial = "/dev/ttyUSB0";
mySerial.Open(serial);
mySerial.SetBaudRate(SerialStreamBuf::BAUD_57600);
mySerial.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
mySerial.SetNumOfStopBits(1);
mySerial.SetParity(SerialStreamBuf::PARITY_NONE);
mySerial.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
mySerial.SetVTime(1);
mySerial.SetVMin(60);
while(!mySerial.IsOpen()){
mySerial.Open(serial);
std::cout << "trying to open port" << std::endl;
usleep(100000);
}
// if(!mySerial.good()){
// std::cerr << "Serial not Good"
// << std::endl;
// exit(1);
// }
char read[12];
char* SerialP = read;
char* motorP = motor;
while(ros::ok())
{
mySerial.write(motorP, sizeof(motor));
if(mySerial.rdbuf()->in_avail() > 0)
{
mySerial.read(SerialP, sizeof(read));
std::cout << read << std::endl;
processSonar(read);
//std::cout << motor << writeServo << std::endl;
}
usleep(100000);
}
std::cout << "Communication Failure"<< std::endl;
return 0;
}