|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using BPASmartClient.Helper;
- using BPASmartClient.SerialPort;
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using static BPASmartClient.GSIceCream.MessageDefine;
-
- namespace BPASmartClient.GSIceCream
- {
- public class IceCreamMachine
- {
- //指令组装
- private CommandHandler commandHandler = new CommandHandler();
- //通讯代理
- SerialPortClient commProxy = null;
- //数据仓库
- private DataStorage<byte> dataStorage = new DataStorage<byte>();
- //主线程运行标识
- private bool running = false;
- //是否下发指令,主线程等待
- private bool free = true;
- public Action<string> SendCallback;
- public Action<string> ReciveCallback;
-
- public IceCreamMachine(string portName, BaudRates baud)
- {
- commProxy = new SerialPortClient(portName, baud);
- commProxy.SetDataStorage(dataStorage);
- commandHandler.Init(commProxy);
- commandHandler.PauseAsk = delegate (bool pause)
- {
- free = !pause;
- };
- }
-
- public void Start()
- {
- commProxy.Start();
- running = true;
- MainLoop();
- }
-
- public void Stop()
- {
- }
-
- private MSG_RESOLVE_STEP currentStep;
- private void MainLoop()
- {
- ThreadManage.GetInstance.StartLong(new Action(() =>
- {
- if (free)
- {
- commProxy.SendData(commandHandler.GetHeartDW());
- SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetHeartDW()));
- }
- Thread.Sleep(500);
- }), "冰淇淋询问线程");
-
- ThreadManage.GetInstance.StartLong(new Action(() =>
- {
- ResolveMsg();
- //Thread.Sleep(2000);
- }), "冰淇淋解析线程");
- }
- int contentLength = 0;
- int currentContentOffset = 0;
- private void ResolveMsg()
- {
-
- //while (running)
- //{
- List<byte> temp = new List<byte>();
- //一系列解包
- while (dataStorage.GetSize() > 0)
- {
- byte item = dataStorage.GetData();
- switch (currentStep)
- {
- case MSG_RESOLVE_STEP.NONE:
- if (item == MessageDefine.HEADER1)
- {
- temp.Add(item);
- currentStep = MSG_RESOLVE_STEP.HEADER1;
- continue;
- }
- break;
- case MSG_RESOLVE_STEP.HEADER1:
- if (item == MessageDefine.HEADER2_UP)
- {
- temp.Add(item);
- currentStep = MSG_RESOLVE_STEP.HEADER2;
- continue;
- }
- else
- {
- temp.Clear();
- currentStep = MSG_RESOLVE_STEP.NONE;
- continue;
- }
- case MSG_RESOLVE_STEP.HEADER2:
- switch ((IC_CMD)item)
- {
- case IC_CMD.HEART:
- temp.Add(item);
- contentLength = MessageDefine.MSG_LENGTH[(IC_CMD)item];
- currentContentOffset = 0;
- currentStep = MSG_RESOLVE_STEP.CMD;
- break;
- default:
- temp.Clear();
- currentStep = MSG_RESOLVE_STEP.NONE;
- break;
- }
- break;
- }
- int retry = 3;
- while (dataStorage.GetSize() < contentLength + 2 && retry >= 0)
- {
- retry--;
- Thread.Sleep(100);
- }
- if (retry < 0)
- {
- currentStep = MSG_RESOLVE_STEP.NONE;
- currentContentOffset = 0;
- contentLength = 0;
- continue;
- }
- while (currentContentOffset < contentLength)
- {
- item = dataStorage.GetData();
- temp.Add(item);
- currentContentOffset++;
- }
-
- retry = 3;
- while (dataStorage.GetSize() < 2 && retry >= 0)
- {
- retry--;
- Thread.Sleep(100);
- }
- temp.Add(dataStorage.GetData());
- temp.Add(dataStorage.GetData());
- ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
- MorkIStatus.GetInstance().ProcessMsg(temp.ToArray());
- currentStep = MSG_RESOLVE_STEP.NONE;
- continue;
- }
- Thread.Sleep(5);
- //}
- }
- }
-
- }
|