Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
d1c3dc64
提交
d1c3dc64
authored
4月 05, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs
上级
40f9deed
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
20 行增加
和
6 行删除
+20
-6
mod.rs
src/comm/mod.rs
+9
-3
serial.rs
src/comm/serial.rs
+3
-1
tcp.rs
src/comm/tcp.rs
+2
-0
eapi.rs
src/io/eapi.rs
+4
-0
mod.rs
src/io/modbus/mod.rs
+1
-1
server.rs
src/io/modbus/server.rs
+1
-1
没有找到文件。
src/comm/mod.rs
浏览文件 @
d1c3dc64
use
parking_lot
::
MutexGuard
;
use
parking_lot
::
MutexGuard
;
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Arc
;
pub
mod
serial
;
pub
mod
serial
;
// Serial communications
pub
mod
tcp
;
pub
mod
tcp
;
// TCP communications
/// A versatile (TCP/serial) client
#[derive(Clone)]
#[derive(Clone)]
pub
struct
Client
(
Arc
<
dyn
Communicator
+
Send
+
Sync
>
);
pub
struct
Client
(
Arc
<
dyn
Communicator
+
Send
+
Sync
>
);
impl
Client
{
impl
Client
{
/// Lock the client for exclusive access
pub
fn
lock
(
&
self
)
->
MutexGuard
<
()
>
{
pub
fn
lock
(
&
self
)
->
MutexGuard
<
()
>
{
self
.
0
.lock
()
self
.
0
.lock
()
}
}
/// Reconnect the client in case of read/write problems
pub
fn
reconnect
(
&
self
)
{
pub
fn
reconnect
(
&
self
)
{
self
.
0
.reconnect
();
self
.
0
.reconnect
();
}
}
/// Write data to the client
pub
fn
write
(
&
self
,
buf
:
&
[
u8
])
->
Result
<
(),
std
::
io
::
Error
>
{
pub
fn
write
(
&
self
,
buf
:
&
[
u8
])
->
Result
<
(),
std
::
io
::
Error
>
{
self
.
0
.write
(
buf
)
self
.
0
.write
(
buf
)
}
}
/// Read data from the client
pub
fn
read_exact
(
&
self
,
buf
:
&
mut
[
u8
])
->
Result
<
(),
std
::
io
::
Error
>
{
pub
fn
read_exact
(
&
self
,
buf
:
&
mut
[
u8
])
->
Result
<
(),
std
::
io
::
Error
>
{
self
.
0
.read_exact
(
buf
)
self
.
0
.read_exact
(
buf
)
}
}
/// Get the protocol of the client
pub
fn
protocol
(
&
self
)
->
Protocol
{
pub
fn
protocol
(
&
self
)
->
Protocol
{
self
.
0
.protocol
()
self
.
0
.protocol
()
}
}
...
@@ -27,7 +33,7 @@ impl Client {
...
@@ -27,7 +33,7 @@ impl Client {
pub
enum
Protocol
{
pub
enum
Protocol
{
Tcp
,
Tcp
,
Rtu
,
Serial
,
}
}
trait
Communicator
{
trait
Communicator
{
...
...
src/comm/serial.rs
浏览文件 @
d1c3dc64
...
@@ -12,6 +12,8 @@ use std::str::FromStr;
...
@@ -12,6 +12,8 @@ use std::str::FromStr;
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Arc
;
use
std
::
time
::{
Duration
,
Instant
};
use
std
::
time
::{
Duration
,
Instant
};
/// Create a new serial client. The client will attempt to connect to the given address at the time
/// of the first request. The client will automatically reconnect if the connection is lost.
pub
fn
connect
(
path
:
&
str
,
timeout
:
Duration
,
frame_delay
:
Duration
)
->
Result
<
Client
>
{
pub
fn
connect
(
path
:
&
str
,
timeout
:
Duration
,
frame_delay
:
Duration
)
->
Result
<
Client
>
{
Ok
(
Client
(
Serial
::
create
(
path
,
timeout
,
frame_delay
)
?
))
Ok
(
Client
(
Serial
::
create
(
path
,
timeout
,
frame_delay
)
?
))
}
}
...
@@ -184,7 +186,7 @@ impl Communicator for Serial {
...
@@ -184,7 +186,7 @@ impl Communicator for Serial {
.map_err
(
Into
::
into
)
.map_err
(
Into
::
into
)
}
}
fn
protocol
(
&
self
)
->
Protocol
{
fn
protocol
(
&
self
)
->
Protocol
{
Protocol
::
Rtu
Protocol
::
Serial
}
}
}
}
...
...
src/comm/tcp.rs
浏览文件 @
d1c3dc64
...
@@ -9,6 +9,8 @@ use std::net::{SocketAddr, ToSocketAddrs};
...
@@ -9,6 +9,8 @@ use std::net::{SocketAddr, ToSocketAddrs};
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Arc
;
use
std
::
time
::
Duration
;
use
std
::
time
::
Duration
;
/// Create a new TCP client. The client will attempt to connect to the given address at the time of
/// the first request. The client will automatically reconnect if the connection is lost.
pub
fn
connect
<
A
:
ToSocketAddrs
+
fmt
::
Debug
>
(
addr
:
A
,
timeout
:
Duration
)
->
Result
<
Client
,
Error
>
{
pub
fn
connect
<
A
:
ToSocketAddrs
+
fmt
::
Debug
>
(
addr
:
A
,
timeout
:
Duration
)
->
Result
<
Client
,
Error
>
{
Ok
(
Client
(
Tcp
::
create
(
addr
,
timeout
)
?
))
Ok
(
Client
(
Tcp
::
create
(
addr
,
timeout
)
?
))
}
}
...
...
src/io/eapi.rs
浏览文件 @
d1c3dc64
...
@@ -316,6 +316,10 @@ where
...
@@ -316,6 +316,10 @@ where
{
{
/// creates a new EAPI connector instance with the name, automatically formatted as
/// creates a new EAPI connector instance with the name, automatically formatted as
/// `fieldbus.HOSTNAME.program`
/// `fieldbus.HOSTNAME.program`
///
/// # Panics
///
/// Will panic if failed to get the hostname
pub
fn
new_program
(
config
:
EAPIConfig
<
D
,
V
>
)
->
Self
{
pub
fn
new_program
(
config
:
EAPIConfig
<
D
,
V
>
)
->
Self
{
Self
::
new
(
Self
::
new
(
format!
(
format!
(
...
...
src/io/modbus/mod.rs
浏览文件 @
d1c3dc64
...
@@ -44,7 +44,7 @@ impl From<Protocol> for ModbusProto {
...
@@ -44,7 +44,7 @@ impl From<Protocol> for ModbusProto {
fn
from
(
value
:
Protocol
)
->
Self
{
fn
from
(
value
:
Protocol
)
->
Self
{
match
value
{
match
value
{
Protocol
::
Tcp
=>
ModbusProto
::
TcpUdp
,
Protocol
::
Tcp
=>
ModbusProto
::
TcpUdp
,
Protocol
::
Rtu
=>
ModbusProto
::
Rtu
,
Protocol
::
Serial
=>
ModbusProto
::
Rtu
,
}
}
}
}
}
}
...
...
src/io/modbus/server.rs
浏览文件 @
d1c3dc64
...
@@ -83,7 +83,7 @@ impl<const C: usize, const D: usize, const I: usize, const H: usize> ModbusServe
...
@@ -83,7 +83,7 @@ impl<const C: usize, const D: usize, const I: usize, const H: usize> ModbusServe
)
->
Result
<
Self
>
{
)
->
Result
<
Self
>
{
let
server
=
match
protocol
{
let
server
=
match
protocol
{
Protocol
::
Tcp
=>
Server
::
Tcp
(
TcpListener
::
bind
(
path
)
?
),
Protocol
::
Tcp
=>
Server
::
Tcp
(
TcpListener
::
bind
(
path
)
?
),
Protocol
::
Rtu
=>
Server
::
Serial
(
comm
::
serial
::
open
(
&
path
.parse
()
?
,
timeout
)
?
),
Protocol
::
Serial
=>
Server
::
Serial
(
comm
::
serial
::
open
(
&
path
.parse
()
?
,
timeout
)
?
),
};
};
Ok
(
Self
{
Ok
(
Self
{
storage
:
<
_
>
::
default
(),
storage
:
<
_
>
::
default
(),
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论