Commit 6b2dc0cb authored by Achim Morschhauser's avatar Achim Morschhauser
Browse files

baud and buf initialization / waitanswer

parent 159b6941
......@@ -41,7 +41,7 @@ class driver_serial
// Serial Port configuration
/** Baudrate */
private: speed_t baud;
private: int baud;
/** Termination characters */
private: char* term;
......
......@@ -92,7 +92,7 @@ driver_obs_obsdaq::driver_obs_obsdaq(std::string port, int baud,
Driver_Obs_Serial(port, buffer, (Obs_Calibration*) cal) {
// Set the termination characters for serial port driver
driver_obs_obsdaq::set_term();
set_term();
// Set the RS485 address
RS485_addr = addr;
......@@ -116,8 +116,13 @@ driver_obs_obsdaq::driver_obs_obsdaq(std::string port, int baud,
*/
// Stop the measurement
stop();
//TODO Reactivate later, baud rate needs to be set when calling stop() !
//stop();
if ( check_baud(baud) != 1){
fprintf(stderr,"ERROR");
}
// Initialize
init();
......@@ -166,7 +171,7 @@ int driver_obs_obsdaq::get(data_obs_vector* data) {
struct timespec recv_time;
// Receive buffer
char buf[100];
char buf[100]="";
/////////////////////////////////////////////////////////////////////////
//
......@@ -224,7 +229,7 @@ int driver_obs_obsdaq::run(double freq) {
// The data
data_obs_vector data;
// The receive buffer
char buf[200];
char buf[200]="";
/////////////////////////////////////////////////////////////////////////
//
......@@ -291,7 +296,7 @@ int driver_obs_obsdaq::freerun(double freq) {
// The vector data
data_obs_vector data;
// The receive buffer
char buf[200];
char buf[200]="";
// Data parser function
int (driver_obs_obsdaq::*parse_data)(data_obs_vector* data, char* buf);
//std::function<int(data_obs_vector* data, char* buf)> &parse_data;
......@@ -380,7 +385,7 @@ int driver_obs_obsdaq::freerun(double freq) {
int driver_obs_obsdaq::stop() {
// Receive buffer
char buf[100];
char buf[100]="";
// Stop free-run mode
send("|||||||||||||||||||||||||||||||||||||||||");
......@@ -625,7 +630,7 @@ int driver_obs_obsdaq::checked_send(const char* cmd, const char* reply,
/////////////////////////////////////////////////////////////////////////
char buf[1000];
char buf[1000]="";
std::string strbuf;
std::string expected;
......@@ -1015,7 +1020,7 @@ int driver_obs_obsdaq::init() {
int driver_obs_obsdaq::init_run(double freq){
// The receive buffer
char buf[200];
char buf[200]="";
// A command to send
char cmd[20];
// Index of the selected frequency
......@@ -1146,6 +1151,8 @@ int driver_obs_obsdaq::set_baud(int baud) {
return(-1);
}
return (0);
////////////////////////////////////////////////////////////////////////
//
// Set the baud rate of serial port
......@@ -1154,9 +1161,7 @@ int driver_obs_obsdaq::set_baud(int baud) {
if (Driver_Obs_Serial::set_baud(baud) < 0) {
return (-1);
}
return (0);
}
/****************************************************************************
......@@ -1189,6 +1194,29 @@ int driver_obs_obsdaq::find_baud() {
***************************************************************************/
int driver_obs_obsdaq::check_baud(int baud){
return(0);
////////////////////////////////////////////////////////////////////////
//
// Set the baud rate of serial port
//
////////////////////////////////////////////////////////////////////////
if (Driver_Obs_Serial::set_baud(baud) < 0) {
return (-1);
}
}
\ No newline at end of file
////////////////////////////////////////////////////////////////////////
//
// Check for response
//
////////////////////////////////////////////////////////////////////////
// Send test command
send("$M");
// Wait for answer
if (waitanswer(2)!=0) {
return(-1);
} else {
return(1);
}
}
......@@ -56,6 +56,7 @@ driver_serial::driver_serial(std::string port) {
this->port = port;
init();
this->baud = 0;
}
......@@ -73,6 +74,7 @@ driver_serial::driver_serial(std::string port, const char* term,
this->port = port;
init();
this->baud = 0;
set_term(term, termlen);
}
......@@ -302,99 +304,90 @@ int driver_serial::receive(char *buf, int buflen, double timeout,
***************************************************************************/
int driver_serial::waitanswer(double timeout) {
//////////////////////////////////////////////////////////////////////////////
//
// Some Variables.
//
//////////////////////////////////////////////////////////////////////////////
// Received char
unsigned char c;
// Counter for received termination characters
int n = 0;
// Counter for waiting time
int nothing = 0;
// Number of termination characters
int termlen = strlen(term);
// Starting time and current time
struct timespec start_time, end_time;
//////////////////////////////////////////////////////////////////////////////
//
// Receive data until termination characters arrive.
//
//////////////////////////////////////////////////////////////////////////////
// Flush the serial port
flush();
// Timeout in nanoseconds
if (timeout>0){
clock_gettime(CLOCK_REALTIME, &start_time);
}
// Repeat loop until all termination characters have been received
while (n < termlen) {
//
// We receive something. Let's check.
//
if (read(fd, &c, 1) > 0) {
//printf("Received: %c %d %d %d %d\n",c,c,n,term[n],termlen);
// A correct termination character has arrived
if (c == term[n]) {
n++;
}
// A character was received
else {
// Reset termination check if not all characters are received in a row
if (n > 0) {
n = 0;
}
}
}
//
// Nothing was received at serial port
//
// Timeout specified
else if (timeout>0) {
clock_gettime(CLOCK_REALTIME, &end_time);
if (start_time.tv_nsec/1e9+timeout >=
(end_time.tv_sec-start_time.tv_sec)+end_time.tv_nsec/1e9) {
//flush();
fprintf(stderr, "Warning: Timeout. No response received.\n");
return (-1);
}
// No timeout (is VTIME in practice)
} else {
return(-1);
}
}
//
// Receive was successful if we arrive here.
//
// Flush the serial port
//flush();
return (0);
//////////////////////////////////////////////////////////////////////////////
//
// Some Variables.
//
//////////////////////////////////////////////////////////////////////////////
// Received char
unsigned char c;
// Counter for received termination characters
int n = 0;
// Counter for waiting time
int nothing = 0;
// Number of termination characters
int termlen = strlen(term);
// Starting time and current time
struct timespec start_time, end_time;
//////////////////////////////////////////////////////////////////////////////
//
// Receive data until termination characters arrive.
//
//////////////////////////////////////////////////////////////////////////////
// Flush the serial port
flush();
// Timeout in nanoseconds
if (timeout>0){
clock_gettime(CLOCK_REALTIME, &start_time);
}
// Repeat loop until all termination characters have been received
while (n < termlen) {
//
// Check for timeout
//
if (timeout > 0) {
clock_gettime(CLOCK_REALTIME, &end_time);
if ( (end_time.tv_sec-start_time.tv_sec)+end_time.tv_nsec/1e9 >=
start_time.tv_nsec/1e9+timeout ) {
//flush();
//fprintf(stderr,"TIMEOUT: %f %f\n",start_time.tv_nsec/1e9+timeout,
// (end_time.tv_sec-start_time.tv_sec)+end_time.tv_nsec/1e9);
fprintf(stderr, "Warning: Timeout. No response received.\n");
return (-1);
}
}
//
// We receive something. Let's check.
//
if (read(fd, &c, 1) > 0) {
//printf("Received: %c %d %d %d %d\n",c,c,n,term[n],termlen);
// A correct termination character has arrived
if (c == term[n]) {
//fprintf(stderr,"TER: %d %d %d %d\n",c,term[n],n,termlen);
n++;
}
// A character was received
else {
// Reset termination check if not all characters are received in a row
//fprintf(stderr,"REC: %d %c\n",c,c);
if (n > 0) {
n = 0;
}
}
}
}
//
// Receive was successful if we arrive here.
//
// Flush the serial port
//flush();
return (0);
}
......@@ -464,7 +457,6 @@ int driver_serial::set_baud(int baud) {
// Baud rate
speed_t tc_baud;
this->baud = baud;
///////////////////////////////////////////////////////////////////////////////
//
......@@ -500,6 +492,12 @@ int driver_serial::set_baud(int baud) {
//
///////////////////////////////////////////////////////////////////////////////
//
// Current baud rate variable
//
this->baud = baud;
//
// Hash table
//
......
......@@ -377,10 +377,7 @@ int driver_serial::set_baud(int baud){
int i;
/* Baud rate */
speed_t tc_baud;
this->baud=tc_baud;
this->baud=baud;
/* Check if set_serial was already called */
if (valid_baudrates_size == 0){
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment