Skip to content

Commit

Permalink
增加转口转发以太网的例程
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 27, 2023
1 parent 6f5e787 commit b91a6b2
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 9 deletions.
1 change: 0 additions & 1 deletion Samples/BuzzerTest/BuzzerTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<OutputPath>..\..\Bin\BuzzerTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
1 change: 0 additions & 1 deletion Samples/KeyTest/KeyTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<OutputPath>..\..\Bin\KeyTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
1 change: 0 additions & 1 deletion Samples/LedTest/LedTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<OutputPath>..\..\Bin\LedTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
1 change: 0 additions & 1 deletion Samples/NetClientTest/NetClientTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<OutputPath>..\..\Bin\NetClientTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion Samples/NetServerTest/NetServerTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<OutputPath>..\..\Bin\NetServerTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
62 changes: 62 additions & 0 deletions Samples/Serial2NetClientTest/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
27 changes: 27 additions & 0 deletions Samples/Serial2NetClientTest/Serial2NetClientTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Company>新生命开发团队</Company>
<Copyright>©2002-2023 新生命开发团队</Copyright>
<VersionPrefix>1.0</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin\Serial2NetClientTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1001" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\SmartA2\SmartA2.csproj" />
</ItemGroup>

</Project>
57 changes: 57 additions & 0 deletions Samples/Serial2NetServerTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using NewLife.Log;
using NewLife.Net;

// 网络服务端,接收客户端数据

XTrace.UseConsole();

// 应用层连接字典
var users = new Dictionary<String, Int32>();

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();
23 changes: 23 additions & 0 deletions Samples/Serial2NetServerTest/Serial2NetServerTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Company>新生命开发团队</Company>
<Copyright>©2002-2023 新生命开发团队</Copyright>
<VersionPrefix>1.0</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin\Serial2NetServerTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1001" />
</ItemGroup>

</Project>
6 changes: 4 additions & 2 deletions Samples/SerialTest/SerialTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
<OutputPath>..\..\Bin\SerialTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1001" />
<PackageReference Include="System.IO.Ports" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\SmartA2\SmartA2.csproj" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion Samples/UsbPowerTest/UsbPowerTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<OutputPath>..\..\Bin\UsbPowerTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
19 changes: 18 additions & 1 deletion SmartA2.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down

0 comments on commit b91a6b2

Please sign in to comment.