Przeglądaj źródła

通讯库修改

样式分支
pry 2 lat temu
rodzic
commit
6dcaae9fcb
7 zmienionych plików z 367 dodań i 169 usunięć
  1. +7
    -2
      BPASmartClient.CustomResource/BPASmartClient.CustomResource.csproj
  2. +76
    -15
      BPASmartClient.Helper/ExpandMethod.cs
  3. +2
    -1
      BPASmartClient.Modbus/BPASmartClient.Modbus.csproj
  4. +211
    -0
      BPASmartClient.Modbus/MODBUS.cs
  5. +61
    -141
      BPASmartClient.Modbus/ModbusTcp.cs
  6. +6
    -6
      BPASmartClient.MorkS/Control_Morks.cs
  7. +4
    -4
      BPASmartClient/App.config

+ 7
- 2
BPASmartClient.CustomResource/BPASmartClient.CustomResource.csproj Wyświetl plik

@@ -7,12 +7,18 @@
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Fonts\Debug\**" />
<EmbeddedResource Remove="Fonts\Debug\**" />
<None Remove="Fonts\Debug\**" />
<Page Remove="Fonts\Debug\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Properties\App.xaml.cs" />
</ItemGroup>

<ItemGroup>
<None Remove="Fonts\Debug\iconfont.ttf" />
<None Remove="Fonts\iconfont.ttf" />
<None Remove="Image\bg.png" />
<None Remove="Image\btn_close.png" />
@@ -173,7 +179,6 @@
</ItemGroup>

<ItemGroup>
<Resource Include="Fonts\Debug\iconfont.ttf" />
<Resource Include="Image\btn_close.png" />
<Resource Include="Image\ComboBoxPopSelect.png" />
<Resource Include="Image\ComboBoxSelect.png" />


+ 76
- 15
BPASmartClient.Helper/ExpandMethod.cs Wyświetl plik

@@ -43,11 +43,6 @@ namespace BPASmartClient.Helper
{
action?.Invoke();
callback?.Invoke();
//if (action != null)
//{
// action();
// if (callback != null) callback();
//}
}

/// <summary>
@@ -60,22 +55,12 @@ namespace BPASmartClient.Helper
{
action?.Invoke(par);
callback?.Invoke();
//if (action != null)
//{
// action(par);
// if (callback != null) callback();
//}
}

public static void Invokes(this Action<object[]> action, object[] par, Action callback)
{
action?.Invoke(par);
callback?.Invoke();
//if (action != null)
//{
// action(par);
// if (callback != null) callback();
//}
}


@@ -94,5 +79,81 @@ namespace BPASmartClient.Helper
}
return ReturnVlaue;
}

/// <summary>
/// 字节数组转换成 ushort 数组
/// </summary>
/// <param name="bytes">要转换的字节数组</param>
/// <param name="reverse">字节高度顺序控制</param>
/// <returns></returns>
public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false)
{
int len = bytes.Length;

byte[] srcPlus = new byte[len + 1];
bytes.CopyTo(srcPlus, 0);
int count = len >> 1;

if (len % 2 != 0)
{
count += 1;
}

ushort[] dest = new ushort[count];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
}
}

return dest;
}

/// <summary>
/// ushort 数组转换成字节数组
/// </summary>
/// <param name="src">需要转换的 ushort数组</param>
/// <param name="reverse">高低字节的设置</param>
/// <returns></returns>
public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false)
{

int count = src.Length;
byte[] dest = new byte[count << 1];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 8);
dest[i * 2 + 1] = (byte)(src[i] >> 0);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 0);
dest[i * 2 + 1] = (byte)(src[i] >> 8);
}
}
return dest;
}








}
}

+ 2
- 1
BPASmartClient.Modbus/BPASmartClient.Modbus.csproj Wyświetl plik

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@@ -9,6 +9,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" />
<ProjectReference Include="..\BPASmartClient.Message\BPASmartClient.Message.csproj" />
</ItemGroup>



