Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
comm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
labrobotica
drivers
comm
Commits
509b0871
Commit
509b0871
authored
9 years ago
by
Sergi Hernandez
Browse files
Options
Downloads
Patches
Plain Diff
Chnages to allow non-standard baudrates.
parent
d961fc9f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/serial/rs232.cpp
+55
-90
55 additions, 90 deletions
src/serial/rs232.cpp
src/serial/rs232.h
+1
-1
1 addition, 1 deletion
src/serial/rs232.h
with
56 additions
and
91 deletions
src/serial/rs232.cpp
+
55
−
90
View file @
509b0871
...
@@ -14,82 +14,38 @@ CRS232::CRS232(const std::string& comm_id) : CComm(comm_id)
...
@@ -14,82 +14,38 @@ CRS232::CRS232(const std::string& comm_id) : CComm(comm_id)
void
CRS232
::
set_baudrate
(
int
baud
)
void
CRS232
::
set_baudrate
(
int
baud
)
{
{
struct
termios
config
;
struct
termios
2
config
;
int
baudrate
;
int
error
;
if
(
tcgetattr
(
this
->
serial_fd
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCGETS2
,
&
config
)
)
<
0
)
{
{
/* handle exceptions */
/* handle exceptions */
throw
CRS232Exception
(
_HERE_
,
"Impossible to
get the attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to get the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
else
else
{
{
switch
(
baud
)
config
.
c_cflag
&=
~
CBAUD
;
{
config
.
c_cflag
|=
BOTHER
;
case
50
:
baudrate
=
B50
;
config
.
c_ispeed
=
baud
;
break
;
config
.
c_ospeed
=
baud
;
case
75
:
baudrate
=
B75
;
break
;
case
110
:
baudrate
=
B110
;
break
;
case
134
:
baudrate
=
B134
;
break
;
case
150
:
baudrate
=
B150
;
break
;
case
200
:
baudrate
=
B200
;
break
;
case
300
:
baudrate
=
B300
;
break
;
case
600
:
baudrate
=
B600
;
break
;
case
1200
:
baudrate
=
B1200
;
break
;
case
1800
:
baudrate
=
B1800
;
break
;
case
2400
:
baudrate
=
B2400
;
break
;
case
4800
:
baudrate
=
B4800
;
break
;
case
9600
:
baudrate
=
B9600
;
break
;
case
19200
:
baudrate
=
B19200
;
break
;
case
38400
:
baudrate
=
B38400
;
break
;
case
57600
:
baudrate
=
B57600
;
break
;
case
115200
:
baudrate
=
B115200
;
break
;
case
230400
:
baudrate
=
B230400
;
break
;
case
460800
:
baudrate
=
B460800
;
break
;
case
921600
:
baudrate
=
B921600
;
break
;
default:
/* handle exception */
throw
CRS232Exception
(
_HERE_
,
"Invalid baudrate. See the documentation for the possible values.
\n
"
,
this
->
comm_id
);
break
;
}
cfsetispeed
(
&
config
,
baudrate
);
cfsetospeed
(
&
config
,
baudrate
);
this
->
serial_config
.
baud
=
baud
;
this
->
serial_config
.
baud
=
baud
;
if
(
tcsetattr
(
this
->
serial_fd
,
TCS
ANOW
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCS
ETS2
,
&
config
)
)
<
0
)
{
{
/* handle exceptions */
/* handle exceptions */
throw
CRS232Exception
(
_HERE_
,
"Impossible to
set up the new attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to set the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
}
}
}
}
void
CRS232
::
set_num_bits
(
char
num_bits
)
void
CRS232
::
set_num_bits
(
char
num_bits
)
{
{
struct
termios
config
;
struct
termios
2
config
;
int
bits
;
int
error
,
bits
;
if
(
tcgetattr
(
this
->
serial_fd
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCGETS2
,
&
config
)
)
<
0
)
{
{
/* handle exceptions */
/* handle exceptions */
throw
CRS232Exception
(
_HERE_
,
"Impossible to
get the attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to get the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
else
else
{
{
...
@@ -110,22 +66,23 @@ void CRS232::set_num_bits(char num_bits)
...
@@ -110,22 +66,23 @@ void CRS232::set_num_bits(char num_bits)
config
.
c_cflag
&=~
CSIZE
;
config
.
c_cflag
&=~
CSIZE
;
config
.
c_cflag
|=
bits
;
config
.
c_cflag
|=
bits
;
this
->
serial_config
.
num_bits
=
num_bits
;
this
->
serial_config
.
num_bits
=
num_bits
;
if
(
tcsetattr
(
this
->
serial_fd
,
TCS
ANOW
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCS
ETS2
,
&
config
)
)
<
0
)
{
{
/* handle exceptions */
/* handle exceptions */
throw
CRS232Exception
(
_HERE_
,
"Impossible to
set up the new attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to set the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
}
}
}
}
void
CRS232
::
set_parity
(
parity_type
parity
)
void
CRS232
::
set_parity
(
parity_type
parity
)
{
{
struct
termios
config
;
struct
termios2
config
;
int
error
;
if
(
tcgetattr
(
this
->
serial_fd
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCGETS2
,
&
config
)
)
<
0
)
{
{
/* handle exception */
/* handle exception
s
*/
throw
CRS232Exception
(
_HERE_
,
"Impossible to
get the attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to get the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
else
else
{
{
...
@@ -142,23 +99,24 @@ void CRS232::set_parity(parity_type parity)
...
@@ -142,23 +99,24 @@ void CRS232::set_parity(parity_type parity)
}
}
else
config
.
c_cflag
&=~
PARENB
;
else
config
.
c_cflag
&=~
PARENB
;
this
->
serial_config
.
parity
=
parity
;
this
->
serial_config
.
parity
=
parity
;
if
(
tcsetattr
(
this
->
serial_fd
,
TCS
ANOW
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCS
ETS2
,
&
config
)
)
<
0
)
{
{
/* handle exceptions */
/* handle exceptions */
throw
CRS232Exception
(
_HERE_
,
"Impossible to
set up the new attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to set the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
}
}
}
}
void
CRS232
::
set_stop_bits
(
char
stop_bits
)
void
CRS232
::
set_stop_bits
(
char
stop_bits
)
{
{
struct
termios
config
;
struct
termios2
config
;
int
error
;
if
(
tcgetattr
(
this
->
serial_fd
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCGETS2
,
&
config
)
)
<
0
)
{
{
/* handle exceptions */
/* handle exceptions */
throw
CRS232Exception
(
_HERE_
,
"Impossible to
get the attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to get the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
else
else
{
{
if
(
stop_bits
==
2
)
config
.
c_cflag
|=
CSTOPB
;
if
(
stop_bits
==
2
)
config
.
c_cflag
|=
CSTOPB
;
...
@@ -169,10 +127,10 @@ void CRS232::set_stop_bits(char stop_bits)
...
@@ -169,10 +127,10 @@ void CRS232::set_stop_bits(char stop_bits)
throw
CRS232Exception
(
_HERE_
,
"Invalid number of stop bits. See the documentation for the possible values.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Invalid number of stop bits. See the documentation for the possible values.
\n
"
,
this
->
comm_id
);
}
}
this
->
serial_config
.
stop_bits
=
stop_bits
;
this
->
serial_config
.
stop_bits
=
stop_bits
;
if
(
tcsetattr
(
this
->
serial_fd
,
TCS
ANOW
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCS
ETS2
,
&
config
)
)
<
0
)
{
{
/* handle exception */
/* handle exception
s
*/
throw
CRS232Exception
(
_HERE_
,
"Impossible to
set up the new attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to set the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
}
}
}
}
...
@@ -180,7 +138,8 @@ void CRS232::set_stop_bits(char stop_bits)
...
@@ -180,7 +138,8 @@ void CRS232::set_stop_bits(char stop_bits)
void
CRS232
::
hard_open
(
void
*
comm_dev
)
void
CRS232
::
hard_open
(
void
*
comm_dev
)
{
{
std
::
string
*
serial_dev
=
(
std
::
string
*
)
comm_dev
;
std
::
string
*
serial_dev
=
(
std
::
string
*
)
comm_dev
;
struct
termios
config
;
struct
termios2
config
;
int
error
;
if
(
serial_dev
->
size
()
==
0
)
if
(
serial_dev
->
size
()
==
0
)
{
{
...
@@ -197,10 +156,10 @@ void CRS232::hard_open(void *comm_dev)
...
@@ -197,10 +156,10 @@ void CRS232::hard_open(void *comm_dev)
}
}
else
else
{
{
if
(
tcgetattr
(
this
->
serial_fd
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCGETS2
,
&
config
)
)
<
0
)
{
{
/* handle exceptions */
/* handle exceptions */
throw
CRS232Exception
(
_HERE_
,
"Impossible to
get the attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to get the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
else
else
{
{
...
@@ -213,10 +172,10 @@ void CRS232::hard_open(void *comm_dev)
...
@@ -213,10 +172,10 @@ void CRS232::hard_open(void *comm_dev)
config
.
c_cc
[
VMIN
]
=
0
;
config
.
c_cc
[
VMIN
]
=
0
;
config
.
c_cc
[
VTIME
]
=
100
;
config
.
c_cc
[
VTIME
]
=
100
;
this
->
serial_device
=*
serial_dev
;
this
->
serial_device
=*
serial_dev
;
if
(
tcsetattr
(
this
->
serial_fd
,
TCS
ANOW
,
&
config
)
==-
1
)
if
(
(
error
=
ioctl
(
this
->
serial_fd
,
TCS
ETS2
,
&
config
)
)
<
0
)
{
{
/* handle exceptions */
/* handle exceptions */
throw
CRS232Exception
(
_HERE_
,
"Impossible to
set up the new attribute
structure.
\n
"
,
this
->
comm_id
);
throw
CRS232Exception
(
_HERE_
,
"Impossible to
execute the IOCTL to set the configuration
structure.
\n
"
,
this
->
comm_id
);
}
}
}
}
}
}
...
@@ -249,7 +208,13 @@ int CRS232::hard_write(unsigned char *data, int len)
...
@@ -249,7 +208,13 @@ int CRS232::hard_write(unsigned char *data, int len)
{
{
int
num_written
=
0
;
int
num_written
=
0
;
tcdrain
(
this
->
serial_fd
);
if
(
ioctl
(
this
->
serial_fd
,
TCSBRK
,
1
)
!=
0
)
{
// static int tcsb_warned=0;
// if (!tcsb_warned) fprintf(stderr, "TCSBRK doesn't work!\n");
// tcsb_warned=1;
}
//tcdrain(this->serial_fd);
if
((
num_written
=::
write
(
this
->
serial_fd
,
data
,
len
))
==-
1
)
if
((
num_written
=::
write
(
this
->
serial_fd
,
data
,
len
))
==-
1
)
{
{
return
-
1
;
return
-
1
;
...
...
This diff is collapsed.
Click to expand it.
src/serial/rs232.h
+
1
−
1
View file @
509b0871
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
#include
<sys/types.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<sys/stat.h>
#include
<sys/time.h>
#include
<sys/time.h>
#include
<term
io
s.h>
#include
<
asm/
term
bit
s.h>
#include
<unistd.h>
#include
<unistd.h>
#include
<fcntl.h>
#include
<fcntl.h>
#include
<errno.h>
#include
<errno.h>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment