|
@@ -176,82 +176,6 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
|
|
|
return new ResMsg(true, "模块ID:" + psp.SourceNumber + ",信息:" + message);
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 验证内包数据包 并简单拆包 UpSimplePack(ClientInfo info,byte[] originData)
|
|
|
- /// </summary>
|
|
|
- /// <param name="info">自带的附加数据的类</param>
|
|
|
- /// <param name="originData">接收的数据包 byte数组</param>
|
|
|
- public static void UpSimplePack(ClientInfo info, byte[] originData)
|
|
|
- {
|
|
|
- // 定义变量
|
|
|
- byte[] _cmdTypeArr = new byte[2], dataBuffer;
|
|
|
- int dataBufferLength = 0, num = 0;
|
|
|
- byte _chekCode = 0, chekCode = 0;//下划线 数据包中的校验和 没有下滑线 计算出的校验和
|
|
|
-
|
|
|
- // 初始话赋值
|
|
|
- info.flag = true;
|
|
|
- info.CmdType = CmdType.NoCmd;
|
|
|
- info.BArr = null;
|
|
|
- //info.
|
|
|
- LogUtil.WriteLog($"接收的数据: " + ByteToHexCode(originData, 0, originData.Length));
|
|
|
-
|
|
|
- // 检查包头包尾 获取数据长度 把数据临时存入 databuffer中
|
|
|
- for (int i = 0; i < originData.Length; i++)
|
|
|
- {
|
|
|
- // 检测包的位置
|
|
|
- if (originData[i] == 0xA5 && originData[originData.Length - 1] == 0xAA)
|
|
|
- {
|
|
|
- num = i;
|
|
|
- // 包的最小长度为18
|
|
|
- if (18 < originData.Length)
|
|
|
- {
|
|
|
- byte hL = originData[num + 1];
|
|
|
- byte lL = originData[num + 2];
|
|
|
- // 获取数据区域的长度
|
|
|
- dataBufferLength = (hL << 8) + lL;
|
|
|
- if (dataBufferLength > 12)
|
|
|
- {
|
|
|
- dataBufferLength = dataBufferLength - 10; // 减去 控制码和水表地址的长度
|
|
|
- chekCode = SumCheck(originData, num + 4, originData.Length - 2);// 计算校验和
|
|
|
- _chekCode = originData[originData.Length - 2];//校验和
|
|
|
- if (true)// (chekCode == _chekCode)
|
|
|
- {
|
|
|
- //_cmdTypeArr[0] = originData[num + 4];//控制码
|
|
|
- // _cmdTypeArr[1] = originData[num + 5];//辅助控制码
|
|
|
- dataBuffer = new byte[dataBufferLength];
|
|
|
- //info.CmdType = GetFunc(_cmdTypeArr);// 指令类型
|
|
|
- info.Address = ByteToHexCodeLToH(originData, num + 7, num + 13);
|
|
|
- //将数据域复制到 dataBufferLength 数组中 表地址后边的数据域
|
|
|
- Array.Copy(originData, num + 14, dataBuffer, 0, dataBufferLength - 10);
|
|
|
- info.BArr = dataBuffer;
|
|
|
- info.msg = "校验成功";
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- info.flag = false;
|
|
|
- info.msg = "校验失败";
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- info.flag = false;
|
|
|
- info.msg = "数据帧不完整";
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- // Debug.WriteLine("################" + num + 1 + "#############" + num + 2);
|
|
|
- info.flag = false;
|
|
|
- info.msg = "数据帧不完整";
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 电池供电进行内包拆包操作
|
|
|
/// </summary>
|
|
@@ -288,9 +212,8 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
|
|
|
{
|
|
|
strVol = "1";
|
|
|
}
|
|
|
- string strReturnData = "";
|
|
|
- CellMonitorConvertByType.ConvertData(data, 9, "0101", "2", out strReturnData);
|
|
|
- int num = count(strReturnData);
|
|
|
+ CellMonitorConvertByType.ConvertData(data, 9, "0101", "2", out string strReturnData);
|
|
|
+ int num = Count(strReturnData);
|
|
|
int num2 = 0;
|
|
|
int num3 = 0;
|
|
|
int num4 = 0;
|
|
@@ -317,8 +240,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
|
|
|
for (int i = 0; i < cm._recordCount; i++)
|
|
|
{
|
|
|
CellRecord item = new CellRecord();
|
|
|
- string str3 = "";
|
|
|
- CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4), "int", "4", out str3);
|
|
|
+ CellMonitorConvertByType.ConvertData(data, 20 + ((i * (num + 1)) * 4), "int", "4", out string str3);
|
|
|
item.PickTime = RecordTime(str3);
|
|
|
int num7 = 0;
|
|
|
int num8 = 0;
|
|
@@ -326,16 +248,14 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
|
|
|
{
|
|
|
if ((num5 == 11) && ((ch2 = ch = strReturnData[15 - num5]).ToString() == "1"))
|
|
|
{
|
|
|
- string str4 = "";
|
|
|
num8 = 1;
|
|
|
- CellMonitorConvertByType.ConvertData(data, (20 + (((((1 + num3) + num4) + num2) * 4) * i)) + ((num3 + 1) * 4), "0101", "4", out str4);
|
|
|
+ CellMonitorConvertByType.ConvertData(data, (20 + (((((1 + num3) + num4) + num2) * 4) * i)) + ((num3 + 1) * 4), "0101", "4", out string str4);
|
|
|
item.Switch = OrgnizeSwitch(str4, strVol);
|
|
|
}
|
|
|
else if (strReturnData.Substring((strReturnData.Length - 1) - num5, 1) == "1")
|
|
|
{
|
|
|
num7++;
|
|
|
- string str5 = "";
|
|
|
- CellMonitorConvertByType.ConvertData(data, (20 + ((i * (((num3 + num4) + num2) + 1)) * 4)) + ((num7 + num8) * 4), "float", "", out str5);
|
|
|
+ CellMonitorConvertByType.ConvertData(data, (20 + ((i * (((num3 + num4) + num2) + 1)) * 4)) + ((num7 + num8) * 4), "float", "", out string str5);
|
|
|
MonitorValue value2 = new MonitorValue();
|
|
|
value2.Name = "A" + num5.ToString();
|
|
|
value2.Value = str5;
|
|
@@ -450,7 +370,7 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- private static int count(string strformat)
|
|
|
+ private static int Count(string strformat)
|
|
|
{
|
|
|
int num = 0;
|
|
|
for (int i = 0; i < strformat.Length; i++)
|
|
@@ -833,28 +753,28 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
|
|
|
|
|
|
foreach (KeyValuePair<string, CommOption> analogData in _analogData)
|
|
|
{
|
|
|
- if (analogData.Value.IsDerivate == "基础量")
|
|
|
+ try
|
|
|
{
|
|
|
- string subDeviceIndex = analogData.Value.SubDeviceIndex;
|
|
|
- int intStartIndex = (Convert.ToInt32(analogData.Value.StartIndex) - _analogStartAddress) * 2;
|
|
|
- if ((intStartIndex < 0) || (intStartIndex > (ReturnData.Length - 1)))
|
|
|
- {
|
|
|
- analogData.Value.Value = "量起始地址设置错误";
|
|
|
- }
|
|
|
- else
|
|
|
+ if (analogData.Value.IsDerivate == "基础量")
|
|
|
{
|
|
|
- try
|
|
|
+ string subDeviceIndex = analogData.Value.SubDeviceIndex;
|
|
|
+ int intStartIndex = (Convert.ToInt32(analogData.Value.StartIndex) - _analogStartAddress) * 2;
|
|
|
+ if ((intStartIndex < 0) || (intStartIndex > (ReturnData.Length - 1)))
|
|
|
{
|
|
|
- string str2;
|
|
|
- msg = ModBusConvertByType.ConvertData(ReturnData, intStartIndex, analogData.Value.ConvertMode, analogData.Value.ConvertParam, out str2);
|
|
|
- msg = analogData.Value.ExplainData(str2);
|
|
|
+ analogData.Value.Value = "量起始地址设置错误";
|
|
|
}
|
|
|
- catch (Exception exception)
|
|
|
+ else
|
|
|
{
|
|
|
-
|
|
|
+ msg = ModBusConvertByType.ConvertData(ReturnData, intStartIndex, analogData.Value.ConvertMode, analogData.Value.ConvertParam, out string str2);
|
|
|
+ msg = analogData.Value.ExplainData(str2);
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -866,23 +786,24 @@ namespace NB_IOT_TCP_HP_SOCKET.WWKJUtil
|
|
|
/// <param name="_switchStartAddress"></param>
|
|
|
public static void OrganizeSwitchData(byte[] ReturnData, Dictionary<string, CommOption> _switchData, int _switchStartAddress)
|
|
|
{
|
|
|
- int num;
|
|
|
foreach (KeyValuePair<string, CommOption> switchData in _switchData)
|
|
|
{
|
|
|
- if (switchData.Value.IsDerivate == "基础量")
|
|
|
+ try
|
|
|
{
|
|
|
- string subDeviceIndex = switchData.Value.SubDeviceIndex;
|
|
|
- int intStartIndex = (Convert.ToInt32(switchData.Value.StartIndex) - _switchStartAddress) * 2;
|
|
|
- try
|
|
|
- {
|
|
|
- string str2;
|
|
|
- ResMsg msg = ModBusConvertByType.ConvertData(ReturnData, intStartIndex, switchData.Value.ConvertMode, switchData.Value.ConvertParam, out str2);
|
|
|
- msg = switchData.Value.ExplainData(str2);
|
|
|
- }
|
|
|
- catch (Exception exception)
|
|
|
+ if (switchData.Value.IsDerivate == "基础量")
|
|
|
{
|
|
|
+ string subDeviceIndex = switchData.Value.SubDeviceIndex;
|
|
|
+ int intStartIndex = (Convert.ToInt32(switchData.Value.StartIndex) - _switchStartAddress) * 2;
|
|
|
+
|
|
|
+ ResMsg msg = ModBusConvertByType.ConvertData(ReturnData, intStartIndex, switchData.Value.ConvertMode, switchData.Value.ConvertParam, out string str2);
|
|
|
+ msg = switchData.Value.ExplainData(str2);
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|