Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
e4fb673b
Commit
e4fb673b
authored
Oct 08, 2011
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(ComPort): добавил функцию reopen(), для возможности "реинициализации" соединения "на ходу".
parent
c940d756
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
38 deletions
+61
-38
ComPort.h
include/ComPort.h
+6
-1
ComPort.cc
src/Communications/ComPort.cc
+55
-37
No files found.
include/ComPort.h
View file @
e4fb673b
...
...
@@ -67,7 +67,7 @@ public:
TwoBits
=
3
};
ComPort
(
std
::
string
comDevice
,
bool
nocreate
=
false
);
ComPort
(
const
std
::
string
comDevice
,
bool
nocreate
=
false
);
virtual
~
ComPort
();
void
setSpeed
(
Speed
s
);
...
...
@@ -95,7 +95,11 @@ public:
void
cleanupChannel
();
virtual
void
reopen
();
protected
:
void
openPort
();
static
const
int
BufSize
=
8192
;
unsigned
char
buf
[
BufSize
];
int
curSym
;
...
...
@@ -104,6 +108,7 @@ protected:
int
uTimeout
;
bool
waiting
;
Speed
speed
;
std
::
string
dev
;
virtual
unsigned
char
m_receiveByte
(
bool
wait
);
...
...
src/Communications/ComPort.cc
View file @
e4fb673b
...
...
@@ -27,52 +27,71 @@ using namespace std;
ComPort
::~
ComPort
()
{
// printf("Destructor\n");
tcsetattr
(
fd
,
TCSAFLUSH
,
&
oldTermios
);
close
(
fd
);
if
(
fd
>
0
)
{
tcsetattr
(
fd
,
TCSAFLUSH
,
&
oldTermios
);
close
(
fd
);
}
}
// --------------------------------------------------------------------------------
ComPort
::
ComPort
(
string
comDevice
,
bool
nocreate
)
:
curSym
(
0
),
bufLength
(
0
),
uTimeout
(
10000
),
waiting
(
true
)
ComPort
::
ComPort
(
const
string
comDevice
,
bool
nocreate
)
:
curSym
(
0
),
bufLength
(
0
),
fd
(
-
1
),
uTimeout
(
10000
),
waiting
(
true
)
,
dev
(
comDevice
)
{
if
(
!
nocreate
)
openPort
();
}
// --------------------------------------------------------------------------------
void
ComPort
::
openPort
()
{
struct
termios
options
;
fd
=
open
(
dev
.
c_str
(),
O_RDWR
|
O_NOCTTY
/*| O_NDELAY*/
);
if
(
fd
==
-
1
)
{
struct
termios
options
;
fd
=
open
(
comDevice
.
c_str
(),
O_RDWR
|
O_NOCTTY
/*| O_NDELAY*/
);
if
(
fd
==
-
1
)
{
string
strErr
=
"Unable to open "
+
comDevice
+
" [Error: "
+
strerror
(
errno
)
+
"]"
;
throw
UniSetTypes
::
SystemError
(
strErr
.
c_str
());
}
string
strErr
=
"Unable to open "
+
dev
+
" [Error: "
+
strerror
(
errno
)
+
"]"
;
throw
UniSetTypes
::
SystemError
(
strErr
.
c_str
());
}
/* Get the current options for the port */
tcgetattr
(
fd
,
&
options
);
/* Get the current options for the port */
tcgetattr
(
fd
,
&
options
);
oldTermios
=
options
;
oldTermios
=
options
;
cfsetispeed
(
&
options
,
B19200
);
/* Set the baud rates to 19200 */
cfsetospeed
(
&
options
,
B19200
);
cfsetispeed
(
&
options
,
B19200
);
/* Set the baud rates to 19200 */
cfsetospeed
(
&
options
,
B19200
);
cfmakeraw
(
&
options
);
options
.
c_lflag
&=
~
(
ICANON
|
ECHO
|
ECHOE
|
ECHOK
|
ECHONL
|
ISIG
|
IEXTEN
/*|NOFLUSH*/
|
TOSTOP
);
options
.
c_cflag
&=
~
CSTOPB
;
options
.
c_iflag
&=
~
(
IGNBRK
|
BRKINT
|
PARMRK
);
options
.
c_iflag
&=
~
(
INLCR
|
IGNCR
|
ICRNL
|
IUCLC
|
IMAXBEL
);
options
.
c_oflag
&=
~
OPOST
;
options
.
c_cflag
&=
~
CRTSCTS
;
options
.
c_cflag
&=
~
HUPCL
;
options
.
c_iflag
&=
~
(
IXON
|
IXOFF
|
IXANY
);
options
.
c_cc
[
VMIN
]
=
0
;
options
.
c_cc
[
VTIME
]
=
1
;
options
.
c_cflag
&=
~
CSIZE
;
options
.
c_cflag
|=
CS8
;
options
.
c_cflag
&=
~
PARENB
;
options
.
c_iflag
&=
~
INPCK
;
options
.
c_cflag
|=
(
CLOCAL
|
CREAD
);
cfmakeraw
(
&
options
);
options
.
c_lflag
&=
~
(
ICANON
|
ECHO
|
ECHOE
|
ECHOK
|
ECHONL
|
ISIG
|
IEXTEN
/*|NOFLUSH*/
|
TOSTOP
);
options
.
c_cflag
&=
~
CSTOPB
;
options
.
c_iflag
&=
~
(
IGNBRK
|
BRKINT
|
PARMRK
);
options
.
c_iflag
&=
~
(
INLCR
|
IGNCR
|
ICRNL
|
IUCLC
|
IMAXBEL
);
options
.
c_oflag
&=
~
OPOST
;
options
.
c_cflag
&=
~
CRTSCTS
;
options
.
c_cflag
&=
~
HUPCL
;
options
.
c_iflag
&=
~
(
IXON
|
IXOFF
|
IXANY
);
options
.
c_cc
[
VMIN
]
=
0
;
options
.
c_cc
[
VTIME
]
=
1
;
options
.
c_cflag
&=
~
CSIZE
;
options
.
c_cflag
|=
CS8
;
options
.
c_cflag
&=
~
PARENB
;
options
.
c_iflag
&=
~
INPCK
;
options
.
c_cflag
|=
(
CLOCAL
|
CREAD
);
tcsetattr
(
fd
,
TCSAFLUSH
,
&
options
);
tcsetattr
(
fd
,
TCSAFLUSH
,
&
options
);
}
// --------------------------------------------------------------------------------
void
ComPort
::
reopen
()
{
if
(
fd
>
0
)
{
struct
termios
options
;
tcgetattr
(
fd
,
&
options
);
tcsetattr
(
fd
,
TCSAFLUSH
,
&
oldTermios
);
close
(
fd
);
openPort
();
if
(
fd
>
0
)
tcsetattr
(
fd
,
TCSAFLUSH
,
&
options
);
}
}
// --------------------------------------------------------------------------------
...
...
@@ -87,7 +106,6 @@ void ComPort::setSpeed( Speed s )
cfsetospeed
(
&
options
,
speed
);
tcsetattr
(
fd
,
TCSADRAIN
,
&
options
);
}
// --------------------------------------------------------------------------------
void
ComPort
::
setParity
(
Parity
parity
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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