SmartOS/Modbus/Slave.cpp

42 lines
718 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Slave.h"
Slave(ITransport* port)
{
_port = port;
Address = 0;
}
Slave::~Slave()
{
delete _port;
_port = nullptr;
}
void Slave::OnReceive(ITransport* transport, byte* buf, uint len, void* param)
{
auto slave = (Slave*)param;
Stream ms(buf, len);
Modbus entity;
if(!entity.Read(ms)) return;
slave->Dispatch(entity);
}
// 分发处理消息。返回值决定是否响应
bool Slave::Dispatch(Modbus& entity)
{
// 是否本地地址或者本地是否0接收所有消息
if(Address && Address != entity.Address) return false;
// 检查Crc
if(entity.Crc != enable.Crc2)
{
entity.SetError(ModbusErrors::Crc);
return true;
}
return true;
}