Browse Source

完成咖啡机、单片机内部控制

样式分支
fyf 2 years ago
parent
commit
b88d7c97db
13 changed files with 396 additions and 590 deletions
  1. +0
    -1
      BPASmartClient.DRCoffee/BPASmartClient.DRCoffee.csproj
  2. +82
    -82
      BPASmartClient.DRCoffee/CoffeeMachine.cs
  3. +0
    -76
      BPASmartClient.DRCoffee/MorkCStatus.cs
  4. +6
    -0
      BPASmartClient.KLMCoffee/Protocal/SystemStatusModel.cs
  5. +8
    -4
      BPASmartClient.Model/单片机/SCChipEvent.cs
  6. +2
    -1
      BPASmartClient.Model/咖啡机/DRCoffeeEvent.cs
  7. +1
    -1
      BPASmartClient.SCChip/BPASmartClient.SCChip.csproj
  8. +0
    -128
      BPASmartClient.SCChip/ChipStatus.cs
  9. +0
    -42
      BPASmartClient.SCChip/CommandEvent.cs
  10. +0
    -225
      BPASmartClient.SCChip/CommandHandler.cs
  11. +293
    -29
      BPASmartClient.SCChip/ICChipMachine.cs
  12. +1
    -1
      BPASmartClient/MainWindow.xaml
  13. +3
    -0
      BPASmartClient/MainWindow.xaml.cs

+ 0
- 1
BPASmartClient.DRCoffee/BPASmartClient.DRCoffee.csproj View File

@@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.MessageCommunication\BPASmartClient.MessageCommunication.csproj" />
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
<ProjectReference Include="..\BPASmartClient.SerialPort\BPASmartClient.SerialPort.csproj" />


+ 82
- 82
BPASmartClient.DRCoffee/CoffeeMachine.cs View File

@@ -15,78 +15,33 @@ namespace BPASmartClient.DRCoffee
/// <summary>
/// 咖啡机
/// </summary>
public class CoffeeMachine: BasePeripheral
public class CoffeeMachine :BasePeripheral
{
//通讯代理
SerialPortClient commProxy = null;
//数据仓库
private DataStorage<byte> dataStorage = new DataStorage<byte>();
//主线程运行标识
private bool running = false;
//是否下发指令,主线程等待
private bool free = true;

private DrCoffeeStatus drCoffeeStatus;
/// <summary>
/// 咖啡机状态
/// </summary>
public DrCoffeeStatus CurrentCoffeeStatus
{
get { return drCoffeeStatus; }
set
{
if (drCoffeeStatus != value)
{
drCoffeeStatus = value;
CoffeeStatusChanged?.Invoke(value);
}
}
}
private DrCoffeeAppStatus coffeeAppStatus;
/// <summary>
/// 应用状态
/// </summary>
public DrCoffeeAppStatus CurrentCoffeeAppStatus
{
get { return coffeeAppStatus; }
set
{
if (coffeeAppStatus != value)
{
coffeeAppStatus = value;
CoffeeAppStatusChanged?.Invoke(value);
}
}
}

public Action<string> SendCallback;
public Action<string> ReciveCallback;

/// <summary>
/// 咖啡机状态改变回调
/// </summary>
public Action<DrCoffeeStatus> CoffeeStatusChanged;
/// <summary>
/// 应用状态改变回调
/// </summary>
public Action<DrCoffeeAppStatus> CoffeeAppStatusChanged;
/// <summary>
/// Dr咖啡机基础协议
/// </summary>
private bool free = false;
//状态询问指令
private byte[] cmdAsk;
//Dr咖啡机基础协议
private DrCoffeePackage drinksOrder = new DrCoffeePackage();

/// <summary>
/// 串口COM口
/// </summary>
//串口COM口
public string PortName { get; set; }
//串口波特率
public string BaudRate { get; set; }
//心跳时间
private DateTime lastRefreshTime = DateTime.MinValue;
/// <summary>
/// 串口波特率
/// 是否在线
/// </summary>
public string BaudRate { get; set; }

public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
public CoffeeMachine()
{
DrCoffeePackage package = new DrCoffeePackage();
package.CommCmd = DrCoffeeCommCmd.饮品制作指令;
cmdAsk = DrCoffee.Packe(package);
}

/// <summary>
@@ -94,10 +49,17 @@ namespace BPASmartClient.DRCoffee
/// </summary>
public override void Start()
{
commProxy.Start();
running = true;
MainLoop();
try
{
commProxy.Start();
IsConnected = true;
free = false;
MainLoop();
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
}

/// <summary>
@@ -105,8 +67,16 @@ namespace BPASmartClient.DRCoffee
/// </summary>
public override void Stop()
{
commProxy.Stop();
running = false;
try
{
commProxy.Stop();
IsConnected = false;
free = true;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
}

/// <summary>
@@ -114,15 +84,14 @@ namespace BPASmartClient.DRCoffee
/// </summary>
private void MainLoop()
{
//ThreadManage.GetInstance.StartLong(new Action(() =>
//{
// if (free)
// {
// commProxy.SendData(commandHandler.GetStatusAsk());
// SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
// }
// Thread.Sleep(200);
//}),"咖啡机询问线程");
ThreadManage.GetInstance.StartLong(new Action(() =>
{
if (!free)
{
commProxy.SendData(cmdAsk);
}
Thread.Sleep(200);
}),"咖啡机询问线程");

ThreadManage.GetInstance.StartLong(new Action(() =>
{
@@ -137,9 +106,8 @@ namespace BPASmartClient.DRCoffee
{
temp.Add(item);
var package = DrCoffee.UnPack(temp.ToArray());
ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
temp.Clear();
MorkCStatus.GetInstance().ProcessPackage(package);
ProcessPackage(package);
}
else
{
@@ -159,14 +127,45 @@ namespace BPASmartClient.DRCoffee
}
}
Thread.Sleep(5);
}), "咖啡机解析线程");
}),"咖啡机解析线程");
}

protected override void InitStatus()
/// <summary>
/// 咖啡机状态解析
/// </summary>
/// <param name="package"></param>
public void ProcessPackage(DrCoffeePackage package)
{
if (((DrCoffeeStatus)status["CoffeeStatus"]) == DrCoffeeStatus.Running && package.Status != DrCoffeeStatus.Running)
{
status["CoffeeStatus"] = package.Status;
lastRefreshTime = DateTime.Now;
new DRCoffee_CoffeEndCookEvent() { Id = DeviceId }.Publish();
}
else status["CoffeeStatus"] = package.Status;
status["AppStatus"] = package.ApplicationStatus;
status["Warning"] = package.Warning;
status["Fault"] = package.Fault;

if ((DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Warning
|| (DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Fault
|| (DrCoffeeWarning)status["Warning"] != DrCoffeeWarning.无警告
|| (DrCoffeeFault)status["Fault"] != DrCoffeeFault.无故障
)
{
IsWork = false;
}
else
IsWork = true;
}

protected override void InitStatus()
{
status["CoffeeStatus"] = DrCoffeeStatus.Wait;
status["AppStatus"] = DrCoffeeAppStatus.应用无状态;
status["Warning"] = DrCoffeeWarning.无警告;
status["Fault"] = DrCoffeeFault.无故障;
}

public override void Init()
{
@@ -181,7 +180,7 @@ namespace BPASmartClient.DRCoffee
free = true;
Thread.Sleep(200);
drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
//drinksOrder.DrinksCode = ((DRCoffee_MakeCoffeeEvent)@event).CommCmd;
drinksOrder.DrinksCode = (DrCoffeeDrinksCode)((DRCoffee_MakeCoffeeEvent)@event).DrinkCode;
commProxy.SendData(DrCoffee.Packe(drinksOrder));
Thread.Sleep(200);
free = false;
@@ -218,7 +217,7 @@ namespace BPASmartClient.DRCoffee
{
free = true;
Thread.Sleep(200);
//drinksOrder.CommCmd = (DrCoffeeCommCmd)int.Parse(e.obj_MessageObj.ToString());
drinksOrder.CommCmd = (DrCoffeeCommCmd)((DRCoffee_CoffeeCommCmdEvent)@event).CommCmd;
commProxy.SendData(DrCoffee.Packe(drinksOrder));
Thread.Sleep(200);
free = false;
@@ -229,5 +228,6 @@ namespace BPASmartClient.DRCoffee
}
});
}

}
}

+ 0
- 76
BPASmartClient.DRCoffee/MorkCStatus.cs View File

@@ -1,76 +0,0 @@
using BPASmartClient.DRCoffee;
using BPASmartClient.MessageCommunication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.DRCoffee
{
public class MorkCStatus
{
private volatile static MorkCStatus _Instance;
public static MorkCStatus GetInstance() => _Instance ?? (_Instance = new MorkCStatus());
private MorkCStatus() { }

private DateTime lastRefreshTime = DateTime.MinValue;
/// <summary>
/// 是否在线
/// </summary>
public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
/// <summary>
/// 咖啡机状态
/// </summary>
public DrCoffeeStatus CoffeeStatus { get; set; }
/// <summary>
/// 应用状态
/// </summary>
public DrCoffeeAppStatus AppStatus { get; set; }
/// <summary>
/// 警告信息
/// </summary>
public DrCoffeeWarning Warning { get; set; }
/// <summary>
/// 故障信息
/// </summary>
public DrCoffeeFault Fault { get; set; }

public bool CanDo
{
get

{
if (!OnLine)
return false;
if (Warning != DrCoffeeWarning.无警告)
return false;
if (Fault != DrCoffeeFault.无故障)
return false;
return true;
}
}

/// <summary>
/// 咖啡机状态解析
/// </summary>
/// <param name="package"></param>
public void ProcessPackage(DrCoffeePackage package)
{
if (CoffeeStatus == DrCoffeeStatus.Running && package.Status != DrCoffeeStatus.Running)
{
CoffeeStatus = package.Status;
Class_InnerMessageBus.GetInstance().PostMessage(this,Class_MessageName.DRCoffee_CoffeEndCook,"");
}
else {
CoffeeStatus = package.Status;
}

AppStatus = package.ApplicationStatus;
Warning = package.Warning;
Fault = package.Fault;

lastRefreshTime = DateTime.Now;
}
}
}

+ 6
- 0
BPASmartClient.KLMCoffee/Protocal/SystemStatusModel.cs View File

@@ -12,26 +12,32 @@ namespace BPASmartClient.KLMCoffee.Protocal
/// 系统状态
/// </summary>
public K95SysTemStatus temStatus { get; set; }

/// <summary>
/// 饮品类型
/// </summary>
public DrinkType drinkType { get; set; }

/// <summary>
/// 制作状态
/// </summary>
public MakeStatus makeStatus { get; set; }

/// <summary>
/// 当前任务
/// </summary>
public TaskIndex taskIndex { get; set; }

/// <summary>
/// 当前进度 10%
/// </summary>
public int progress { get; set; }

/// <summary>
/// 故障信息
/// </summary>
public FaultMessage faultMessage { get; set; }

/// <summary>
/// 保养信息
/// </summary>


+ 8
- 4
BPASmartClient.Model/单片机/SCChipEvent.cs View File

@@ -17,7 +17,10 @@ namespace BPASmartClient.Model.单片机
/// </summary>
public class SCChip_TakeCupEvent :BaseEvent
{

/// <summary>
/// 杯
/// </summary>
public byte Cup { get; set; }
}

/// <summary>
@@ -25,7 +28,7 @@ namespace BPASmartClient.Model.单片机
/// </summary>
public class SCChip_MakeIceCreamEvent :BaseEvent
{
public byte SteeringEngine { get; set; }
}

/// <summary>
@@ -33,7 +36,8 @@ namespace BPASmartClient.Model.单片机
/// </summary>
public class SCChip_SESwitchCreamEvent :BaseEvent
{

public byte SteeringEngine { get; set; }
public bool IsOpen { get; set; }
}

/// <summary>
@@ -41,6 +45,6 @@ namespace BPASmartClient.Model.单片机
/// </summary>
public class SCChip_RotorSwitchEvent :BaseEvent
{
public bool TurnOn { get; set; }
}
}

+ 2
- 1
BPASmartClient.Model/咖啡机/DRCoffeeEvent.cs View File

@@ -17,6 +17,7 @@ namespace BPASmartClient.Model
/// </summary>
public class DRCoffee_MakeCoffeeEvent :BaseEvent
{
public byte DrinkCode { get; set; }
}

/// <summary>
@@ -32,7 +33,7 @@ namespace BPASmartClient.Model
/// </summary>
public class DRCoffee_CoffeeCommCmdEvent :BaseEvent
{
public byte CommCmd { get; set; }
}

/// <summary>


+ 1
- 1
BPASmartClient.SCChip/BPASmartClient.SCChip.csproj View File

@@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.MessageCommunication\BPASmartClient.MessageCommunication.csproj" />
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
<ProjectReference Include="..\BPASmartClient.SerialPort\BPASmartClient.SerialPort.csproj" />
</ItemGroup>


+ 0
- 128
BPASmartClient.SCChip/ChipStatus.cs View File

@@ -1,128 +0,0 @@
using System;

namespace BPASmartClient.SCChip
{
public class ChipStatus
{
private volatile static ChipStatus _Instance;
public static ChipStatus GetInstance() => _Instance ?? (_Instance = new ChipStatus());
private ChipStatus() { }

private DateTime lastRefreshTime = DateTime.MinValue;
/// <summary>
/// 是否在线
/// </summary>
public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
/// <summary>
/// 取冰淇淋杯完成
/// </summary>
public bool CompletedTake_CPU_CUP_ICECREAM { get; set; }
/// <summary>
/// 取咖啡杯完成
/// </summary>
public bool CompletedTake_CPU_CUP_COFFEE { get; set; }
/// <summary>
/// 1号舵机打开完成
/// </summary>
public bool CompletedOpen_SE_1 { get; set; }
/// <summary>
/// 2号舵机打开完成
/// </summary>
public bool CompletedOpen_SE_2 { get; set; }
/// <summary>
/// 3号舵机打开完成
/// </summary>
public bool CompletedOpen_SE_3 { get; set; }
/// <summary>
/// 1号舵机关闭完成
/// </summary>
public bool CompletedClose_SE_1 { get; set; }
/// <summary>
/// 2号舵机关闭完成
/// </summary>
public bool CompletedClose_SE_2 { get; set; }
/// <summary>
/// 3号舵机关闭完成
/// </summary>
public bool CompletedClose_SE_3 { get; set; }
/// <summary>
/// 是否存在物品
/// </summary>
public bool ArticleExits { get; set; }
/// <summary>
/// 物品距离
/// </summary>
public byte ArticleDist { get; set; }

public bool CanDo
{
get
{
if (!OnLine)
return false;
return true;
}
}

public void ProcessMsg(ICChipPackage data)
{
try
{
switch (data.Cmd)
{
case IC_CMD.HEART_BEAT:
lastRefreshTime = DateTime.Now;
break;
case IC_CMD.TAKE_CUP:
switch ((IC_CUP)data.Value) {
case IC_CUP.CUP_COFFEE:
CompletedTake_CPU_CUP_COFFEE = true;
break;
case IC_CUP.CUP_ICECREAM:
CompletedTake_CPU_CUP_ICECREAM = true;
break;
}
break;
case IC_CMD.OPEN_SE:
switch ((IC_SE)data.Value)
{
case IC_SE.SE_1:
CompletedOpen_SE_1 = true;
break;
case IC_SE.SE_2:
CompletedOpen_SE_2 = true;
break;
case IC_SE.SE_3:
CompletedOpen_SE_3 = true;
break;
}
break;
case IC_CMD.CLOSE_SE:
switch ((IC_SE)data.Value)
{
case IC_SE.SE_1:
CompletedClose_SE_1 = true;
break;
case IC_SE.SE_2:
CompletedClose_SE_2 = true;
break;
case IC_SE.SE_3:
CompletedClose_SE_3 = true;
break;
}
break;
case IC_CMD.ARTICLE_EXITS:
ArticleExits = data.Value > 0;
break;
case IC_CMD.ARTICLE_DIST:
ArticleDist = data.Value;
break;
}
}
catch (Exception ex)
{

}
}
}
}

+ 0
- 42
BPASmartClient.SCChip/CommandEvent.cs View File

