Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
serial2
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cs
ds
serial2
Commits
934a5159
Commit
934a5159
authored
1 year ago
by
Alessio Igor Bogani
Browse files
Options
Downloads
Patches
Plain Diff
Make code more reliable
parent
9db88dde
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/Serial2.cpp
+25
-16
25 additions, 16 deletions
src/Serial2.cpp
src/Serial2.h
+1
-1
1 addition, 1 deletion
src/Serial2.h
with
26 additions
and
17 deletions
src/Serial2.cpp
+
25
−
16
View file @
934a5159
...
@@ -536,8 +536,12 @@ void Serial2::write(const Tango::DevVarCharArray *argin)
...
@@ -536,8 +536,12 @@ void Serial2::write(const Tango::DevVarCharArray *argin)
int
olength
;
int
olength
;
while
(
bytes_total
<
bytes_to_write
)
{
while
(
bytes_total
<
bytes_to_write
)
{
if
(
!
wait_for
(
WRITE
))
int
s
=
select
(
WRITE
);
if
(
s
==
0
)
goto
timeout
;
goto
timeout
;
else
if
(
s
<
0
)
goto
error
;
else
{
/* s > 0 */
}
ssize_t
bytes_written
=
_write
(
ssize_t
bytes_written
=
_write
(
fd
,
argin_data
.
data
()
+
bytes_total
,
fd
,
argin_data
.
data
()
+
bytes_total
,
...
@@ -861,8 +865,12 @@ void Serial2::_read(size_t bytes_to_read)
...
@@ -861,8 +865,12 @@ void Serial2::_read(size_t bytes_to_read)
size_t
bytes_total
=
data
.
size
();
size_t
bytes_total
=
data
.
size
();
while
(
bytes_total
<
bytes_to_read
)
{
while
(
bytes_total
<
bytes_to_read
)
{
if
(
!
wait_for
(
READ
))
int
s
=
select
(
READ
);
if
(
s
==
0
)
goto
timeout
;
goto
timeout
;
else
if
(
s
<
0
)
goto
error
;
else
{
/* s > 0 */
}
size_t
count
=
min
((
size_t
)
max
(
input_queue_length
(),
0
),
sizeof
(
buffer
));
size_t
count
=
min
((
size_t
)
max
(
input_queue_length
(),
0
),
sizeof
(
buffer
));
ssize_t
bytes_readed
=
::
read
(
fd
,
buffer
,
count
);
ssize_t
bytes_readed
=
::
read
(
fd
,
buffer
,
count
);
...
@@ -880,7 +888,7 @@ void Serial2::_read(size_t bytes_to_read)
...
@@ -880,7 +888,7 @@ void Serial2::_read(size_t bytes_to_read)
}
}
return
;
return
;
error
:
error
:
check_state
(
fals
e
);
check_state
(
tru
e
);
sleep
(
tout
);
sleep
(
tout
);
timeout
:
timeout
:
Tango
::
Except
::
throw_exception
(
Tango
::
Except
::
throw_exception
(
...
@@ -947,7 +955,7 @@ void Serial2::check_state(bool forcing)
...
@@ -947,7 +955,7 @@ void Serial2::check_state(bool forcing)
reconnections
+=
1
;
reconnections
+=
1
;
}
}
bool
Serial2
::
wait_for
(
event_type
et
)
int
Serial2
::
select
(
event_type
et
)
{
{
if
(
multiplexing
==
SLEEP
)
{
if
(
multiplexing
==
SLEEP
)
{
timeval
twait
;
timeval
twait
;
...
@@ -964,8 +972,8 @@ bool Serial2::wait_for(event_type et)
...
@@ -964,8 +972,8 @@ bool Serial2::wait_for(event_type et)
FD_ZERO
(
&
errorfds
);
FD_ZERO
(
&
errorfds
);
FD_ZERO
(
&
readfds
);
FD_ZERO
(
&
readfds
);
FD_ZERO
(
&
writefds
);
FD_ZERO
(
&
writefds
);
FD_SET
(
fd
,
&
errorfds
);
FD_SET
(
fd
,
&
errorfds
);
switch
(
et
)
{
switch
(
et
)
{
case
WRITE
:
case
WRITE
:
FD_SET
(
fd
,
&
writefds
);
FD_SET
(
fd
,
&
writefds
);
...
@@ -975,27 +983,28 @@ bool Serial2::wait_for(event_type et)
...
@@ -975,27 +983,28 @@ bool Serial2::wait_for(event_type et)
break
;
break
;
}
}
int
select_ret
=
select
(
fd
+
1
,
&
readfds
,
int
select_ret
=
::
select
(
fd
+
1
,
&
readfds
,
&
writefds
,
&
errorfds
,
&
tout
);
&
writefds
,
&
errorfds
,
&
tout
);
if
(
FD_ISSET
(
fd
,
&
errorfds
))
{
assert
(
false
);
return
-
1
;
}
if
(
select_ret
>
0
)
{
if
(
select_ret
>
0
)
{
if
(
FD_ISSET
(
fd
,
&
errorfds
))
return
false
;
if
(
et
==
READ
&&
FD_ISSET
(
fd
,
&
readfds
))
if
(
et
==
READ
&&
FD_ISSET
(
fd
,
&
readfds
))
return
true
;
return
select_ret
;
if
(
et
==
WRITE
&&
FD_ISSET
(
fd
,
&
writefds
))
if
(
et
==
WRITE
&&
FD_ISSET
(
fd
,
&
writefds
))
return
true
;
return
select_ret
;
assert
(
false
);
assert
(
false
);
return
false
;
return
-
1
;
}
else
if
(
select_ret
==
0
)
{
}
else
if
(
select_ret
==
0
)
{
return
false
;
return
select_ret
;
}
else
{
// select_ret < 0
}
else
{
// select_ret < 0
ERROR_STREAM
<<
"Select() error "
<<
select_ret
return
select_ret
;
<<
" not handled:"
<<
strerror
(
errno
)
<<
endl
;
return
false
;
}
}
assert
(
false
);
assert
(
false
);
return
false
;
return
-
1
;
}
}
void
Serial2
::
resolve
()
{}
void
Serial2
::
resolve
()
{}
...
...
This diff is collapsed.
Click to expand it.
src/Serial2.h
+
1
−
1
View file @
934a5159
...
@@ -268,7 +268,7 @@ public:
...
@@ -268,7 +268,7 @@ public:
ssize_t
_write
(
int
,
const
void
*
,
size_t
);
ssize_t
_write
(
int
,
const
void
*
,
size_t
);
void
_read
(
size_t
);
void
_read
(
size_t
);
void
check_state
(
bool
);
void
check_state
(
bool
);
bool
wait_for
(
event_type
);
int
select
(
event_type
);
void
resolve
();
void
resolve
();
/*----- PROTECTED REGION END -----*/
// Serial2::Additional Method prototypes
/*----- PROTECTED REGION END -----*/
// Serial2::Additional Method prototypes
};
};
...
...
This diff is collapsed.
Click to expand it.
Preview
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!
Save comment
Cancel
Please
register
or
sign in
to comment