Browse Source

单片机协议

master
pry 2 years ago
parent
commit
1d6a0dd6a7
6 changed files with 127 additions and 67 deletions
  1. +1
    -0
      HBLConsole.Business/AbstractServer/Base.cs
  2. +21
    -47
      HBLConsole.Communication/MCUSerialHelper.cs
  3. +2
    -2
      HBLConsole.Communication/Serial/SerialPortClient.cs
  4. +77
    -17
      HBLConsole.MORKM/Control_MORKM.cs
  5. +18
    -1
      HBLConsole.MORKM/GVL_MORKM.cs
  6. +8
    -0
      HBLConsole.MainConsole/Main.cs

+ 1
- 0
HBLConsole.Business/AbstractServer/Base.cs View File

@@ -134,6 +134,7 @@ namespace HBLConsole.Business.AbstractServer
/// <returns></returns>
public override bool OrderStatusChange(string subOrderId, ORDER_STATUS status)
{
return false;
string result = string.Empty;
OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = status, SuborderId = subOrderId };
try


+ 21
- 47
HBLConsole.Communication/MCUSerialHelper.cs View File

@@ -19,11 +19,6 @@ namespace HBLConsole.Communication
private MCUSerialHelper() { }

private SerialPort comPort = new SerialPort();
ConcurrentQueue<byte[]> SerialMessages = new ConcurrentQueue<byte[]>();
//帧尾
private byte end = 0xDD;
//帧头
private byte start = 0xCC;
public bool IsOpen => comPort.IsOpen;

public bool Open(string portName, int baudRate)
@@ -41,8 +36,9 @@ namespace HBLConsole.Communication
comPort.StopBits = StopBits.One;
comPort.ReadTimeout = 1000;
comPort.WriteTimeout = 1000;
comPort.DataReceived += ComPort_DataReceived;
comPort.RtsEnable = true;
//comPort.RtsEnable = true; //设置为 true后会读取不到数据
//comPort.DtrEnable = true;//获取或设置一个值,该值在串行通信过程中启用数据终端就绪 (DTR) 信号。
//comPort.RtsEnable = true;//获取或设置一个值,该值指示在串行通信中是否启用请求发送 (RTS) 信号
try
{
comPort.Open();
@@ -57,28 +53,6 @@ namespace HBLConsole.Communication
return comPort.IsOpen;
}

private void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int len = comPort.BytesToRead;
if (len <= 0) return;
byte[] receive = new byte[len];
comPort.Read(receive, 0, receive.Length);
if (receive[len - 1] != end) return;
List<byte> ReceiveList = new List<byte>();
for (int i = 0; i < receive.Length; i++)
{
ReceiveList.Add(receive[i]);
if (ReceiveList.Count > 0)
{
if (ReceiveList.ElementAt(0) == start && ReceiveList.ElementAt(ReceiveList.Count - 1) == end)
{
SerialMessages.Enqueue(ReceiveList.ToArray());
ReceiveList.Clear();
}
}
}
comPort.DiscardInBuffer();
}

/// <summary>
/// 单片机输出端口控制
@@ -87,13 +61,9 @@ namespace HBLConsole.Communication
/// <param name="value">控制值</param>
public void OutputControl(byte index, bool value)
{
byte NumValue = (byte)(value ? 1 : 0);
byte NumValue = (byte)(value ? 0x01 : 0x00);
byte[] buffers = new byte[6] { 0xCC, 0x01, 0x01, index, NumValue, 0xDD };
if (comPort.IsOpen)
{
comPort.Write(buffers, 0, buffers.Length);
}

if (IsOpen) comPort.Write(buffers, 0, buffers.Length);
}

/// <summary>
@@ -104,10 +74,7 @@ namespace HBLConsole.Communication
public void ServoControl(byte index, byte value)
{
byte[] buffers = new byte[6] { 0xCC, 0x01, 0x02, index, value, 0xDD };
if (comPort.IsOpen)
{
comPort.Write(buffers, 0, buffers.Length);
}
if (IsOpen) comPort.Write(buffers, 0, buffers.Length);
}

