-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters