You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
2.1 KiB
56 lines
2.1 KiB
|
4 weeks ago
|
using EGFramework;
|
||
|
|
|
||
|
|
public class ModelSerialServer : EGModule,IEGFramework
|
||
|
|
{
|
||
|
|
public int SerialServerPort { set; get; } = 6550;
|
||
|
|
public int SerialServerPortBackUp { set; get; } = 6551;
|
||
|
|
public List<string> SerialNames { set; get; } = new List<string>();
|
||
|
|
public override void Init()
|
||
|
|
{
|
||
|
|
this.EGTCPServerListen(SerialServerPort);
|
||
|
|
this.EGOnMessage<ResponseSerialPort>();
|
||
|
|
this.EGOnMessage<ResponseSerialServer>();
|
||
|
|
this.EGRegisterMessageEvent<ResponseSerialServer>(OnReceivedFromTCP);
|
||
|
|
this.EGRegisterMessageEvent<ResponseSerialPort>(OnReceivedFromSerialPort);
|
||
|
|
EG.Print("串口转TCP服务已开启!");
|
||
|
|
// throw new NotImplementedException();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SendToSerial(ResponseSerialServer res)
|
||
|
|
{
|
||
|
|
if (!SerialNames.Contains(res.SerialPort) && res.BaudRate != 0)
|
||
|
|
{
|
||
|
|
this.EGSerialPort().Open(res.SerialPort, res.BaudRate);
|
||
|
|
SerialNames.Add(res.SerialPort);
|
||
|
|
}
|
||
|
|
EG.Print("Send Serial : " + res.StringData);
|
||
|
|
this.EGSendMessage<RequestSerialPort>(new RequestSerialPort(res), res.SerialPort, ProtocolType.SerialPort);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void SendToClient(byte[] data,string serialPortName)
|
||
|
|
{
|
||
|
|
if (this.EGTCPServer().LinkedIPs.ContainsKey(6550))
|
||
|
|
{
|
||
|
|
EG.Print("Send Client : " + data.ToStringByHex());
|
||
|
|
foreach (string sender in this.EGTCPServer().LinkedIPs[6550])
|
||
|
|
{
|
||
|
|
this.EGSendMessage<RequestSerialRevert>(new RequestSerialRevert(data,serialPortName), sender, ProtocolType.TCPServer);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnReceivedFromTCP(ResponseSerialServer tcpData,string sender,ProtocolType protocol)
|
||
|
|
{
|
||
|
|
if(protocol == ProtocolType.TCPServer && this.EGTCPServer().LinkedIPs[6550].Contains(sender))
|
||
|
|
{
|
||
|
|
SendToSerial(tcpData);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
public void OnReceivedFromSerialPort(ResponseSerialPort serialData,string sender,ProtocolType protocol)
|
||
|
|
{
|
||
|
|
if(protocol == ProtocolType.SerialPort)
|
||
|
|
{
|
||
|
|
SendToClient(serialData.Data,sender);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|