+ 211
- 0
BPASmartClient.Modbus/MODBUS.cs Wyświetl plik

@@ -0,0 +1,211 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.Modbus
{
public class MODBUS
{
/// <summary>
/// 赋值string
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
/// <returns></returns>
public static void SetString(ushort[] src, int start, string value)
{
byte[] bytesTemp = Encoding.UTF8.GetBytes(value);
ushort[] dest = Bytes2Ushorts(bytesTemp);
dest.CopyTo(src, start);
}

/// <summary>
/// 获取string
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string GetString(ushort[] src, int start, int len)
{
ushort[] temp = new ushort[len];
for (int i = 0; i < len; i++)
{
temp[i] = src[i + start];
}
byte[] bytesTemp = Ushorts2Bytes(temp);
string res = Encoding.UTF8.GetString(bytesTemp).Trim(new char[] { '\0' });
return res;
}

/// <summary>
/// 赋值Real类型数据
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
public static void SetReal(ushort[] src, int start, float value)
{
byte[] bytes = BitConverter.GetBytes(value);

ushort[] dest = Bytes2Ushorts(bytes);

dest.CopyTo(src, start);
}

/// <summary>
/// 获取float类型数据
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns></returns>
public static float GetReal(ushort[] src, int start)
{
ushort[] temp = new ushort[2];
for (int i = 0; i < 2; i++)
{
temp[i] = src[i + start];
}
byte[] bytesTemp = Ushorts2Bytes(temp);
float res = BitConverter.ToSingle(bytesTemp, 0);
return res;
}

/// <summary>
/// 赋值Short类型数据
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
public static void SetShort(ushort[] src, int start, short value)
{
byte[] bytes = BitConverter.GetBytes(value);

ushort[] dest = Bytes2Ushorts(bytes);

dest.CopyTo(src, start);
}

/// <summary>
/// 获取short类型数据
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns></returns>
public static short GetShort(ushort[] src, int start)
{
ushort[] temp = new ushort[1];
temp[0] = src[start];
byte[] bytesTemp = Ushorts2Bytes(temp);
short res = BitConverter.ToInt16(bytesTemp, 0);
return res;
}


public static bool[] GetBools(ushort[] src, int start, int num)
{
ushort[] temp = new ushort[num];
for (int i = start; i < start + num; i++)
{
temp[i] = src[i + start];
}
byte[] bytes = Ushorts2Bytes(temp);

bool[] res = Bytes2Bools(bytes);

return res;
}

private static bool[] Bytes2Bools(byte[] b)
{
bool[] array = new bool[8 * b.Length];

for (int i = 0; i < b.Length; i++)
{
for (int j = 0; j < 8; j++)
{
array[i * 8 + j] = (b[i] & 1) == 1;//判定byte的最后一位是否为1,若为1,则是true;否则是false
b[i] = (byte)(b[i] >> 1);//将byte右移一位
}
}
return array;
}

private static byte Bools2Byte(bool[] array)
{
if (array != null && array.Length > 0)
{
byte b = 0;
for (int i = 0; i < 8; i++)
{
if (array[i])
{
byte nn = (byte)(1 << i);//左移一位,相当于×2
b += nn;
}
}
return b;
}
return 0;
}

private static ushort[] Bytes2Ushorts(byte[] src, bool reverse = false)
{
int len = src.Length;

byte[] srcPlus = new byte[len + 1];
src.CopyTo(srcPlus, 0);
int count = len >> 1;

if (len % 2 != 0)
{
count += 1;
}

ushort[] dest = new ushort[count];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
}
}

return dest;
}

private static byte[] Ushorts2Bytes(ushort[] src, bool reverse = false)
{

int count = src.Length;
byte[] dest = new byte[count << 1];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 8);
dest[i * 2 + 1] = (byte)(src[i] >> 0);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 0);
dest[i * 2 + 1] = (byte)(src[i] >> 8);
}
}
return dest;
}
}
}

