diff --git a/Samples/BuzzerTest/BuzzerTest.csproj b/Samples/BuzzerTest/BuzzerTest.csproj index 7c5a8fd..62c0a75 100644 --- a/Samples/BuzzerTest/BuzzerTest.csproj +++ b/Samples/BuzzerTest/BuzzerTest.csproj @@ -14,7 +14,6 @@ ..\..\Bin\BuzzerTest false enable - enable diff --git a/Samples/KeyTest/KeyTest.csproj b/Samples/KeyTest/KeyTest.csproj index c88657f..fd3f49b 100644 --- a/Samples/KeyTest/KeyTest.csproj +++ b/Samples/KeyTest/KeyTest.csproj @@ -14,7 +14,6 @@ ..\..\Bin\KeyTest false enable - enable diff --git a/Samples/LedTest/LedTest.csproj b/Samples/LedTest/LedTest.csproj index 09464e8..340b958 100644 --- a/Samples/LedTest/LedTest.csproj +++ b/Samples/LedTest/LedTest.csproj @@ -14,7 +14,6 @@ ..\..\Bin\LedTest false enable - enable diff --git a/Samples/NetClientTest/NetClientTest.csproj b/Samples/NetClientTest/NetClientTest.csproj index 072f3b3..a307fdd 100644 --- a/Samples/NetClientTest/NetClientTest.csproj +++ b/Samples/NetClientTest/NetClientTest.csproj @@ -14,7 +14,6 @@ ..\..\Bin\NetClientTest false enable - enable diff --git a/Samples/NetServerTest/NetServerTest.csproj b/Samples/NetServerTest/NetServerTest.csproj index b7dfd9d..6d3003d 100644 --- a/Samples/NetServerTest/NetServerTest.csproj +++ b/Samples/NetServerTest/NetServerTest.csproj @@ -14,7 +14,6 @@ ..\..\Bin\NetServerTest false enable - enable diff --git a/Samples/Serial2NetClientTest/Program.cs b/Samples/Serial2NetClientTest/Program.cs new file mode 100644 index 0000000..39bc2d9 --- /dev/null +++ b/Samples/Serial2NetClientTest/Program.cs @@ -0,0 +1,62 @@ +using System.IO.Ports; +using NewLife; +using NewLife.Data; +using NewLife.Log; +using NewLife.Net; +using SmartA2; + +// 客户端,本地连接串口,远程连接服务端 + +internal class Program +{ + static SerialPort _serial; + static ISocketRemote _client; + + private static void Main(string[] args) + { + XTrace.UseConsole(); + + var host = new A2(); + + // 配置并打开串口COM1 + var serial = host.CreateSerial(1, 9600); + serial.DataReceived += OnReceiveSerial; + serial.Open(); + + // 服务器地址,可保存在配置文件中,支持tcp/udp地址 + var server = "tcp://10.0.2.6:888"; + var uri = new NetUri(server); + var client = uri.CreateRemote(); + client.Log = XTrace.Log; + client.Received += OnReceiveSocket; + client.Open(); + + _serial = serial; + _client = client; + + // 等待退出 + Console.ReadLine(); + } + + static void OnReceiveSerial(Object sender, SerialDataReceivedEventArgs e) + { + // 等一会儿,等待数据接收完毕 + Thread.Sleep(10); + + var sp = sender as SerialPort; + var buf = new Byte[sp.BytesToRead]; + var count = sp.Read(buf, 0, buf.Length); + if (count <= 0) return; + + // 发送串口数据到服务器 + var pk = new Packet(buf, 0, count); + _client.Send(pk); + } + + static void OnReceiveSocket(Object sender, ReceivedEventArgs e) + { + // 接收到服务器数据,转发到串口 + var pk = e.Packet; + _serial.Write(pk.Data, pk.Offset, pk.Count); + } +} \ No newline at end of file diff --git a/Samples/Serial2NetClientTest/Serial2NetClientTest.csproj b/Samples/Serial2NetClientTest/Serial2NetClientTest.csproj new file mode 100644 index 0000000..11ea5b9 --- /dev/null +++ b/Samples/Serial2NetClientTest/Serial2NetClientTest.csproj @@ -0,0 +1,27 @@ + + + + Exe + net7.0 + 新生命开发团队 + ©2002-2023 新生命开发团队 + 1.0 + $([System.DateTime]::Now.ToString(`yyyy.MMdd`)) + $(VersionPrefix).$(VersionSuffix) + $(Version) + $(VersionPrefix).* + false + ..\..\Bin\Serial2NetClientTest + false + enable + + + + + + + + + + + diff --git a/Samples/Serial2NetServerTest/Program.cs b/Samples/Serial2NetServerTest/Program.cs new file mode 100644 index 0000000..e7646c4 --- /dev/null +++ b/Samples/Serial2NetServerTest/Program.cs @@ -0,0 +1,57 @@ +using NewLife.Log; +using NewLife.Net; + +// 网络服务端,接收客户端数据 + +XTrace.UseConsole(); + +// 应用层连接字典 +var users = new Dictionary(); + +var server = new NetServer(888) +{ + Log = XTrace.Log, + SessionLog = XTrace.Log +}; + +// 新连接会话事件 +server.NewSession += (s, e) => +{ + var uri = e.Session.Remote; + XTrace.WriteLine("新会话:{0}", uri); + + // 记录该设备IP,后面通过IP找到对应会话并下发数据 + var session = e.Session; + users[uri.Address + ""] = session.ID; +}; + +// 在事件中接收数据 +server.Received += (s, e) => +{ + var msg = e.Packet.ToStr(); + XTrace.WriteLine("收到数据:{0}", msg); + + // 倒序返回 + var session = s as INetSession; + var cs = msg.Reverse().ToArray(); + session.Send(new String(cs)); +}; + +server.Start(); + +// 应用层向指定设备下发数据 +var ip = "10.0.2.6"; +if (users.TryGetValue(ip, out var id)) +{ + // 根据ID找到对应会话,如果会话不存在,可能是设备已经断开 + var session = server.GetSession(id); + if (session != null) + { + var msg = "Hello " + ip; + session.Send(msg); + XTrace.WriteLine("向[{0}]发送数据[{1}]", ip, msg); + } +} + +// 等待退出 +Console.ReadLine(); \ No newline at end of file diff --git a/Samples/Serial2NetServerTest/Serial2NetServerTest.csproj b/Samples/Serial2NetServerTest/Serial2NetServerTest.csproj new file mode 100644 index 0000000..55a8f17 --- /dev/null +++ b/Samples/Serial2NetServerTest/Serial2NetServerTest.csproj @@ -0,0 +1,23 @@ + + + + Exe + net7.0 + 新生命开发团队 + ©2002-2023 新生命开发团队 + 1.0 + $([System.DateTime]::Now.ToString(`yyyy.MMdd`)) + $(VersionPrefix).$(VersionSuffix) + $(Version) + $(VersionPrefix).* + false + ..\..\Bin\Serial2NetServerTest + false + enable + + + + + + + diff --git a/Samples/SerialTest/SerialTest.csproj b/Samples/SerialTest/SerialTest.csproj index 173372e..f89adb3 100644 --- a/Samples/SerialTest/SerialTest.csproj +++ b/Samples/SerialTest/SerialTest.csproj @@ -14,12 +14,14 @@ ..\..\Bin\SerialTest false enable - enable - + + + + diff --git a/Samples/UsbPowerTest/UsbPowerTest.csproj b/Samples/UsbPowerTest/UsbPowerTest.csproj index 5558dec..a7aa338 100644 --- a/Samples/UsbPowerTest/UsbPowerTest.csproj +++ b/Samples/UsbPowerTest/UsbPowerTest.csproj @@ -14,7 +14,6 @@ ..\..\Bin\UsbPowerTest false enable - enable diff --git a/SmartA2.sln b/SmartA2.sln index 990384a..ba46428 100644 --- a/SmartA2.sln +++ b/SmartA2.sln @@ -42,12 +42,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "6.网络", "6.网络", "{43 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerialTest", "Samples\SerialTest\SerialTest.csproj", "{65D7E24B-FAA3-4159-825F-8EE25316D52C}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "7.数据库", "7.数据库", "{C1A2B597-61C1-4A4B-AF34-A6E808B89522}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "8.数据库", "8.数据库", "{C1A2B597-61C1-4A4B-AF34-A6E808B89522}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetClientTest", "Samples\NetClientTest\NetClientTest.csproj", "{A005D70E-BEC9-473C-AE26-F7C4217C5DC2}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetServerTest", "Samples\NetServerTest\NetServerTest.csproj", "{3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "7.串口转以太网", "7.串口转以太网", "{5BE54C41-A5D4-4255-986F-5FB9CE486BFE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serial2NetClientTest", "Samples\Serial2NetClientTest\Serial2NetClientTest.csproj", "{DE468AF1-CA80-4B4C-88FD-2AE016C4F970}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serial2NetServerTest", "Samples\Serial2NetServerTest\Serial2NetServerTest.csproj", "{C26767B7-9386-4EF3-8625-FCD3BB403066}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -94,6 +100,14 @@ Global {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}.Debug|Any CPU.Build.0 = Debug|Any CPU {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}.Release|Any CPU.ActiveCfg = Release|Any CPU {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}.Release|Any CPU.Build.0 = Release|Any CPU + {DE468AF1-CA80-4B4C-88FD-2AE016C4F970}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DE468AF1-CA80-4B4C-88FD-2AE016C4F970}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DE468AF1-CA80-4B4C-88FD-2AE016C4F970}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DE468AF1-CA80-4B4C-88FD-2AE016C4F970}.Release|Any CPU.Build.0 = Release|Any CPU + {C26767B7-9386-4EF3-8625-FCD3BB403066}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C26767B7-9386-4EF3-8625-FCD3BB403066}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C26767B7-9386-4EF3-8625-FCD3BB403066}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C26767B7-9386-4EF3-8625-FCD3BB403066}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -113,6 +127,9 @@ Global {C1A2B597-61C1-4A4B-AF34-A6E808B89522} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0} {A005D70E-BEC9-473C-AE26-F7C4217C5DC2} = {43CA44E0-92E8-4448-89A4-240AD34494A4} {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD} = {43CA44E0-92E8-4448-89A4-240AD34494A4} + {5BE54C41-A5D4-4255-986F-5FB9CE486BFE} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0} + {DE468AF1-CA80-4B4C-88FD-2AE016C4F970} = {5BE54C41-A5D4-4255-986F-5FB9CE486BFE} + {C26767B7-9386-4EF3-8625-FCD3BB403066} = {5BE54C41-A5D4-4255-986F-5FB9CE486BFE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {323831A1-A95B-40AB-B9AD-36A0BC10C2CB}