This commit is contained in:
anlicheng 2025-06-21 01:02:47 +08:00
parent 8ef4c9786c
commit 9ec12be0f7
2 changed files with 57 additions and 16 deletions

View File

@ -10,6 +10,14 @@ pub(crate) struct SerialConfig {
pub timeout: u32, // Timeout:32 pub timeout: u32, // Timeout:32
} }
#[derive(Debug)]
pub(crate) struct Request {
pub packet_id: u32,
pub slave_id: u8,
pub address: u16,
pub cnt: u16,
}
pub(crate) fn parse_serial_config(data: &[u8]) -> std::io::Result<SerialConfig> { pub(crate) fn parse_serial_config(data: &[u8]) -> std::io::Result<SerialConfig> {
let mut cursor = Cursor::new(data); let mut cursor = Cursor::new(data);
@ -29,4 +37,20 @@ pub(crate) fn parse_serial_config(data: &[u8]) -> std::io::Result<SerialConfig>
parity, parity,
timeout, timeout,
}) })
}
pub(crate) fn parse_request(data: &[u8]) -> std::io::Result<Request> {
let mut cursor = Cursor::new(data);
let packet_id = cursor.read_u32::<BigEndian>()?;
let slave_id = cursor.read_u8()?;
let address = cursor.read_u16::<BigEndian>()?;
let cnt = cursor.read_u16::<BigEndian>()?;
Ok(Request {
packet_id,
slave_id,
address,
cnt,
})
} }

View File

@ -34,6 +34,8 @@ async fn main() {
println!("port: {:?}", c.port); println!("port: {:?}", c.port);
let mut client: Option<ModbusClient> = None;
loop { loop {
// 1. 读取4字节长度前缀 (big-endian) // 1. 读取4字节长度前缀 (big-endian)
let mut len_bytes = [0u8; 2]; let mut len_bytes = [0u8; 2];
@ -53,29 +55,44 @@ async fn main() {
0x01 => { 0x01 => {
let config = codec::parse_serial_config(&data[1..]).unwrap(); let config = codec::parse_serial_config(&data[1..]).unwrap();
println!("{:?}", config); println!("{:?}", config);
let client = ModbusClient::new(config); client = Option::from(ModbusClient::new(config));
},
0x02 => {
let request = codec::parse_request(&data[1..]).unwrap();
println!("request {:?}", request);
if let Some(client) = client.as_mut() {
match client.request(request.slave_id, request.address, request.cnt).await {
Ok(vec) => {
}
Err(e) => {
}
}
}
}, },
_ => { _ => {
} }
} }
// let data_str = String::from_utf8(data).unwrap_or_else(|_| {
// eprintln!("Invalid UTF-8 data");
// process::exit(-1);
// });
//
// // 3. 处理数据并生成响应
// let response = format!("Processed: {}", data_str);
// let response_bytes = response.as_bytes();
//
// // 4. 写入响应(长度前缀 + 数据)
// stdout.write_all(&(response_bytes.len() as u32).to_be_bytes())?;
// stdout.write_all(response_bytes)?;
// stdout.flush()?; // 确保立即发送
} }
// let data_str = String::from_utf8(data).unwrap_or_else(|_| {
// eprintln!("Invalid UTF-8 data");
// process::exit(-1);
// });
//
// // 3. 处理数据并生成响应
// let response = format!("Processed: {}", data_str);
// let response_bytes = response.as_bytes();
//
// // 4. 写入响应(长度前缀 + 数据)
// stdout.write_all(&(response_bytes.len() as u32).to_be_bytes())?;
// stdout.write_all(response_bytes)?;
// stdout.flush()?; // 确保立即发送
} }
async fn handle_client(mut stream: UnixStream) -> Result<(), Box<dyn std::error::Error>> { async fn handle_client(mut stream: UnixStream) -> Result<(), Box<dyn std::error::Error>> {