/// <summary>
@@ -119,21 +86,28 @@ namespace HBLConsole.Communication
{
if (index <= 0 || index > 8) return false;
byte[] buffers = new byte[6] { 0xCC, 0x01, 0x03, index, 0x00, 0xDD };
if (comPort.IsOpen)
if (IsOpen)
{
comPort.Write(buffers, 0, buffers.Length);
while (SerialMessages.Count <= 0)
DateTime dt = DateTime.Now;
List<byte> receive = new List<byte>();
while (true)
{
byte[] re = new byte[comPort.BytesToRead];
comPort.Read(re, 0, re.Length);
if (re.Contains<byte>(0xcc) && re.Contains<byte>(0xDD)) receive.AddRange(re);
comPort.DiscardInBuffer();
if (receive.Contains(0xcc) && receive.Contains(0xdd)) break;
if (DateTime.Now.Subtract(dt).TotalSeconds >= 2) break;
Thread.Sleep(1);
}
if (SerialMessages.TryDequeue(out byte[] receive))
if (receive != null)
{
if (receive.Length == 6)
int Reindex = Array.FindIndex(receive.ToArray(), p => p == 0xcc);
if (Reindex < receive.Count && Reindex >= 0)
{
if (receive[2] == 0x03)
{
return receive[4] == 0x01;
}
var res = receive.GetRange(Array.FindIndex(receive.ToArray(), p => p == 0xcc), 6);
return res != null && res.Count() == 6 && res.ElementAt(4) == 0x01;
}
}
}


+ 2
- 2
HBLConsole.Communication/Serial/SerialPortClient.cs View File

@@ -202,7 +202,7 @@ namespace HBLConsole.Communication
}
}

/// <summary>
/// 读取数据
/// </summary>
@@ -215,7 +215,7 @@ namespace HBLConsole.Communication
if (!(comPort.IsOpen)) comPort.Open();
byte[] buffer = new byte[1024];
comPort.Read(buffer, 0, buffer.Length);
return buffer;
return buffer;
}
}
public void Start()


+ 77
- 17
HBLConsole.MORKM/Control_MORKM.cs View File