@@ -1,42 +0,0 @@
using BPASmartClient.SCChip;

namespace BPASmartClient.SCChip
{
public class TakeCupEvent
{
/// <summary>
/// 杯
/// </summary>
public IC_CUP Cup { get; set; }
}

public class MakeIceCreamEvent
{
public IC_SE SteeringEngine { get; set; }
}

public class SESwitchCreamEvent
{
public IC_SE SteeringEngine { get; set; }
public bool IsOpen { get; set; }
}

public class RotorSwitchEvent
{
public bool TurnOn { get; set; }
}
/// <summary>
/// 检测有无杯子
/// </summary>
public class ArticleExitsEvent
{
}
/// <summary>
/// 检测物品距离
/// </summary>
public class ArticleDistEvent
{

}
}

+ 0
- 225
BPASmartClient.SCChip/CommandHandler.cs View File

@@ -1,225 +0,0 @@
using BPASmartClient.Message;
using BPASmartClient.MessageCommunication;
using BPASmartClient.MessageCommunication.MsgControl;
using BPASmartClient.SerialPort;
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace BPASmartClient.SCChip
{
/// <summary>
/// 指令封装
/// </summary>
internal class CommandHandler
{
private SerialPortClient commProxy;
private ICChipPackage package = new ICChipPackage();

/// <summary>
/// 初始化
/// </summary>
internal void Init(SerialPortClient commProxy)
{
this.commProxy = commProxy;
Class_InnerMessageBus.GetInstance().ListenMessage(this,Class_MessageName.SCChip_TakeCup,"TakeCupHandler");
Class_InnerMessageBus.GetInstance().ListenMessage(this,Class_MessageName.SCChip_MakeIceCream,"MakeIceCreamHandler");
Class_InnerMessageBus.GetInstance().ListenMessage(this,Class_MessageName.SCChip_SESwitchCream,"SESwitchCreamHandler");
Class_InnerMessageBus.GetInstance().ListenMessage(this,Class_MessageName.SCChip_RotorSwitch,"RotorSwitchHandler");

}

/// <summary>
/// STM32F103RCT6单片机舵机打开或者关闭
/// </summary>
public void SESwitchCreamHandler(object sender,InnerMessageEventArgs e)
{
try
{
if (e.obj_MessageObj is TakeCupEvent)
{
ChipStatus.GetInstance().ArticleDist = 0;
package.Cmd = (e.obj_MessageObj as SESwitchCreamEvent).IsOpen ? IC_CMD.OPEN_SE : IC_CMD.CLOSE_SE;
package.Value = (byte)(e.obj_MessageObj as SESwitchCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,SESwitchCreamHandler 类,描述:[{ex.Message}]");
}
}

/// <summary>
/// STM32F103RCT6单片机控制冰淇淋机器转
/// </summary>
public void RotorSwitchHandler(object sender,InnerMessageEventArgs e)
{
try
{
if (e.obj_MessageObj is RotorSwitchEvent)
{
package.Cmd = IC_CMD.ROTOR;
package.Value = (e.obj_MessageObj as RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.CLOSE_ROTOR;
commProxy.SendData(StructureToByte(package));
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,RotorSwitchHandler 类,描述:[{ex.Message}]");
}
}

/// <summary>
/// STM32F103RCT6单片机舵机打料
/// </summary>
public void MakeIceCreamHandler(object sender,InnerMessageEventArgs e)
{
try
{
if (e.obj_MessageObj is MakeIceCreamEvent)
{
switch ((e.obj_MessageObj as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
ChipStatus.GetInstance().CompletedOpen_SE_1 = false;
break;
case IC_SE.SE_2:
ChipStatus.GetInstance().CompletedOpen_SE_2 = false;
break;
case IC_SE.SE_3:
ChipStatus.GetInstance().CompletedOpen_SE_3 = false;
break;
}
package.Cmd = IC_CMD.OPEN_SE;
package.Value = (byte)(e.obj_MessageObj as MakeIceCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));

bool wait = true;
DateTime waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((e.obj_MessageObj as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
wait = !ChipStatus.GetInstance().CompletedOpen_SE_1;
break;
case IC_SE.SE_2:
wait = !ChipStatus.GetInstance().CompletedOpen_SE_2;
break;
case IC_SE.SE_3:
wait = !ChipStatus.GetInstance().CompletedOpen_SE_3;
break;
}
}
Thread.Sleep(10);
}
Thread.Sleep(2000);
package.Cmd = IC_CMD.CLOSE_SE;
package.Value = (byte)(e.obj_MessageObj as MakeIceCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));

wait = true;
waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((e.obj_MessageObj as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
wait = !ChipStatus.GetInstance().CompletedClose_SE_1;
break;
case IC_SE.SE_2:
wait = !ChipStatus.GetInstance().CompletedClose_SE_2;
break;
case IC_SE.SE_3:
wait = !ChipStatus.GetInstance().CompletedClose_SE_3;
break;
}
}
Thread.Sleep(10);
}
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,MakeIceCreamHandler 类,描述:[{ex.Message}]");
}
}

/// <summary>
/// STM32F103RCT6单片机下杯
/// </summary>
public void TakeCupHandler(object sender,InnerMessageEventArgs e)
{
try
{
if (e.obj_MessageObj is TakeCupEvent)
{
switch ((e.obj_MessageObj as TakeCupEvent).Cup)
{
case IC_CUP.CUP_ICECREAM:
ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM = false;
break;
case IC_CUP.CUP_COFFEE:
ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE = false;
break;
}
package.Cmd = IC_CMD.TAKE_CUP;
package.Value = (byte)(e.obj_MessageObj as TakeCupEvent).Cup;
commProxy.SendData(StructureToByte(package));

bool wait = true;
var waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((e.obj_MessageObj as TakeCupEvent).Cup)
{
case IC_CUP.CUP_ICECREAM:
wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM;
break;
case IC_CUP.CUP_COFFEE:
wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE;
break;
}
}
Thread.Sleep(10);
}
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,TakeCupHandler 类,描述:[{ex.Message}]");
}
}

private byte[] StructureToByte(ICChipPackage structure)
{
structure.Header = 0xAA;
structure.Sender = IC_SENDER.CONSOLE;
structure.End = 0xBB;

int size = Marshal.SizeOf(typeof(ICChipPackage));
byte[] buffer = new byte[size];
IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structure, bufferIntPtr, true);
Marshal.Copy(bufferIntPtr, buffer, 0, size);
}
finally
{
Marshal.FreeHGlobal(bufferIntPtr);
}
return buffer;
}
}
}

