-
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
5 changed files
with
144 additions
and
0 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
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> |
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,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); | ||
} |
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,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()); | ||
} | ||
} |
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,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> |
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