|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- using BPASmartClient.EventBus;
- using BPASmartClient.Helper;
- using BPASmartClient.Message;
- using BPASmartClient.Model;
- using BPASmartClient.Model.单片机;
- using BPASmartClient.Model.单片机.Enum;
- 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
- {
- //通讯代理
- SerialPortClient commProxy = null;
- //数据仓库
- private DataStorage<byte> dataStorage = new DataStorage<byte>();
- //单片机基础协议
- private ICChipPackage package = new ICChipPackage();
- //串口COM口
- public string PortName { get; set; }
- //串口波特率
- public string BaudRate { get; set; }
- //心跳时间
- private DateTime lastRefreshTime = DateTime.MinValue;
- /// <summary>
- /// 是否在线
- /// </summary>
- public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
-
- public ICChipMachine()
- {
-
- }
-
- /// <summary>
- /// 主线程开始运行
- /// </summary>
- public override void Start()
- {
- try
- {
- commProxy.Start();
- IsConnected = true;
- MainLoop();
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
- }
-
- }
-
- /// <summary>
- /// 停止运行
- /// </summary>
- public override void Stop()
- {
- try
- {
- commProxy.Stop();
- IsConnected = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
- }
- }
-
- /// <summary>
- /// 主循环,循环询问状态
- /// </summary>
- private void MainLoop()
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- ResolveMsg();
- }),"单片机解析线程");
- }
-
- private void ResolveMsg()
- {
- List<byte> temp = new List<byte>();
- //一系列解包
- while (dataStorage.GetSize() > 0)
- {
- byte item = dataStorage.GetData();
- if (item == 0xAA)
- {
- temp.Add(item);
- while (dataStorage.GetSize() < 4) { Thread.Sleep(5); }
- while (temp.Count < 5)
- {
- temp.Add(dataStorage.GetData());
- }
- if (temp[4] == 0xBB)
- {
- var package = ByteToStructure(temp.ToArray());
- ProcessMsg(package);
- }
- temp.Clear();
- }
- continue;
- }
- Thread.Sleep(5);
- }
-
- /// <summary>
- /// 由byte数组转换为结构体
- /// </summary>
- private ICChipPackage ByteToStructure(byte[] dataBuffer)
- {
- ICChipPackage structure = new ICChipPackage();
- int size = Marshal.SizeOf(typeof(ICChipPackage));
- IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
- try
- {
- Marshal.Copy(dataBuffer,0,allocIntPtr,size);
- structure = (ICChipPackage)Marshal.PtrToStructure(allocIntPtr,typeof(ICChipPackage));
- }
- finally
- {
- Marshal.FreeHGlobal(allocIntPtr);
- }
- return structure;
- }
-
- 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 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 ((@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 ((@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.ShowEx($"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.ShowEx($"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.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{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.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
- }
- });
- InitStatus();
- }
- }
-
- }
|