+ 61
- 141
BPASmartClient.Modbus/ModbusTcp.cs Wyświetl plik

@@ -1,9 +1,11 @@
using BPASmartClient.Message;
using BPASmartClient.Helper;
using BPASmartClient.Message;
using NModbus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

@@ -107,28 +109,6 @@ namespace BPASmartClient.Modbus
return -1;
}

//public void Readbool(ushort startAddress, ushort len, Action<bool[]> action)
//{
// object result;
// result = Read(startAddress, CommandType.Coils, len);
// if (result != null)
// {
// if (result is bool[] bools)
// {
// if (bools.Length == len)
// {
// action(bools);
// }
// }
// else if (result is bool bl)
// {
// List<bool> boolLists = new List<bool>();
// boolLists.Add(bl);
// action(boolLists.ToArray());
// }
// }
//}

public object Read(string address, ushort len = 1, byte slaveAddress = 1)
{
if (address == null || tcpClient == null) return default(object);
@@ -228,125 +208,65 @@ namespace BPASmartClient.Modbus
}
}

//public object Read(ushort startAddress, CommandType readType, ushort num = 1, byte slaveAddress = 1)
//{
// object result = new object();
// if (tcpClient == null) return result;
// if (num <= 0) return result;
// try
// {
// switch (readType)
// {
// case CommandType.Coils:
// result = master.ReadCoils(slaveAddress, startAddress, num);
// break;
// case CommandType.Inputs:
// result = master.ReadInputs(slaveAddress, startAddress, num);
// break;
// case CommandType.HoldingRegisters:
// result = master.ReadHoldingRegisters(slaveAddress, startAddress, num);
// break;
// case CommandType.InputRegisters:
// result = master.ReadInputRegisters(slaveAddress, startAddress, num);
// break;
// default:
// break;
// }
// if (result == null) return new object();
// if (result is bool[] bools)
// {
// if (bools.Length == 1)
// return bools[0];
// else
// return bools;
// }
// if (result is ushort[] ushorts)
// {
// if (ushorts.Length == 1)
// return ushorts[0];
// else
// return ushorts;
// }
// }
// catch (Exception ex)
// {
// MessageLog.GetInstance.ShowEx($"读取地址:【{startAddress}】,读取类型:【{readType}】出错,{ex.ToString()}");
// if (ex.InnerException is SocketException)
// {
// tcpClient = null;
// Connect();
// }
// }
// return result;
//}

//public bool Write(ushort startAddress, CommandType writeType, object InputValue, byte slaveAddress = 1)
//{
// bool result = false;
// if (tcpClient == null) return result;
// if (!(InputValue is bool || InputValue is bool[] || InputValue is ushort || InputValue is ushort[])) return result;
// try
// {
// switch (writeType)
// {
// case CommandType.Coils:
// if (InputValue is bool boolValue)
// master.WriteSingleCoil(slaveAddress, startAddress, boolValue);

// if (InputValue is bool[] boolsValue)
// master.WriteMultipleCoils(slaveAddress, startAddress, boolsValue);
// break;
// case CommandType.HoldingRegisters:
// if (InputValue is ushort ushortValue)
// master.WriteSingleRegister(slaveAddress, startAddress, ushortValue);

// if (InputValue is ushort[] ushortsValue)
// {
// int len = 100;
// if (ushortsValue.Length > len)
// {
// List<ushort[]> ushortLists = new List<ushort[]>();
// for (int i = 0; i < ushortsValue.Length / len; i++)
// {
// ushortLists.Add(ushortsValue.Skip(0).Take(len).ToArray());
// }
// int y = ushortsValue.Length % len;

