From 255a87096dd93649ee2ce5a2e2d0027adde03a80 Mon Sep 17 00:00:00 2001
From: MoYue <18168119590@163.com>
Date: Sat, 11 Mar 2023 08:34:39 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A0=A1=E6=97=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
MonitoringTechnology/App.config | 1 +
MonitoringTechnology/Ble/BleDevice.cs | 55 +++++++++++++---
MonitoringTechnology/Common/tools.cs | 22 +++++++
.../Views/SystemOperationView.xaml | 2 +
.../Views/SystemOperationView.xaml.cs | 63 +++++++++++++++++--
5 files changed, 130 insertions(+), 13 deletions(-)
diff --git a/MonitoringTechnology/App.config b/MonitoringTechnology/App.config
index f3255b8..1cc236d 100644
--- a/MonitoringTechnology/App.config
+++ b/MonitoringTechnology/App.config
@@ -6,6 +6,7 @@
+
diff --git a/MonitoringTechnology/Ble/BleDevice.cs b/MonitoringTechnology/Ble/BleDevice.cs
index 6f6ec9a..55f27cd 100644
--- a/MonitoringTechnology/Ble/BleDevice.cs
+++ b/MonitoringTechnology/Ble/BleDevice.cs
@@ -43,8 +43,11 @@ namespace MonitoringTechnology.Ble
private readonly CharacteristicDictionary GattCharacteristics = new CharacteristicDictionary();
public static FileModel FileModel { get; set; } = new FileModel();
- public event TypedEventHandler CharacteristicValueChanged;
- public event TypedEventHandler ConnectionStateChanged;
+ public event TypedEventHandler CharacteristicValueChanged;//数据解析 -- 报文返回
+
+ public event TypedEventHandler ReceiveValueChanged; //数据解析 --自定义数据返回
+
+ public event TypedEventHandler ConnectionStateChanged;//连接状态变化
public static MonitoringSensorModel monitoringSensorModel { get; set; } = new MonitoringSensorModel();
static LocalDataAccess localDataAccess = new LocalDataAccess();
@@ -272,7 +275,7 @@ namespace MonitoringTechnology.Ble
private static string _localtime => $"{DateTime.Now: yyyy-MM-dd HH:mm:ss.fff}";
///
- /// 收到数据
+ /// 收到数据 ----报文fanhui
///
///
///
@@ -286,12 +289,21 @@ namespace MonitoringTechnology.Ble
ParsingData(data.ToList());
}
+ ///
+ /// 自定义数据接收--数据解析
+ ///
+ ///
+ ///
+ private void ReceiveValueChangedChanged(GattCharacteristic sender, string data)
+ {
+ ReceiveValueChanged?.Invoke(sender, data);
+ }
///
/// 定义一个委托,用于传值老的版本号
///
Action func = new Action(SystemOperationView.Setbbh);
- string _message = "";
+
byte[] bbh = new byte[4] { 0x00, 0x00, 0x00, 0x00 };
///
/// 解析数据
@@ -299,6 +311,8 @@ namespace MonitoringTechnology.Ble
///
private void ParsingData(List byteList)
{
+ string _message = "";
+
//校验当前List是否为空或者长度
if (byteList == null || byteList.Count == 0)
return;
@@ -364,7 +378,7 @@ namespace MonitoringTechnology.Ble
switch (_fun_code)
{
- //数据解析
+ //数据解析 ---实时数据
case 0x02:
switch (_kind)
{
@@ -467,6 +481,9 @@ namespace MonitoringTechnology.Ble
AttitudeDisplayView.monitoringSensor.F_DegreesZ = monitoringSensorModel.degrees_z;
localDataAccess.write("INSERT INTO monitoringsensormodel(F_CreateDate,F_DatetimeSensor,F_Tem,F_Hum,F_Pressure,F_AccelerationX,F_AccelerationY,F_AccelerationZ,F_AngularVelocityX,F_AngularVelocityY,F_AngularVelocityZ,F_DegreesX,F_DegreesY,F_DegreesZ) VALUES('" + monitoringSensorModel.f_CreateDate + "','" + monitoringSensorModel._datetime_Sensor + "','" + monitoringSensorModel._Tem + "','" + monitoringSensorModel._Hum + "','" + monitoringSensorModel._pressure + "','" + monitoringSensorModel._acceleration_X + "','" + monitoringSensorModel._acceleration_Y + "','" + monitoringSensorModel._acceleration_Z + "','" + monitoringSensorModel._angularVelocity_X + "','" + monitoringSensorModel._angularVelocity_Y + "','" + monitoringSensorModel._angularVelocity_Z + "','" + monitoringSensorModel.degrees_x + "','" + monitoringSensorModel.degrees_y + "','" + monitoringSensorModel.degrees_z + "');");
+
+ _message = "状态数据获取成功: 温度:"+ monitoringSensorModel._Tem + "℃ 湿度:" + monitoringSensorModel._Hum + "% 气压:" + monitoringSensorModel._pressure + "KPa 俯仰角:" + monitoringSensorModel._acceleration_X + "° 横滚角:"+ monitoringSensorModel._acceleration_Y + "° 偏航角:" + monitoringSensorModel._acceleration_Z + "° 角速度X:" + monitoringSensorModel._angularVelocity_X + "m/s² 角速度Y:" + monitoringSensorModel._angularVelocity_Y + "m/s² 角速度Z:" + monitoringSensorModel._angularVelocity_Z + "m/s² 角度X:" + monitoringSensorModel.degrees_x + "rad/s 角度Y:" + monitoringSensorModel.degrees_y + "rad/s 角度Z:" + monitoringSensorModel.degrees_z + "rad/s" ;
+ ReceiveValueChangedChanged(null, _message);
break;
//主系统数据
@@ -508,27 +525,47 @@ namespace MonitoringTechnology.Ble
switch (byteList[11])
{
case 0x00:
- _message += "获取成功";
+ _message += "版本查询:获取成功";
break;
case 0x01:
- _message += "空";
+ _message += "版本查询:空";
+ ReceiveValueChangedChanged(null, _message);
break;
case 0x02:
- _message += "设备忙";
+ _message += "版本查询:设备忙";
+ ReceiveValueChangedChanged(null, _message);
break;
default:
break;
}
//显示版本号
SystemOperationView.systemOperationViewModel.VersionQueryData = ((int)bbh[0]).ToString() + "." + ((int)bbh[1]).ToString() + "." + ((int)bbh[2]).ToString() + "." + ((int)bbh[3]).ToString();
-
+ _message += " 版本号:" + SystemOperationView.systemOperationViewModel.VersionQueryData;
+ ReceiveValueChangedChanged(null, _message);
break;
+
//设备回复文件数据指令
case 0x66:
int _start = Convert.ToInt32(byteList[6].ToString("X2") + byteList[7].ToString("X2") + byteList[8].ToString("X2") + byteList[9].ToString("X2"), 16);
int _Length = Convert.ToInt32(byteList[10].ToString("X2") + byteList[11].ToString("X2"), 16);
systemOperationView._send(_start, _Length);
break;
+
+ //校时
+ case 0x06:
+ //UTC时间
+ dt = startTime.AddSeconds(Convert.ToInt32(byteList[6].ToString("X2") + byteList[7].ToString("X2") + byteList[8].ToString("X2") + byteList[9].ToString("X2"), 16));
+ if ((time_DataBase - dt).TotalMinutes < 60)
+ {
+ _message = "系统校时成功";
+ ReceiveValueChangedChanged(null, _message);
+ }
+ else
+ {
+ _message = "系统校时失败";
+ ReceiveValueChangedChanged(null, _message);
+ }
+ break;
default:
break;
}
diff --git a/MonitoringTechnology/Common/tools.cs b/MonitoringTechnology/Common/tools.cs
index 79d3609..2fd3221 100644
--- a/MonitoringTechnology/Common/tools.cs
+++ b/MonitoringTechnology/Common/tools.cs
@@ -230,6 +230,28 @@ namespace MonitoringTechnology.Common
#endregion
+ /// 16进制原码字符串转字节数组
+ ///
+ /// "AABBCC"或"AA BB CC"格式的字符串
+ ///
+ public static byte[] ConvertHexStringToBytes(string hexString)
+ {
+ hexString = hexString.Replace(" ", "");
+ if (hexString.Length % 2 != 0)
+ {
+ throw new ArgumentException("参数长度不正确");
+ }
+
+ byte[] returnBytes = new byte[hexString.Length / 2];
+ for (int i = 0; i < returnBytes.Length; i++)
+ {
+ returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
+ }
+
+ return returnBytes;
+ }
+
+
public static List O_C(string o_c)
{
List result = new List();
diff --git a/MonitoringTechnology/Views/SystemOperationView.xaml b/MonitoringTechnology/Views/SystemOperationView.xaml
index e66a81d..78cb0cf 100644
--- a/MonitoringTechnology/Views/SystemOperationView.xaml
+++ b/MonitoringTechnology/Views/SystemOperationView.xaml
@@ -104,6 +104,8 @@
Click="buttonSend_Click" Template="{StaticResource ButtonTemplate}"/>
+
diff --git a/MonitoringTechnology/Views/SystemOperationView.xaml.cs b/MonitoringTechnology/Views/SystemOperationView.xaml.cs
index b7756f0..457b741 100644
--- a/MonitoringTechnology/Views/SystemOperationView.xaml.cs
+++ b/MonitoringTechnology/Views/SystemOperationView.xaml.cs
@@ -141,6 +141,17 @@ namespace MonitoringTechnology.Views
}
+ ///
+ /// 系统校时
+ ///
+ ///
+ ///
+ private void TimeCheck_Click(object sender, RoutedEventArgs e)
+ {
+ TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
+ send_port_time(Convert.ToInt64(ts.TotalSeconds).ToString("X2"));
+ }
+
///
/// 收数据
///
@@ -151,6 +162,16 @@ namespace MonitoringTechnology.Views
AddLog($"收到数据 <<< {args.ToHex()}");
}
+ ///
+ /// 自定义数据显示功能 --数据解析
+ ///
+ ///
+ ///
+ private void BleDevice_ReceiveValueChanged(GattCharacteristic sender, string data)
+ {
+ AddLog($"{data}");
+ }
+
///
/// 连接按钮
///
@@ -322,7 +343,7 @@ namespace MonitoringTechnology.Views
bool success = await systemOperationViewModel.BleDevice.Write(_send);
if (success)
{
- AddLog("版本查询指令发送成功");
+ AddLog("已发送版本查询指令,请稍等!");
PreservationData(_send.ToList(), "版本查询指令");
}
}
@@ -406,7 +427,7 @@ namespace MonitoringTechnology.Views
bool success = await systemOperationViewModel.BleDevice.Write(_send);
if (success)
{
- AddLog("版本升级指令发送成功");
+ AddLog("已发送版本升级指令,请稍等!");
PreservationData(_send.ToList(), "版本升级指令");
}
}
@@ -423,8 +444,9 @@ namespace MonitoringTechnology.Views
public SystemOperationView(BleDevice device) : this()
{
systemOperationViewModel.BleDevice = device;
- systemOperationViewModel.BleDevice.CharacteristicValueChanged += BleDevice_CharacteristicValueChanged;
- systemOperationViewModel.BleDevice.ConnectionStateChanged += BleDevice_ConnectionStateChanged;
+ systemOperationViewModel.BleDevice.CharacteristicValueChanged += BleDevice_CharacteristicValueChanged;//收数据
+ systemOperationViewModel.BleDevice.ConnectionStateChanged += BleDevice_ConnectionStateChanged;//连接变化
+ systemOperationViewModel.BleDevice.ReceiveValueChanged += BleDevice_ReceiveValueChanged;//自定义数据显示功能
DataContext = systemOperationViewModel;
isSelect = true;//已选择蓝牙设备
if (device.IsConnected == true)
@@ -461,6 +483,37 @@ namespace MonitoringTechnology.Views
});
}
+ ///
+ /// 时间同步
+ ///
+ ///
+ public async void send_port_time(string time)
+ {
+ if (isConnection)
+ {
+ byte[] time_byte = tools.ConvertHexStringToBytes(time);
+ int a = 0xFE + 0xEF + 0x00 + 0x08 + 0x00 + 0x06;
+ for (int i = 0; i < time_byte.Length; i++)
+ {
+ a += time_byte[i];
+ }
+ //取十六进制最低两位
+ a = a & 0xFF;
+ string send_mes = "FEEF00080006" + time + a.ToString("X2") + "16";
+ byte[] bytea = tools.ConvertHexStringToBytes(send_mes);
+ //调用发送的那个方法
+ bool success = await systemOperationViewModel.BleDevice.Write(bytea);
+ if (success)
+ {
+ AddLog("时间正在同步中,请稍等!");
+ }
+ }
+ else
+ {
+ AddLog("请搜索附近蓝牙设备并连接后重试!!!");
+ }
+ }
+
///
/// int数组转化为byte数组
///
@@ -587,5 +640,7 @@ namespace MonitoringTechnology.Views
tools.AddLgoToTXT(FileModel.File_Name + FileModel.File_Time.ToString("yyyy-MM-dd") + "日志" + ".txt", FileModel.File_Path + FileModel.File_Time.ToString("yyyy-MM-dd") + @"\", _localtime + " " + type + ": " + rec_16 + "\r\n");
}
#endregion
+
+
}
}