+ 293
- 29
BPASmartClient.SCChip/ICChipMachine.cs View File

@@ -1,59 +1,85 @@
using BPASmartClient.Helper;
using BPASmartClient.EventBus;
using BPASmartClient.Helper;
using BPASmartClient.Message;
using BPASmartClient.Model;
using BPASmartClient.Model.单片机;
using BPASmartClient.Peripheral;
using BPASmartClient.SerialPort;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using static BPASmartClient.EventBus.EventBus;

namespace BPASmartClient.SCChip
{
public class ICChipMachine: BasePeripheral
public class ICChipMachine :BasePeripheral
{
//指令组装
private CommandHandler commandHandler = new CommandHandler();
//通讯代理
SerialPortClient commProxy = null;
//数据仓库
private DataStorage<byte> dataStorage = new DataStorage<byte>();
//主线程运行标识
private bool running = false;
//是否下发指令,主线程等待
public Action<string> SendCallback;
public Action<string> ReciveCallback;

/// <summary>
/// 串口COM口
/// </summary>
//单片机基础协议
private ICChipPackage package = new ICChipPackage();
//串口COM口
public string PortName { get; set; }
//串口波特率
public string BaudRate { get; set; }
//心跳时间
private DateTime lastRefreshTime = DateTime.MinValue;
/// <summary>
/// 串口波特率
/// 是否在线
/// </summary>
public string BaudRate { get; set; }
public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }

public ICChipMachine()
{
//commProxy = new SerialPortClient(portName, baud);
//commProxy.SetDataStorage(dataStorage);
//commandHandler.Init(commProxy);

}

/// <summary>
/// 主线程开始运行
/// </summary>
public override void Start()
{
commProxy.Start();
running = true;
MainLoop();
try
{
commProxy.Start();
IsConnected = true;
MainLoop();
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
}

}

/// <summary>
/// 停止运行
/// </summary>
public override void Stop()
{
try
{
commProxy.Stop();
IsConnected = false;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
}
}

/// <summary>
/// 主循环,循环询问状态
/// </summary>
private void MainLoop()
{
ThreadManage.GetInstance.StartLong(new Action(() =>
{
ResolveMsg();
//Thread.Sleep(2000);
}), "单片机解析线程");
}),"单片机解析线程");
}