@@ -474,7 +474,9 @@ namespace HBLConsole.MORKM
{
if (mORKS.RBTakeNoodleTask.TryDequeue(out OrderLocInfo orderLocInfo))
{
mORKS.CookNodelId[loc] = orderLocInfo.SuborderId;
mORKS.CookNodelId.ElementAt(loc).CookNodelId = orderLocInfo.SuborderId;
mORKS.CookNodelId.ElementAt(loc).Loc = mORKS.TurntableFeedbackloc;

SetFallNoodleLoc((ushort)(loc + 1));
//机器人开始取面
RobotTakeNoodle();
@@ -500,28 +502,86 @@ namespace HBLConsole.MORKM
{
if (mORKS.AllowFallNoodle && mORKS.RobotTaskInterlock && !mORKS.TakeNoodleInterlock && mORKS.RobotIdle && !mORKS.TakeMealDetect)
{
int loc = Array.FindIndex(mORKS.CookNodelId, p => p == mORKS.IngredientsCompleteId && p.Length > 0);
if (loc >= 0 && loc <= 5)
var CookNodelIdList = mORKS.CookNodelId.Where(p => p.CookNodelId == mORKS.IngredientsCompleteId && p.CookNodelId.Length > 0).ToList();
if (CookNodelIdList != null)
{
if (mORKS.CookNoodlesComplete[loc])
var res = mORKS.RBTakeNoodleTask.FirstOrDefault(p => p.SuborderId == mORKS.IngredientsCompleteId);
if (res == null)//排队订单中不存在该订单
{
SetTakeNoodleLoc((ushort)(loc + 1));
RobotOutMeal();
CookNoodleStatusReset((ushort)(loc + 1));
ResetAllowFallNoodle();
mORKS.OutMealId = mORKS.IngredientsCompleteId;
mORKS.IngredientsCompleteId = string.Empty;
mORKS.CookNodelId[loc] = string.Empty;

if (mORKS.CookNodelId.GetIndex(mORKS.OutMealId) < 0)
var isHaveEven = CookNodelIdList.FirstOrDefault(p => p.Loc > 0 && p.Loc % 2 == 0);//偶数,判断是否有素菜
var isHaveOddNumber = CookNodelIdList.FirstOrDefault(p => p.Loc % 2 != 0);//奇数,判断是否有荤菜
if (isHaveEven != null)
{
//素菜
OutMealControl(Array.FindIndex(mORKS.CookNodelId.ToArray(), p => p.CookNodelId == isHaveEven.CookNodelId && p.CookNodelId.Length > 0));
}
else
{
CookComplete(); //告诉机器人冒菜已经煮完
//IsAddSoup();//判断是否加汤
if (isHaveOddNumber != null)
//荤菜
OutMealControl(Array.FindIndex(mORKS.CookNodelId.ToArray(), p => p.CookNodelId == isHaveOddNumber.CookNodelId && p.CookNodelId.Length > 0));
}

MessageLog.GetInstance.Show($"{loc + 1}号位置出餐控制");
mORKS.OutNoodleing = true;
}
else//排队订单中有该订单
{

}
}

//int loc = Array.FindIndex(mORKS.CookNodelId.ToArray(), p => p.CookNodelId == mORKS.IngredientsCompleteId && p.CookNodelId.Length > 0);

//if (loc >= 0 && loc <= 5)
//{
// if (mORKS.CookNoodlesComplete[loc])
// {
// SetTakeNoodleLoc((ushort)(loc + 1));
// RobotOutMeal();
// CookNoodleStatusReset((ushort)(loc + 1));
// mORKS.OutMealId = mORKS.IngredientsCompleteId;
// mORKS.IngredientsCompleteId = string.Empty;
// mORKS.CookNodelId.ElementAt(loc).CookNodelId = string.Empty;
// mORKS.CookNodelId.ElementAt(loc).Loc = -1;

// if (mORKS.RBTakeNoodleTask.FirstOrDefault(p => p.SuborderId == mORKS.OutMealId) == null && mORKS.CookNodelId.FirstOrDefault(p => p.CookNodelId == mORKS.OutMealId) == null)
// {
// CookComplete(); //告诉机器人冒菜已经煮完
// //IsAddSoup();//判断是否加汤
// ResetAllowFallNoodle();
// MessageLog.GetInstance.Show($"{mORKS.OutMealId} 出餐完成");
// }

// MessageLog.GetInstance.Show($"{loc + 1}号位置出餐控制");
// mORKS.OutNoodleing = true;
// }
//}
}
}

private void OutMealControl(int loc)
{
if (loc >= 0 && loc <= 5)
{
if (mORKS.CookNoodlesComplete[loc])
{
SetTakeNoodleLoc((ushort)(loc + 1));
RobotOutMeal();
CookNoodleStatusReset((ushort)(loc + 1));
mORKS.CookNodelId.ElementAt(loc).CookNodelId = string.Empty;
mORKS.CookNodelId.ElementAt(loc).Loc = -1;

if (mORKS.RBTakeNoodleTask.FirstOrDefault(p => p.SuborderId == mORKS.IngredientsCompleteId) == null && mORKS.CookNodelId.FirstOrDefault(p => p.CookNodelId == mORKS.IngredientsCompleteId) == null)
{
CookComplete(); //告诉机器人冒菜已经煮完
//IsAddSoup();//判断是否加汤
ResetAllowFallNoodle();
mORKS.OutMealId = mORKS.IngredientsCompleteId;
mORKS.IngredientsCompleteId = string.Empty;
MessageLog.GetInstance.Show($"{mORKS.OutMealId} 出餐完成");
}

MessageLog.GetInstance.Show($"{loc + 1}号位置出餐控制");
mORKS.OutNoodleing = true;
}
}
}


+ 18
- 1
HBLConsole.MORKM/GVL_MORKM.cs View File

@@ -16,6 +16,14 @@ namespace HBLConsole.MORKM
/// </summary>
public class GVL_MORKM : IGvl
{
public GVL_MORKM()
{
for (int i = 0; i < 6; i++)
{
CookNodelId.Add(new CookNodleLocAndId() { CookNodelId = "", Loc = -1 });
}
}

/// <summary>
/// 机器人取面
/// PLC -> M0.3
@@ -330,7 +338,9 @@ namespace HBLConsole.MORKM
/// <summary>
/// 煮面口对应的订单ID
/// </summary>
public string[] CookNodelId = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, };
//public string[] CookNodelId = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, };
public List<CookNodleLocAndId> CookNodelId { get; set; } = new List<CookNodleLocAndId>();


/// <summary>
/// 出餐订单ID
@@ -350,4 +360,11 @@ namespace HBLConsole.MORKM

#endregion
}

public class CookNodleLocAndId
{
public string CookNodelId { get; set; }

public int Loc { get; set; }
}
}

+ 8
- 0
HBLConsole.MainConsole/Main.cs View File

@@ -12,6 +12,7 @@ using HBLConsole.Business;
using HBLConsole.Factory;
using BPA.Message.Enum;
using System.Collections.ObjectModel;
using System.Threading;

namespace HBLConsole.MainConsole
{
@@ -67,6 +68,13 @@ namespace HBLConsole.MainConsole

public void BusinessInit()
{
//MCUSerialHelper.GetInstance.Open("COM7", 9600);
//MCUSerialHelper.GetInstance.OutputControl(1, true);
//while (true)
//{
// Thread.Sleep(1000);
//}
//var res = MCUSerialHelper.GetInstance.GetInputStatus(1);
ThreadManage.GetInstance.Start(new Action(() =>
{
InternetInfo.ConfigInit();//从 consul 获取配置数据


Loading…
Cancel
Save