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
4f355923
Commit
4f355923
authored
Aug 18, 2016
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(MBTCPMaster): попытка использовать select() вместо poll()
parent
b93be6d3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
4 deletions
+43
-4
ModbusTCPMaster.h
include/modbus/ModbusTCPMaster.h
+7
-0
ModbusTCPMaster.cc
src/Communications/Modbus/ModbusTCPMaster.cc
+36
-4
No files found.
include/modbus/ModbusTCPMaster.h
View file @
4f355923
...
...
@@ -56,6 +56,9 @@ class ModbusTCPMaster:
virtual
ModbusRTU
::
mbErrCode
query
(
ModbusRTU
::
ModbusAddr
addr
,
ModbusRTU
::
ModbusMessage
&
msg
,
ModbusRTU
::
ModbusMessage
&
reply
,
timeout_t
timeout
)
override
;
bool
waitInput
(
int
timeout_msec
);
bool
waitOutput
(
int
timeout_msec
);
private
:
//ost::TCPStream* tcp;
std
::
shared_ptr
<
UTCPStream
>
tcp
;
...
...
@@ -66,6 +69,10 @@ class ModbusTCPMaster:
int
port
=
{
0
};
bool
force_disconnect
=
{
false
};
int
keepAliveTimeout
=
{
1000
};
// структура для реализации использования select
fd_set
s_set
;
timeval
s_timeout
;
};
// -------------------------------------------------------------------------
#endif // ModbusTCPMaster_H_
...
...
src/Communications/Modbus/ModbusTCPMaster.cc
View file @
4f355923
...
...
@@ -116,9 +116,10 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
msg
.
makeHead
(
++
nTransaction
,
crcNoCheckit
);
for
(
unsigned
in
t
i
=
0
;
i
<
2
;
i
++
)
for
(
size_
t
i
=
0
;
i
<
2
;
i
++
)
{
if
(
tcp
->
isPending
(
ost
::
Socket
::
pendingOutput
,
timeout
)
)
//if( tcp->isPending(ost::Socket::pendingOutput, timeout) )
if
(
waitOutput
(
timeout
)
)
{
mbErrCode
res
=
send
(
msg
);
...
...
@@ -165,7 +166,8 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
tcp
->
sync
();
//reply.clear();
if
(
tcp
->
isPending
(
ost
::
Socket
::
pendingInput
,
timeout
)
)
//if( tcp->isPending(ost::Socket::pendingInput, timeout) )
if
(
waitInput
(
timeout
)
)
{
size_t
ret
=
0
;
...
...
@@ -176,7 +178,8 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
if
(
ret
==
sizeof
(
reply
.
aduhead
)
)
break
;
if
(
!
tcp
->
isPending
(
ost
::
Socket
::
pendingInput
,
timeout
)
)
//if( !tcp->isPending(ost::Socket::pendingInput, timeout) )
if
(
!
waitInput
(
timeout
)
)
break
;
}
...
...
@@ -317,6 +320,35 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
return
erTimeOut
;
// erHardwareError
}
// -------------------------------------------------------------------------
bool
ModbusTCPMaster
::
waitInput
(
int
msec
)
{
if
(
!
tcp
)
return
false
;
FD_ZERO
(
&
s_set
);
FD_SET
(
tcp
->
getSocket
(),
&
s_set
);
s_timeout
.
tv_sec
=
0
;
s_timeout
.
tv_usec
=
1000
*
msec
;
return
(
select
(
1
+
(
int
)
tcp
->
getSocket
(),
&
s_set
,
NULL
,
NULL
,
&
s_timeout
)
>
0
);
}
// -------------------------------------------------------------------------
bool
ModbusTCPMaster
::
waitOutput
(
int
timeout_msec
)
{
if
(
!
tcp
)
return
false
;
FD_ZERO
(
&
s_set
);
FD_SET
(
tcp
->
getSocket
(),
&
s_set
);
s_timeout
.
tv_sec
=
0
;
s_timeout
.
tv_usec
=
1000
*
timeout_msec
;
//return ( select(FD_SETSIZE, NULL, &s_set, NULL, &s_timeout) > 0 );
return
(
select
(
1
+
(
int
)
tcp
->
getSocket
(),
NULL
,
&
s_set
,
NULL
,
&
s_timeout
)
>
0
);
}
// -------------------------------------------------------------------------
void
ModbusTCPMaster
::
cleanInputStream
()
{
unsigned
char
buf
[
100
];
...
...
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