private void ResolveMsg()
@@ -74,7 +100,7 @@ namespace BPASmartClient.SCChip
if (temp[4] == 0xBB)
{
var package = ByteToStructure(temp.ToArray());
ChipStatus.GetInstance().ProcessMsg(package);
ProcessMsg(package);
}
temp.Clear();
}
@@ -93,8 +119,8 @@ namespace BPASmartClient.SCChip
IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(dataBuffer, 0, allocIntPtr, size);
structure = (ICChipPackage)Marshal.PtrToStructure(allocIntPtr, typeof(ICChipPackage));
Marshal.Copy(dataBuffer,0,allocIntPtr,size);
structure = (ICChipPackage)Marshal.PtrToStructure(allocIntPtr,typeof(ICChipPackage));
}
finally
{
@@ -103,16 +129,254 @@ namespace BPASmartClient.SCChip
return structure;
}

protected override void InitStatus()
private byte[] StructureToByte(ICChipPackage structure)
{
structure.Header = 0xAA;
structure.Sender = IC_SENDER.CONSOLE;
structure.End = 0xBB;

int size = Marshal.SizeOf(typeof(ICChipPackage));
byte[] buffer = new byte[size];
IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structure,bufferIntPtr,true);
Marshal.Copy(bufferIntPtr,buffer,0,size);
}
finally
{
Marshal.FreeHGlobal(bufferIntPtr);
}
return buffer;
}

public override void Stop()
public void ProcessMsg(ICChipPackage data)
{
try
{
switch (data.Cmd)
{
case IC_CMD.HEART_BEAT:
lastRefreshTime = DateTime.Now;
status["OnLine"] = OnLine;
break;
case IC_CMD.TAKE_CUP:
switch ((IC_CUP)data.Value)
{
case IC_CUP.CUP_COFFEE:
status["CompletedTake_CPU_CUP_COFFEE"] = true;
break;
case IC_CUP.CUP_ICECREAM:
status["CompletedTake_CPU_CUP_ICECREAM"] = true;
break;
}
break;
case IC_CMD.OPEN_SE:
switch ((IC_SE)data.Value)
{
case IC_SE.SE_1:
status["CompletedOpen_SE_1"] = true;
break;
case IC_SE.SE_2:
status["CompletedOpen_SE_2"] = true;
break;
case IC_SE.SE_3:
status["CompletedOpen_SE_3"] = true;
break;
}
break;
case IC_CMD.CLOSE_SE:
switch ((IC_SE)data.Value)
{
case IC_SE.SE_1:
status["CompletedClose_SE_1"] = true;
break;
case IC_SE.SE_2:
status["CompletedClose_SE_2"] = true;
break;
case IC_SE.SE_3:
status["CompletedClose_SE_3"] = true;
break;
}
break;
}
if(!OnLine) IsWork = false;
else IsWork = true;
}
catch (Exception ex)
{

}
}

protected override void InitStatus()
{
status["CompletedTake_CPU_CUP_ICECREAM"] = false;
status["CompletedTake_CPU_CUP_COFFEE"] = false;
status["CompletedOpen_SE_1"] = false;
status["CompletedOpen_SE_2"] = false;
status["CompletedOpen_SE_3"] = false;
status["CompletedClose_SE_1"] = false;
status["CompletedClose_SE_2"] = false;
status["CompletedClose_SE_3"] = false;
}

public override void Init()
{
commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
commProxy.SetDataStorage(dataStorage);

//STM32F103RCT6单片机下杯
EventBus.EventBus.GetInstance().Subscribe<SCChip_TakeCupEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
switch ((IC_CUP)(@event as SCChip_TakeCupEvent).Cup)
{
case IC_CUP.CUP_ICECREAM:
status["CompletedTake_CPU_CUP_ICECREAM"] = false;
break;
case IC_CUP.CUP_COFFEE:
status["CompletedTake_CPU_CUP_COFFEE"] = false;
break;
}
package.Cmd = IC_CMD.TAKE_CUP;
package.Value = (byte)(@event as SCChip_TakeCupEvent).Cup;
commProxy.SendData(StructureToByte(package));

bool wait = true;
var waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((IC_CUP)(@event as SCChip_TakeCupEvent).Cup)
{
case IC_CUP.CUP_ICECREAM:
wait = !(bool)status["CompletedTake_CPU_CUP_ICECREAM"];
break;
case IC_CUP.CUP_COFFEE:
wait = !(bool)status["CompletedTake_CPU_CUP_COFFEE"];
break;
}
}
Thread.Sleep(10);
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
}
});

//STM32F103RCT6单片机舵机打料
EventBus.EventBus.GetInstance().Subscribe<SCChip_MakeIceCreamEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
switch ((IC_SE)(@event as SCChip_MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
status["CompletedOpen_SE_1"] = false;
break;
case IC_SE.SE_2:
status["CompletedOpen_SE_2"] = false;
break;
case IC_SE.SE_3:
status["CompletedOpen_SE_3"] = false;
break;
}
package.Cmd = IC_CMD.OPEN_SE;
package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));

bool wait = true;
DateTime waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((IC_SE)(@event as SCChip_MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
wait = !(bool)status["CompletedOpen_SE_1"];
break;
case IC_SE.SE_2:
wait = !(bool)status["CompletedOpen_SE_2"];
break;
case IC_SE.SE_3:
wait = !(bool)status["CompletedOpen_SE_3"];
break;
}
}
Thread.Sleep(10);
}
Thread.Sleep(2000);
package.Cmd = IC_CMD.CLOSE_SE;
package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));

wait = true;
waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((IC_SE)(@event as SCChip_MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
wait = !(bool)status["CompletedClose_SE_1"];
break;
case IC_SE.SE_2:
wait = !(bool)status["CompletedClose_SE_2"];
break;
case IC_SE.SE_3:
wait = !(bool)status["CompletedClose_SE_3"];
break;
}
}
Thread.Sleep(10);
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
}
});

//STM32F103RCT6单片机舵机打开或者关闭
EventBus.EventBus.GetInstance().Subscribe<SCChip_SESwitchCreamEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
package.Cmd = (@event as SCChip_SESwitchCreamEvent).IsOpen ? IC_CMD.OPEN_SE : IC_CMD.CLOSE_SE;
package.Value = (byte)(@event as SCChip_SESwitchCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeCommCmdHandler 类,描述:[{ex.Message}]");
}
});

//STM32F103RCT6单片机控制冰淇淋机器转
EventBus.EventBus.GetInstance().Subscribe<SCChip_RotorSwitchEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
package.Cmd = IC_CMD.ROTOR;
package.Value = (@event as SCChip_RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.CLOSE_ROTOR;
commProxy.SendData(StructureToByte(package));
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
}
});

}
}



+ 1
- 1
BPASmartClient/MainWindow.xaml View File

@@ -10,6 +10,6 @@
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="153,123,0,0" VerticalAlignment="Top" Click="Button_Click"/>

<pry:IcoButton IcoText="&#xe626;"/>
<!--<pry:IcoButton IcoText="&#xe626;"/>-->
</Grid>
</Window>

+ 3
- 0
BPASmartClient/MainWindow.xaml.cs View File

@@ -32,6 +32,9 @@ namespace BPASmartClient
private void Button_Click(object sender, RoutedEventArgs e)
{
new Demo_MakeCoffeeEvent() { Id = "1" }.Publish();
new DRCoffee_MakeCoffeeEvent() { Id = "1", DrinkCode=120 }.Publish();
new DRCoffee_CoffeeCommCmdEvent() { Id = "1",CommCmd = 0x01 }.Publish();

}
}
}

Loading…
Cancel
Save