Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Achim Morschhauser
GeomagLogger
Commits
c13a34d2
Commit
c13a34d2
authored
Nov 08, 2018
by
Achim Morschhauser
Browse files
Automatically find baud rate improved
parent
9be33a52
Changes
5
Hide whitespace changes
Inline
Side-by-side
include/Driver_Obs_Serial.hpp
View file @
c13a34d2
...
...
@@ -83,12 +83,16 @@ int waitanswer(double timeout);
/** Flush the buffer of the serial port. */
virtual
int
flush
();
/** Initialize the instrument with automatic determination of the baud rate */
virtual
int
set_baud
(
char
*
init_chars
[],
int
init_chars_num
);
/** Initialize the instrument with automatic determination of the baud rate */
virtual
int
set_baud
(
const
char
*
init_chars
);
/** Initialize the instrument with given baud rate */
/** Automatic determination of the baud rate */
virtual
int
find_baud
(
char
*
init_chars
[],
int
init_chars_num
);
/** Automatic determination of the baud rate */
virtual
int
find_baud
(
const
char
*
init_chars
);
/** Automatic determination of the baud rate */
virtual
int
find_baud
(
Driver_Obs_Serial
*
context
,
int
(
Driver_Obs_Serial
::*
check_baud
)(
int
));
/** Set given baud rate */
virtual
int
set_baud
(
int
baud
);
/** Check if baud rate is correct */
virtual
int
check_baud
(
int
baud
);
/** Set the termination characters. */
virtual
int
set_term
(
const
char
*
term
,
int
termlen
);
...
...
src/Driver_Obs_Serial.cpp
View file @
c13a34d2
...
...
@@ -180,12 +180,12 @@ int Driver_Obs_Serial::set_baud(int baud) {
*
*
******************************************************************************/
int
Driver_Obs_Serial
::
set
_baud
(
const
char
*
init_chars
)
{
int
Driver_Obs_Serial
::
find
_baud
(
const
char
*
init_chars
)
{
//! The init string
char
*
tmp
[]
=
{(
char
*
)
init_chars
};
return
(
set
_baud
(
tmp
,
1
));
return
(
find
_baud
(
tmp
,
1
));
}
...
...
@@ -195,7 +195,7 @@ int Driver_Obs_Serial::set_baud(const char* init_chars) {
* Several init strings are passed.
*
******************************************************************************/
int
Driver_Obs_Serial
::
set
_baud
(
char
*
init_chars
[],
int
init_chars_num
)
{
int
Driver_Obs_Serial
::
find
_baud
(
char
*
init_chars
[],
int
init_chars_num
)
{
////////////////////////////////////////////////////////////////////////
//
...
...
@@ -204,7 +204,7 @@ int Driver_Obs_Serial::set_baud(char* init_chars[], int init_chars_num) {
////////////////////////////////////////////////////////////////////////
int
baud
=
0
;
char
buf
[
100
];
char
buf
[
100
]
=
""
;
int
*
valid_baudrates
;
int
valid_baudrates_size
;
...
...
@@ -217,7 +217,7 @@ int Driver_Obs_Serial::set_baud(char* init_chars[], int init_chars_num) {
//printf("---- Test baud rate: %7d ----\n", valid_baudrates[i]);
serial
.
set_baud
(
valid_baudrates
[
i
]);
usleep
(
10000
);
usleep
(
10000
0
);
for
(
int
j
=
0
;
j
<
init_chars_num
-
1
;
j
++
){
//printf("BAUD: %s\n",init_chars[j]);
...
...
@@ -227,7 +227,7 @@ int Driver_Obs_Serial::set_baud(char* init_chars[], int init_chars_num) {
}
send
(
init_chars
[
init_chars_num
-
1
]);
if
(
serial
.
receive
(
buf
,
sizeof
(
buf
),
1
)
>
=
0
)
{
if
(
waitanswer
(
1
)
=
=
0
)
{
baud
=
valid_baudrates
[
i
];
break
;
}
...
...
@@ -254,3 +254,60 @@ int Driver_Obs_Serial::set_baud(char* init_chars[], int init_chars_num) {
}
}
/*******************************************************************************
*
* Initialize the Serial Instrument with auto-determination of baud rate.
* Test function is passed.
*
******************************************************************************/
int
Driver_Obs_Serial
::
find_baud
(
Driver_Obs_Serial
*
context
,
int
(
Driver_Obs_Serial
::*
check_baud
)(
int
))
{
////////////////////////////////////////////////////////////////////////
//
// Try to auto-determine the baud rate
//
////////////////////////////////////////////////////////////////////////
int
baud
=
0
;
char
buf
[
100
]
=
""
;
int
*
valid_baudrates
;
int
valid_baudrates_size
;
// Get the valid baudrates from the serial driver
serial
.
get_baudrates
(
&
valid_baudrates
,
&
valid_baudrates_size
);
// Try different baudrates
for
(
int
i
=
0
;
i
<
valid_baudrates_size
;
i
++
)
{
//printf("---- Test baud rate: %7d ----\n", valid_baudrates[i]);
if
(
context
->
check_baud
(
valid_baudrates
[
i
])
==
1
)
{
baud
=
valid_baudrates
[
i
];
break
;
}
/* This has to be in loop as characters received at false baud rate
somehow can corrupt the value in valid_baudrates_size (Buffer overrun
in driver_serial::receive */
serial
.
get_baudrates
(
&
valid_baudrates
,
&
valid_baudrates_size
);
//printf(" %d\n",valid_baudrates_size);
}
// Check if baud rate could be determined
if
(
baud
==
0
)
{
fprintf
(
stderr
,
"==== Baud rate could not be determined. ====
\n
"
);
return
(
-
1
);
}
else
{
fprintf
(
stderr
,
"==== Baud rate set to %7d ====
\n
"
,
baud
);
return
(
0
);
}
}
int
Driver_Obs_Serial
::
check_baud
(
int
baud
){
fprintf
(
stderr
,
"WRONG
\n
"
);
return
(
0
);
}
src/driver_obs_gsm.cpp
View file @
c13a34d2
...
...
@@ -36,7 +36,7 @@ driver_obs_gsm::driver_obs_gsm(std::string port, buffer_obs* buffer,
Driver_Obs_Serial
(
port
,
buffer
)
{
set_term
();
set
_baud
(
init_char
);
find
_baud
(
init_char
);
}
...
...
src/driver_obs_obsdaq.cpp
View file @
c13a34d2
...
...
@@ -74,6 +74,7 @@ driver_obs_obsdaq::driver_obs_obsdaq(std::string port, buffer_obs* buffer,
// Check for baud rate with the right address.
// Stop any running measurement when doing so.
find_baud
();
stop
();
// Initialize the OBSDAQ
init
();
...
...
@@ -120,7 +121,9 @@ driver_obs_obsdaq::driver_obs_obsdaq(std::string port, int baud,
//stop();
if
(
check_baud
(
baud
)
!=
1
){
fprintf
(
stderr
,
"ERROR"
);
find_baud
();
stop
();
set_baud
(
baud
);
}
// Initialize
...
...
@@ -605,19 +608,19 @@ int driver_obs_obsdaq::checked_send(const char* cmd, const char* reply,
}
/****************************************************************************
*
*
* Send an OBSDAQ command to address of serial port and check reply
*
*
*
* IN:
*
* - cmd
const char*
Command to send
*
* - reply
const char*
Expected reply
*
* - digits int
Number of 'digits' first characters
*
* that are checked to match the received
*
* answer.
*
* - timeout double
Time in [s] to wait for answer
*
* - recv
std::string* Received answer
*
* - verbose int
Print error messages
*
*
*
*
* Send an OBSDAQ command to address of serial port and check reply
*
* IN:
* - cmd const char* Command to send
* - reply const char* Expected reply
* - digits int Number of 'digits' first characters
*
that are checked to match the received
*
answer.
* - timeout double Time in [s] to wait for answer
* - recv std::string* Received answer
* - verbose int Print error messages
*
***************************************************************************/
int
driver_obs_obsdaq
::
checked_send
(
const
char
*
cmd
,
const
char
*
reply
,
int
digits
,
double
timeout
,
std
::
string
*
recv
,
int
verbose
)
{
...
...
@@ -643,7 +646,7 @@ int driver_obs_obsdaq::checked_send(const char* cmd, const char* reply,
// Send the command
send
(
cmd
);
// Wait for an answer
is received
// Wait for an answer
if
(
receive
(
buf
,
sizeof
(
buf
),
timeout
)
<
0
)
{
if
(
verbose
)
{
fprintf
(
stderr
,
"Reply not received for cmd: %s
\n
"
,
cmd
);
...
...
@@ -1170,20 +1173,12 @@ int driver_obs_obsdaq::set_baud(int baud) {
*
***************************************************************************/
int
driver_obs_obsdaq
::
find_baud
()
{
const
char
*
cmds
[]
=
{
"||||||||||||||||||||||||||||||||||||||||||||||||"
,
"||||||||||||||||||||||||||||||||||||||||||||||||"
,
"||||||||||||||||||||||||||||||||||||||||||||||||"
,
"||||||||||||||||||||||||||||||||||||||||||||||||"
,
"||||||||||||||||||||||||||||||||||||||||||||||||"
,
"||||||||||||||||||||||||||||||||||||||||||||||||"
,
"||||||||||||||||||||||||||||||||||||||||||||||||"
,
"||||||||||||||||||||||||||||||||||||||||||||||||"
,
"$M"
,
"$M"
};
Driver_Obs_Serial
::
set_baud
((
char
**
)
cmds
,
10
);
// Try to find serial port
Driver_Obs_Serial
::
find_baud
(
this
,
static_cast
<
int
(
Driver_Obs_Serial
::*
)
(
int
)
>
(
&
driver_obs_obsdaq
::
check_baud
));
}
...
...
@@ -1194,6 +1189,8 @@ int driver_obs_obsdaq::find_baud() {
***************************************************************************/
int
driver_obs_obsdaq
::
check_baud
(
int
baud
){
fprintf
(
stderr
,
"----- CHECK BAUD -----
\n
"
);
////////////////////////////////////////////////////////////////////////
//
// Set the baud rate of serial port
...
...
@@ -1208,15 +1205,21 @@ int driver_obs_obsdaq::check_baud(int baud){
// Check for response
//
////////////////////////////////////////////////////////////////////////
// Send test command
send
(
"$M"
);
// Wait for answer
if
(
waitanswer
(
2
)
!=
0
)
{
return
(
-
1
);
}
else
{
return
(
1
);
// Check for expected response
if
(
checked_send
(
"$M"
,
"<ObsDaq"
,
7
,
1
,
0
)
!=
0
)
{
// No response, but freerun mode might be running, so check for data
int
n
=
0
;
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
if
(
waitanswer
(
0.4
)
==
0
)
n
++
;
}
fprintf
(
stderr
,
"N: %d
\n
"
,
n
);
if
(
n
<
3
)
{
return
(
-
1
);
}
}
return
(
1
);
}
src/driver_serial.cpp
View file @
c13a34d2
...
...
@@ -178,122 +178,112 @@ int driver_serial::receive(char *buf, int buflen, double timeout) {
int
driver_serial
::
receive
(
char
*
buf
,
int
buflen
,
double
timeout
,
struct
timespec
*
recv_time
)
{
//////////////////////////////////////////////////////////////////////////////
//
// Some Variables.
//
//////////////////////////////////////////////////////////////////////////////
// Received char
unsigned
char
c
;
// Counter for received termination characters
int
n
=
0
;
// Counter for received characters
int
i
=
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.
//
//////////////////////////////////////////////////////////////////////////////
// Empty the buffer
memset
(
buf
,
0
,
strlen
(
buf
));
// Flush the serial port
//flush();
//////////////////////////////////////////////////////////////////////////////
//
// Some Variables.
//
//////////////////////////////////////////////////////////////////////////////
// Timeout in nanoseconds
if
(
timeout
>
0
){
clock_gettime
(
CLOCK_REALTIME
,
&
start_time
);
// Received char
unsigned
char
c
;
}
// Counter for received termination characters
int
n
=
0
;
// Counter for received characters
int
i
=
0
;
// Counter for waiting time
int
nothing
=
0
;
// Repeat loop until all
termination characters
have been received
while
(
n
<
termlen
)
{
// Number of
termination characters
int
termlen
=
strlen
(
term
);
//
// We receive something. Let's check.
//
if
(
read
(
fd
,
&
c
,
1
)
>
0
)
{
// Starting time and current time
struct
timespec
start_time
,
end_time
;
//fprintf(stderr,"Received: %d %d %d %d\n",
// c,n,term[n],termlen);
// A correct termination character has arrived
if
(
c
==
term
[
n
])
{
n
++
;
buf
[
i
++
]
=
c
;
}
// A character was received
else
{
// Time of first received character is saved
if
(
i
==
0
)
{
clock_gettime
(
CLOCK_REALTIME
,
recv_time
);
}
// Reset termination check if not all characters are received in a row
if
(
n
>
0
)
{
i
-=
n
;
buf
[
i
]
=
0
;
n
=
0
;
}
// Save character to buffer
buf
[
i
++
]
=
c
;
}
// Check for buffer overrun
if
(
i
>=
buflen
)
{
flush
();
fprintf
(
stderr
,
"Warning: %d characters received."
"Buffer overrun.
\n
"
,
buflen
);
return
(
-
1
);
}
//////////////////////////////////////////////////////////////////////////////
//
// Receive data until termination characters arrive.
//
//////////////////////////////////////////////////////////////////////////////
}
// Empty the buffer
memset
(
buf
,
0
,
strlen
(
buf
));
//
// Nothing was received at serial port
//
// Flush the serial port
//flush();
// Timeout specified
else
if
(
timeout
>
0
)
{
// Timeout in nanoseconds
if
(
timeout
>
0
){
clock_gettime
(
CLOCK_REALTIME
,
&
start_time
);
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
);
}
// Repeat loop until all termination characters have been received
while
(
n
<
termlen
)
{
// No timeout (is VTIME in practice)
}
else
{
//
// 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
)
{
return
(
-
1
);
// A correct termination character has arrived
if
(
c
==
term
[
n
])
{
n
++
;
buf
[
i
++
]
=
c
;
}
// A character was received
else
{
// Time of first received character is saved
if
(
i
==
0
)
{
clock_gettime
(
CLOCK_REALTIME
,
recv_time
);
}
// Reset termination check if not all characters are received in a row
if
(
n
>
0
)
{
i
-=
n
;
buf
[
i
]
=
0
;
n
=
0
;
}
// Save character to buffer
buf
[
i
++
]
=
c
;
}
// Check for buffer overrun
if
(
i
>=
buflen
)
{
flush
();
fprintf
(
stderr
,
"Warning: %d characters received."
"Buffer overrun.
\n
"
,
buflen
);
return
(
-
1
);
}
}
}
}
}
//
// Receive was successfull if we arrive here.
//
//
// Receive was successfull if we arrive here.
//
// Set zero character correctly to ignore termination characters.
buf
[
i
-
n
]
=
0
;
// Set zero character correctly to ignore termination characters.
buf
[
i
-
n
]
=
0
;
// Flush the serial port
//flush();
// Flush the serial port
//flush();
return
(
0
);
return
(
0
);
}
...
...
@@ -311,7 +301,7 @@ int driver_serial::waitanswer(double timeout) {
//////////////////////////////////////////////////////////////////////////////
// Received char
unsigned
char
c
;
unsigned
char
c
=
0
;
// Counter for received termination characters
int
n
=
0
;
...
...
@@ -356,25 +346,27 @@ int driver_serial::waitanswer(double timeout) {
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);
//fprintf(stderr,"REC: %3d %3d\n",c,term[n]);
// A correct termination character has arrived
if
(
c
==
term
[
n
])
{
//fprintf(stderr,"TER: %d %d %d %d\n",c,term[n],n,termlen);
n
++
;
//fprintf(stderr,"TER: %d %d %d %d\n",c,term[n],n,termlen);
}
// 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);
// Reset termination check if not all characters are received
//
in a row
if
(
n
>
0
)
{
n
=
0
;
}
//fprintf(stderr,"REC: %d %c\n",c,c);
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment