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 9c940db commit 363f017
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Samples/NetTest/NetTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<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\KeyTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
16 changes: 16 additions & 0 deletions Samples/NetTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SmartA2;

var key = new InputPort { FileName = "/dev/key" };

var f = false;
for (var i = 0; i < 100; i++)
{
var rs = key.Read();
if (rs != f)
{
f = rs;
Console.WriteLine(f ? "按下" : "松开");
}

Thread.Sleep(100);
}
60 changes: 60 additions & 0 deletions Samples/SerialTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.IO.Ports;
using NewLife;
using NewLife.Log;

internal class Program
{
private static void Main(string[] args)
{
XTrace.UseConsole();

// 硬件串口对照表
// COM1:/dev/ttyAMA0
// COM2:/dev/ttyAMA1
// COM3:/dev/ttyAMA2
// COM4:/dev/ttyAMA3

// 列出所有串口
var ports = SerialPort.GetPortNames();
XTrace.WriteLine("串口列表[{0}]:", ports.Length);
foreach (var port in ports)
{
XTrace.WriteLine(port);
}

// 配置并打开串口COM1
var serial = new SerialPort
{
PortName = "/dev/ttyAMA0",
BaudRate = 9600,
};
// 注册数据接收事件
serial.DataReceived += OnReceive;
serial.Open();

// 发送数据
for (var i = 0; i < 10; i++)
{
XTrace.WriteLine("请输入要发送的数据:");
var input = Console.ReadLine();

serial.Write(input);
}

// 关闭串口
serial.Close();
}

static void OnReceive(Object sender, SerialDataReceivedEventArgs e)
{
// 等一会儿,等待数据接收完毕
Thread.Sleep(10);

var sp = sender as SerialPort;
var buf = new Byte[sp.BytesToRead];
sp.Read(buf, 0, buf.Length);

XTrace.WriteLine("收到数据:{0}", buf.ToStr());
//XTrace.WriteLine("收到数据:{0}", buf.ToHex());
}
}
25 changes: 25 additions & 0 deletions Samples/SerialTest/SerialTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<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\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>

</Project>
23 changes: 23 additions & 0 deletions SmartA2.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsbPowerTest", "Samples\Usb
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyTest", "Samples\KeyTest\KeyTest.csproj", "{6BE3AB6D-88A6-4493-B6BB-C725882B991D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "5.串口", "5.串口", "{50F09D8D-EE42-467C-A366-E50676F157E0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "6.网络", "6.网络", "{43CA44E0-92E8-4448-89A4-240AD34494A4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerialTest", "Samples\SerialTest\SerialTest.csproj", "{65D7E24B-FAA3-4159-825F-8EE25316D52C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetTest", "Samples\NetTest\NetTest.csproj", "{25809B61-CED1-4D7B-82B4-E2E3FC462A74}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "7.数据库", "7.数据库", "{C1A2B597-61C1-4A4B-AF34-A6E808B89522}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -70,6 +80,14 @@ Global
{6BE3AB6D-88A6-4493-B6BB-C725882B991D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BE3AB6D-88A6-4493-B6BB-C725882B991D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BE3AB6D-88A6-4493-B6BB-C725882B991D}.Release|Any CPU.Build.0 = Release|Any CPU
{65D7E24B-FAA3-4159-825F-8EE25316D52C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65D7E24B-FAA3-4159-825F-8EE25316D52C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65D7E24B-FAA3-4159-825F-8EE25316D52C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65D7E24B-FAA3-4159-825F-8EE25316D52C}.Release|Any CPU.Build.0 = Release|Any CPU
{25809B61-CED1-4D7B-82B4-E2E3FC462A74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25809B61-CED1-4D7B-82B4-E2E3FC462A74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25809B61-CED1-4D7B-82B4-E2E3FC462A74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25809B61-CED1-4D7B-82B4-E2E3FC462A74}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -83,6 +101,11 @@ Global
{88DF6E42-8776-4073-89BB-26666888BB5D} = {8825FC53-DDAF-4F1E-8F3C-38A85C19731A}
{A027B945-184C-4EDD-95E1-038586B60FFD} = {8862FCEB-8460-4E40-B8D5-A91EA1F2E3BA}
{6BE3AB6D-88A6-4493-B6BB-C725882B991D} = {A3BF37DF-1078-41A2-82DF-4AE854096DF5}
{50F09D8D-EE42-467C-A366-E50676F157E0} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0}
{43CA44E0-92E8-4448-89A4-240AD34494A4} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0}
{65D7E24B-FAA3-4159-825F-8EE25316D52C} = {50F09D8D-EE42-467C-A366-E50676F157E0}
{25809B61-CED1-4D7B-82B4-E2E3FC462A74} = {43CA44E0-92E8-4448-89A4-240AD34494A4}
{C1A2B597-61C1-4A4B-AF34-A6E808B89522} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {323831A1-A95B-40AB-B9AD-36A0BC10C2CB}
Expand Down

0 comments on commit 363f017

Please sign in to comment.