// if (y > 0)
// {
// ushortLists.Add(ushortsValue.Skip(ushortsValue.Length - y).Take(y).ToArray());
// }
// foreach (var item in ushortLists)
// {
// master.WriteMultipleRegisters(slaveAddress, startAddress, item);
// startAddress += (ushort)item.Length;
// }
// }
// else
// {
// master.WriteMultipleRegisters(slaveAddress, startAddress, ushortsValue);
// }

// }

// break;
// default:
// break;
// }
// }
// catch (Exception ex)
// {
// MessageLog.GetInstance.ShowEx(ex.ToString());
// if (ex.InnerException is SocketException)
// {
// tcpClient = null;
// Connect();
// }
// return false;
// }
// return true;
//}

#region 字符串数据读写
/// <summary>
/// 赋值string
/// </summary>
/// <param name="StartAddress"></param>
/// <param name="value"></param>
/// <returns></returns>
public void SetString(string StartAddress, string value)
{
var bytes = Encoding.UTF8.GetBytes(value);
Write(StartAddress, bytes.BytesToUshorts());
}

/// <summary>
/// 获取string
/// </summary>
/// <param name="StartAddress"></param>
/// <param name="len"></param>
/// <returns></returns>
public string GetString(string StartAddress, ushort len)
{
var res = Read(StartAddress, len);
if (res != null && res is ushort[] ushorts)
{
return Encoding.UTF8.GetString(ushorts.UshortsToBytes()).Trim(new char[] { '\0' });
}
return String.Empty;
}

#endregion

#region 浮点数数据读写
/// <summary>
/// 赋值Real类型数据
/// </summary>
/// <param name="StartAddress"></param>
/// <param name="value"></param>
public void SetReal(string StartAddress, float value)
{
var bytes = BitConverter.GetBytes(value);
Write(StartAddress, bytes.BytesToUshorts());
}

/// <summary>
/// 获取float类型数据
/// </summary>
/// <param name="StartAddress"></param>
/// <returns></returns>
public float GetReal(string StartAddress)
{
var res = Read(StartAddress, 2);
if (res != null && res is ushort[] ushorts)
{
return BitConverter.ToSingle(ushorts.UshortsToBytes(), 0);
}
return 0;
}
#endregion

#region 批量数据读取
/// <summary>


+ 6
- 6
BPASmartClient.MorkS/Control_Morks.cs Wyświetl plik

@@ -512,12 +512,12 @@ namespace BPASmartClient.MorkS


//新增,待测试
//if (mORKS.RbOutMealComplete)
//{
// ResetCookComplete();
// mORKS.CookCompleteFlatBit = false;
// DeviceProcessLogShow("取餐过程中复位出餐完成信号");
//}
if (mORKS.RbOutMealComplete)
{
ResetCookComplete();
mORKS.CookCompleteFlatBit = false;
DeviceProcessLogShow("取餐过程中复位出餐完成信号");
}


mORKS.OutMealId = mORKS.IngredientsCompleteId;


+ 4
- 4
BPASmartClient/App.config Wyświetl plik

@@ -7,14 +7,14 @@
<add key="IsEnableTest" value="false"/>

<!--开发环境-->
<add key="apollouri" value="http://10.2.1.21:28080/"/>
<!--<add key="apollouri" value="http://10.2.1.21:28080/"/>
<add key="AppId" value="dev1_common"/>
<add key ="Namespaces" value="DEV.Config"/>
<add key ="Namespaces" value="DEV.Config"/>-->

<!--正式环境-->
<!--<add key="apollouri" value="http://47.108.65.220:28080/"/>
<add key="apollouri" value="http://47.108.65.220:28080/"/>
<add key="AppId" value="order"/>
<add key ="Namespaces" value="TEST1.Config"/>-->
<add key ="Namespaces" value="TEST1.Config"/>

<!--阿里云上报启动方式:API 或者 LOCAL-->
<!--API :通过客户端ID,调用接口查询“设备连接信息”-->


Ładowanie…
Anuluj
Zapisz