diff --git a/General/General.csproj b/General/Soul2.General.csproj
similarity index 100%
rename from General/General.csproj
rename to General/Soul2.General.csproj
diff --git a/SerialPort/Soul2.SerialPort.csproj b/SerialPort/Soul2.SerialPort.csproj
new file mode 100644
index 0000000..2e95d3f
--- /dev/null
+++ b/SerialPort/Soul2.SerialPort.csproj
@@ -0,0 +1,18 @@
+
+
+
+ net7.0
+ enable
+ enable
+ Soul2.$(MSBuildProjectName.Replace(" ", "_"))
+
+
+
+
+
+
+
+
+
+
+
diff --git a/General/utils/GodSerialPortUtils.cs b/SerialPort/utils/GodSerialPortUtils.cs
similarity index 78%
rename from General/utils/GodSerialPortUtils.cs
rename to SerialPort/utils/GodSerialPortUtils.cs
index e35987d..950f1d4 100644
--- a/General/utils/GodSerialPortUtils.cs
+++ b/SerialPort/utils/GodSerialPortUtils.cs
@@ -9,10 +9,9 @@ using GodSharp.SerialPort;
using System.IO.Ports;
using System.Text.RegularExpressions;
-namespace Soul2.General.utils {
+namespace Soul2.SerialPort.utils {
///
/// 串口通信工具类
- /// 需要TimerUtils
///
public static class GodSerialPortUtils {
@@ -42,17 +41,22 @@ namespace Soul2.General.utils {
/// 查询所有可用串口
///
/// 串口名称数组
- public static string[] find() {
- return SerialPort.GetPortNames();
- }
+ public static string[] find() => System.IO.Ports.SerialPort.GetPortNames();
///
/// 当前连接状态
///
///
- public static bool status() {
- return gsp != null && gsp.IsOpen != false;
- }
+ public static bool status() => (gsp != null && gsp.IsOpen != false);
+
+ ///
+ /// 发送报文信息
+ ///
+ /// 发送的内容
+ /// 回调函数,参数为byte[]类型的回信
+ /// 是否等待回信完成再执行回调函数,默认为否
+ ///
+ public static void send(string data, Action callback = null, bool wait_complete = false) => send(data.HexToByte(), callback, wait_complete);
///
/// 发送报文信息
@@ -142,5 +146,24 @@ namespace Soul2.General.utils {
return hexBytes.ToHexString();
}
+ ///
+ /// 计算校验码
+ ///
+ /// 当前报文,不应包含校验码的部分
+ ///
+ public static string CalcChecksum(string msg) {
+ if (msg.StartsWith("FE FE FE FE ")) {
+ msg = msg.Replace("FE FE FE FE ", "");
+ }
+
+ // 检查输入字符串是否符合要求
+ if (!Regex.IsMatch(msg, @"^[\da-fA-F]{2}( [\da-fA-F]{2})*$")) {
+ return "==";
+ }
+
+ var bts = msg.Split(' ').Select(b => Convert.ToInt32(b, 16)).ToArray();
+ var r = (bts.Sum() % 256).ToString("X2");
+ return r;
+ }
}
}
diff --git a/Soul2-Library.sln b/Soul2-Library.sln
index f5640eb..592c8ff 100644
--- a/Soul2-Library.sln
+++ b/Soul2-Library.sln
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33530.505
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "General", "General\General.csproj", "{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Soul2.General", "General\Soul2.General.csproj", "{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Soul2.SerialPort", "SerialPort\Soul2.SerialPort.csproj", "{B2EFA26A-A2CF-4090-85FD-31085D7A1A65}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}.Release|Any CPU.Build.0 = Release|Any CPU
+ {B2EFA26A-A2CF-4090-85FD-31085D7A1A65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {B2EFA26A-A2CF-4090-85FD-31085D7A1A65}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {B2EFA26A-A2CF-4090-85FD-31085D7A1A65}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {B2EFA26A-A2CF-4090-85FD-31085D7A1A65}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE