C#駿鵬自動售貨機接口,
MachineJP類:
第1部分:串口初始化,串口數據讀寫

![]()
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MachineJPDll.Models;
using MachineJPDll.Utils;
namespace MachineJPDll
{
/// <summary>
/// 售貨機接口(駿鵬接口)
/// </summary>
public partial class MachineJP
{
#region 變量
/// <summary>
/// 串口資源
/// </summary>
private SerialPort m_SerialPort = null;
/// <summary>
/// 待發送給串口的命令列表
/// </summary>
private List<Cmd> m_CommandList = new List<Cmd>();
/// <summary>
/// 等待ACK_RPT或NAK_RPT的PC端向VMC端發送的消息列表
/// </summary>
private List<MT> m_WaitResultMTList = new List<MT>();
/// <summary>
/// 從串口接收的數據集合(數據已通過驗證)
/// </summary>
private ReceiveDataCollection m_ReceiveDataCollection = new ReceiveDataCollection();
#endregion
#region 構造函數與析構函數
/// <summary>
/// 售貨機接口(駿鵬接口)
/// </summary>
public MachineJP()
{
}
~MachineJP()
{
if (m_SerialPort != null)
{
m_SerialPort.Close();
m_SerialPort.Dispose();
m_SerialPort = null;
}
}
#endregion
#region 讀取串口數據
/// <summary>
/// 讀取串口數據
/// </summary>
/// <returns>從串口讀取的數據</returns>
private byte[] ReadPort()
{
//讀取串口數據
DateTime dt = DateTime.Now;
while (m_SerialPort.BytesToRead < 2)
{
Thread.Sleep(1);
if (DateTime.Now.Subtract(dt).TotalMilliseconds > 1500) //超時
{
return new byte[0];
}
}
List<byte> recList = new List<byte>();
byte[] recData = new byte[m_SerialPort.BytesToRead];
m_SerialPort.Read(recData, 0, recData.Length);
recList.AddRange(recData);
int length = recData[1] + 2; //報文數據總長度
while (recList.Count < length)
{
if (m_SerialPort.BytesToRead > 0)
{
recData = new byte[m_SerialPort.BytesToRead];
m_SerialPort.Read(recData, 0, recData.Length);
recList.AddRange(recData);
}
Thread.Sleep(1);
}
return recList.ToArray();
}
#endregion
#region 向串口發送數據
/// <summary>
/// 向串口發送數據
/// </summary>
/// <param name="cmd">待發送的命令</param>
/// <param name="SN">序列號</param>
private void WritePort(Cmd cmd, byte SN)
{
//發送數據
List<byte> sendData = cmd.Data;
sendData[1] = (byte)sendData.Count;
sendData[2] = SN;
byte[] checkCode = CommonUtil.CalCheckCode(sendData, sendData.Count);
sendData.AddRange(checkCode);
if (cmd.Mt != null)
{
m_WaitResultMTList.Add(cmd.Mt);
}
m_SerialPort.Write(sendData.ToArray(), 0, sendData.Count);
LogHelper.Log(LogMsgType.Info, true, sendData.ToArray());
}
#endregion
#region 發送ACK消息
/// <summary>
/// 發送ACK消息
/// </summary>
/// <param name="SN">序列號</param>
private void SendACK(byte SN)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x40, 0x80 };
WritePort(new Cmd(sendData), SN);
}
#endregion
#region Init 初始化
/// <summary>
/// 初始化
/// </summary>
/// <param name="com">串口號(例:COM1)</param>
public void Init(string com)
{
if (m_SerialPort == null)
{
m_SerialPort = new SerialPort(com, 9600, Parity.None, 8, StopBits.One);
m_SerialPort.ReadBufferSize = 1024;
m_SerialPort.WriteBufferSize = 1024;
m_SerialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
}
if (!m_SerialPort.IsOpen)
{
m_SerialPort.Open();
}
GET_SETUP();
CONTROL_IND(0x13, new byte[] { 0x00 }); //初始化完成標志
GET_STATUS();
SetDecimalPlaces(2); //設置小數點位數
}
#endregion
#region Close 關閉連接
/// <summary>
/// 關閉連接
/// </summary>
public void Close()
{
m_SerialPort.Close();
}
#endregion
#region 接收串口數據
/// <summary>
/// 接收串口數據
/// </summary>
/// <param name="type">消息類型</param>
/// <param name="subtype">消息子類型</param>
public byte[] Receive(byte type, byte subtype)
{
return m_ReceiveDataCollection.Get(type, subtype);
}
/// <summary>
/// 接收串口數據
/// </summary>
/// <param name="type">消息類型</param>
/// <param name="subtype">消息子類型</param>
public byte[] WaitReceive(byte type, byte subtype)
{
DateTime time = DateTime.Now;
while (true)
{
byte[] receiveData = m_ReceiveDataCollection.Get(type, subtype);
if (receiveData != null) return receiveData;
if (DateTime.Now.Subtract(time).TotalMinutes > 3) return null;
Thread.Sleep(50);
}
}
/// <summary>
/// 接收串口數據
/// </summary>
/// <param name="type">消息類型</param>
public byte[] WaitReceive(byte type)
{
DateTime time = DateTime.Now;
while (true)
{
byte[] receiveData = m_ReceiveDataCollection.Get(type);
if (receiveData != null) return receiveData;
if (DateTime.Now.Subtract(time).TotalMinutes > 3) return null;
Thread.Sleep(50);
}
}
#endregion
#region 判斷消息是否發送成功
/// <summary>
/// 判斷消息是否發送成功
/// </summary>
public bool SendSuccess(byte type, byte subtype)
{
DateTime time = DateTime.Now;
while (true)
{
if (DateTime.Now.Subtract(time).TotalMinutes > 3)
{
return false;
}
byte[] ack = m_ReceiveDataCollection.Get(type, subtype);
byte[] nak = m_ReceiveDataCollection.Get(type, subtype);
if (ack != null) return true;
if (nak != null) return false;
Thread.Sleep(1);
}
}
#endregion
}
}
View Code
第2部分:接收串口數據,並響應貨機,向貨機發送數據

![]()
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using MachineJPDll.Models;
using MachineJPDll.Utils;
/*
* VMC->PC數據的接收,貨機事件的分發
*/
namespace MachineJPDll
{
partial class MachineJP
{
#region serialPort_DataReceived
/// <summary>
/// 數據接收事件的方法
/// </summary>
public void serialPort_DataReceived(object obj, SerialDataReceivedEventArgs args)
{
byte[] receiveData = ReadPort();
if (CommonUtil.ValidReceiveData(receiveData)) //只處理驗證正確的數據,不正確的數據拋棄不處理
{
LogHelper.Log(LogMsgType.Info, false, receiveData);
byte SN = CommonUtil.GetSN(receiveData);
MT mt = new MT(receiveData);
#region 輪詢(POLL)
if (mt.Type == 0x03)
{
if (m_CommandList.Count > 0)
{
WritePort(m_CommandList[0], SN);
m_CommandList.RemoveAt(0);
}
else
{
//發送ACK消息
SendACK(SN);
}
}
#endregion
#region 發送ACK消息
if (CommonUtil.NeedACK(receiveData))
{
SendACK(SN); //發送ACK消息
}
#endregion
#region VMC系統參數
if (mt.Type == 0x05)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region ACK_RPT或NAK_RPT
if (mt.Type == 0x01 //ACK_RPT
|| mt.Type == 0x02) //NAK_RPT
{
if (m_WaitResultMTList.Count > 0)
{
m_ReceiveDataCollection.Add(m_WaitResultMTList[0].Type, m_WaitResultMTList[0].Subtype, receiveData);
m_WaitResultMTList.RemoveAt(0);
}
}
#endregion
#region INFO_RPT 數據報告
if (mt.Type == 0x11)
{
#region 紙幣器信息
if (mt.Subtype == 16)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region 硬幣器信息
if (mt.Subtype == 17)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region 用戶投幣余額
if (mt.Subtype == 3)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
}
#endregion
#region VENDOUT_RPT 出貨報告
if (mt.Type == 0x08)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region STATUS_RPT VMC整機狀態報告
if (mt.Type == 0x0D)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region SALEPRICE_IND 設置當前商品售價
if (mt.Type == 0x8E)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region PAYIN_RPT VMC發送現金投幣報告給PC
if (mt.Type == 0x06)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region PAYOUT_RPT 出幣報告
if (mt.Type == 0x07)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region COST_RPT VMC扣款報告
if (mt.Type == 0x10)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region ACTION_RPT 售貨機行為報告
if (mt.Type == 0x0B)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region HUODAO_RPT VMC貨道報告
if (mt.Type == 0x0E)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
}
else //接收到的數據沒有驗證通過
{
LogHelper.LogException(LogMsgType.Error, false, "數據異常", receiveData);
}
}
#endregion
}
}
View Code
第3部分:貨機狀態、投幣、出貨等接口

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using MachineJPDll.Enums;
using MachineJPDll.Models;
using MachineJPDll.Utils;
/*
* PC->VMC數據的發送(並非直接發送,只是添加到發送列表)
*/
namespace MachineJPDll
{
partial class MachineJP
{
#region GET_SETUP
/// <summary>
/// PC通知VMC發送VMC_SETUP
/// </summary>
public VmcSetup GET_SETUP()
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x40, 0x90 };
m_CommandList.Add(new Cmd(sendData));
byte[] receiveData = WaitReceive(0x05);
if (receiveData != null)
{
return new VmcSetup(receiveData);
}
return null;
}
#endregion
#region CONTROL_IND PC控制售貨機完成對應的動作
/// <summary>
/// PC控制VMC
/// </summary>
public bool CONTROL_IND(byte subtype, byte[] value)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x41, 0x85 };
sendData.Add(subtype);
if (value != null && value.Length > 0)
{
sendData.AddRange(value);
}
m_CommandList.Add(new Cmd(sendData, new MT(sendData)));
return SendSuccess(0x85, subtype);
}
#endregion
#region 設置小數點位數
/// <summary>
/// 設置小數點位數
/// 用於PC 通知VMC,雙方的金額數據比例系數關系,PC 每次啟動時,都會給
/// VMC 下發一次type=18 的消息,VMC 需要自己永久保存該數據,直到被PC 再
/// 次更新。
/// 取值范圍:0、1、2 分別表示以 元、 角 、分 為單位
/// </summary>
public bool SetDecimalPlaces(int data)
{
return CONTROL_IND(18, new byte[] { (byte)data });
}
#endregion
#region GET_STATUS PC通知VMC發送STATUS_RPT
/// <summary>
/// PC通知VMC發送STATUS_RPT
/// </summary>
public StatusRpt GET_STATUS()
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x40, 0x86 };
m_CommandList.Add(new Cmd(sendData));
byte[] receiveData = WaitReceive(0x0D);
if (receiveData != null)
{
return new StatusRpt(receiveData);
}
return null;
}
#endregion
#region GET_INFO PC通知VMC發送INFO_RPT
/// <summary>
/// PC通知VMC發送INFO_RPT
/// </summary>
public byte[] GET_INFO(byte subtype)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x40, 0x8C };
sendData.Add(subtype);
m_CommandList.Add(new Cmd(sendData));
return WaitReceive(0x11);
}
#endregion
#region VENDOUT_IND 出貨
/// <summary>
/// PC出貨指示
/// </summary>
/// <param name="device">售貨機的箱體號 例如櫃1 為 0x01 以此類推</param>
/// <param name="method">method =1:VMC 通過商品ID 指示出貨,如果商品ID 不存在,回復NAK_RPT method =2:VMC 通過貨道ID 指示VMC 出貨,如果貨道ID 不存在,回復NAK_RPT</param>
/// <param name="sp_id_hd_id">sp_id:通過商品ID 指示VMC 出貨 hd_id:通過貨道ID 指示VMC 出貨</param>
/// <param name="type">如果type=0,cost 代表本次出貨扣款金額 如果TYPE 不為0,則COST 必須為0</param>
/// <param name="cost">cost 代表本次出貨扣款金額</param>
public VendoutRpt VENDOUT_IND(byte device, byte method, byte sp_id_hd_id, byte type, int cost)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x41, 0x83 };
sendData.AddRange(new byte[] { device, method, sp_id_hd_id, type });
sendData.AddRange(CommonUtil.Int2ByteArray(cost, 2));
m_CommandList.Add(new Cmd(sendData, new MT(sendData)));
if (SendSuccess(0x83, 0x00))
{
byte[] receiveData = WaitReceive(0x08);
if (receiveData != null)
{
return new VendoutRpt(receiveData);
}
}
return null;
}
#endregion
#region HUODAO_SET_IND 設置貨道商品數量
/// <summary>
/// PC通知VMC,當前貨道對應商品的數量等信息
/// </summary>
/// <param name="device">表示箱櫃號</param>
/// <param name="huodao">zyxxxxxx “z”固定填0 “y”固定填0 “xxxxxx”,表示商品余量,如果商品余量大於63,則統一為63</param>
public bool HUODAO_SET_IND(byte device, List<int> huodao)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x41, 0x8F };
sendData.Add(device);
for (int i = 0; i < huodao.Count; i++)
{
if (huodao[i] > 63)
{
huodao[i] = 63;
}
}
sendData.AddRange(huodao.ConvertAll<byte>(a => (byte)a));
m_CommandList.Add(new Cmd(sendData, new MT(sendData)));
return SendSuccess(0x8F, 0x00);
}
#endregion
#region SALEPRICE_IND 設置當前商品售價
/// <summary>
/// PC通知VMC,當前商品售價
/// </summary>
/// <param name="device">表示箱櫃號</param>
/// <param name="type">表示設置單價的方式;Type = 0:為按商品ID 發送單價,可以變長發送,商品種類最大不超過80 個;Type = 1: 為按貨道號發送,固定發送80 個貨道的單價信息</param>
/// <param name="sp_price">商品售價</param>
public bool SALEPRICE_IND(byte device, byte type, List<int> sp_price)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x41, 0x8E };
sendData.Add(device);
sendData.Add(type);
sendData.AddRange(sp_price.ConvertAll<byte>(a => (byte)a));
m_CommandList.Add(new Cmd(sendData, new MT(sendData)));
return SendSuccess(0x8E, 0x00);
}
#endregion
#region PAYOUT_IND PC指示VMC出幣
/// <summary>
/// PC指示VMC出幣
/// </summary>
/// <param name="device">出幣設備</param>
/// <param name="value">本次出幣總金額</param>
/// <param name="type">出幣類型 無需理解type 的含義,只需要在出幣完成後的PAYOUT_RPT 中將該type 值回傳給PC 即可</param>
public PayoutRpt PAYOUT_IND(PayoutType device, int value, byte type)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x41, 0x89 };
sendData.Add((byte)device);
sendData.AddRange(CommonUtil.Int2ByteArray(value, 2));
sendData.Add(type);
m_CommandList.Add(new Cmd(sendData, new MT(sendData)));
if (SendSuccess(0x89, 0x00))
{
byte[] receiveData = WaitReceive(0x07);
if (receiveData != null)
{
return new PayoutRpt(receiveData);
}
}
return null;
}
#endregion
#region COST_IND PC扣款指示
/// <summary>
/// PC扣款指示
/// </summary>
/// <param name="device">device=0,從用戶投幣總額中扣款;優先從用戶非暫存金額中扣除(紙幣盡量滯後壓鈔),參見《現金扣款順序》</param>
/// <param name="value">扣款金額</param>
/// <param name="type">VMC 不用理解type 的含義,只需上報對應的COST_RPT 時回傳即可</param>
public CostRpt COST_IND(byte device, int value, byte type)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x41, 0x8B };
sendData.Add(device);
sendData.AddRange(CommonUtil.Int2ByteArray(value, 2));
sendData.Add(type);
m_CommandList.Add(new Cmd(sendData, new MT(sendData)));
if (SendSuccess(0x8B, 0x00))
{
byte[] receiveData = WaitReceive(0x10);
if (receiveData != null)
{
return new CostRpt(receiveData);
}
}
return null;
}
#endregion
#region GET_HUODAO PC通知VMC上報HUODAO_RPT
/// <summary>
/// PC通知VMC上報HUODAO_RPT
/// </summary>
/// <param name="device">箱櫃號</param>
public HuoDaoRpt GET_HUODAO(byte device)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x40, 0x8A };
sendData.Add(device);
m_CommandList.Add(new Cmd(sendData));
byte[] receiveData = WaitReceive(0x0E);
if (receiveData != null)
{
return new HuoDaoRpt(receiveData);
}
return null;
}
#endregion
#region SET_HUODAO PC發送配置貨道信息
/// <summary>
/// PC發送配置貨道信息
/// </summary>
/// <param name="Cabinet">箱櫃號</param>
/// <param name="hd_no">貨道邏輯編號,十進制</param>
/// <param name="Hd_id">商品ID號</param>
/// <param name="Hd_count">貨道剩余量</param>
/// <param name="Hd_price">貨道單價</param>
/// <param name="Reserved">保留字段VMC忽略此字段,PC最好將此字段置為0</param>
public bool SET_HUODAO(byte Cabinet, int hd_no, int Hd_id, int Hd_count, int Hd_price, int Reserved = 0)
{
List<byte> sendData = new List<byte>() { 0xE5, 0x00, 0x00, 0x41, 0x93 };
sendData.Add(Cabinet);
sendData.Add((byte)hd_no);
sendData.Add((byte)Hd_id);
sendData.Add((byte)Hd_count);
sendData.AddRange(CommonUtil.Int2ByteArray(Hd_price, 2));
sendData.AddRange(CommonUtil.Int2ByteArray(Reserved, 2));
m_CommandList.Add(new Cmd(sendData, new MT(sendData)));
return SendSuccess(0x93, 0x00);
}
#endregion
#region 開啟紙硬幣器
/// <summary>
/// 現金收銀模組(紙幣器、硬幣器)開關
/// </summary>
/// <param name="open">true:開,false:關</param>
public bool CtrlCoinPaper(bool open)
{
if (open)
{
return CONTROL_IND(2, new byte[] { 1 });
}
else
{
return CONTROL_IND(2, new byte[] { 0 });
}
}
#endregion
#region 找零
/// <summary>
/// 找零
/// 與手工撥弄物理找零開關相同
/// </summary>
public bool MakeChange()
{
return CONTROL_IND(6, new byte[] { 0 });
}
#endregion
#region 獲取硬幣器信息
/// <summary>
/// 獲取硬幣器信息
/// </summary>
public InfoRpt_17 GetCoinInfo()
{
return new InfoRpt_17(GET_INFO(17));
}
#endregion
#region 獲取紙幣器信息
/// <summary>
/// 獲取紙幣器信息
/// </summary>
public InfoRpt_16 GetPaperInfo()
{
return new InfoRpt_16(GET_INFO(16));
}
#endregion
#region 獲取現金投幣報告
/// <summary>
/// 獲取現金投幣報告
/// </summary>
public PayinRpt GetPayinRpt()
{
byte[] receiveData = Receive(0x06, 0x00);
if (receiveData != null)
{
return new PayinRpt(receiveData);
}
return null;
}
#endregion
#region 獲取用戶投幣余額
/// <summary>
/// 獲取用戶投幣余額
/// </summary>
public InfoRpt_3 GetRemaiderAmount()
{
byte[] receiveData = WaitReceive(0x11, 0x003);
if (receiveData != null)
{
return new InfoRpt_3(receiveData);
}
return null;
}
#endregion
}
}
View Code
ReceiveDataCollection類和ReceiveData類:

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MachineJPDll.Models
{
/// <summary>
/// 從串口接收到的數據(數據已通過驗證)
/// </summary>
public class ReceiveData
{
/// <summary>
/// 從串口接收到的數據(數據已通過驗證)
/// </summary>
public byte[] Data { get; set; }
/// <summary>
/// 添加到集合ReceiveDataCollection的時間
/// </summary>
public DateTime AddTime { get; set; }
/// <summary>
/// 消息類型
/// </summary>
public byte Type { get; set; }
/// <summary>
/// 消息子類型
/// </summary>
public byte Subtype { get; set; }
/// <summary>
/// 從串口接收到的數據(數據已通過驗證)
/// </summary>
/// <param name="type">消息類型</param>
/// <param name="subtype">消息子類型</param>
/// <param name="data">從串口接收到的數據(數據已通過驗證)</param>
/// <param name="addTime">添加到集合ReceiveDataCollection的時間</param>
public ReceiveData(byte type, byte subtype, byte[] data, DateTime addTime)
{
this.Type = type;
this.Subtype = subtype;
this.Data = data;
this.AddTime = addTime;
}
}
}
View Code

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MachineJPDll.Models
{
/// <summary>
/// 從串口接收到的數據集合(數據已通過驗證)
/// </summary>
public class ReceiveDataCollection
{
/// <summary>
/// 從串口接收到的數據集合(數據已通過驗證)
/// </summary>
private List<ReceiveData> m_ReceiveDataList = new List<ReceiveData>();
/// <summary>
/// 數據過期時間
/// </summary>
private int m_Timeout = 3;
private static object _lock = new object();
/// <summary>
/// 添加到集合
/// </summary>
/// <param name="type">消息類型</param>
/// <param name="subtype">消息子類型</param>
/// <param name="data">從串口接收到的數據(數據已通過驗證)</param>
public void Add(byte type, byte subtype, byte[] data)
{
lock (_lock)
{
ReceiveData receiveData = new ReceiveData(type, subtype, data, DateTime.Now);
m_ReceiveDataList.Add(receiveData);
for (int i = m_ReceiveDataList.Count - 1; i >= 0; i--)
{
if (DateTime.Now.Subtract(m_ReceiveDataList[i].AddTime).TotalMinutes > m_Timeout)
{
m_ReceiveDataList.RemoveAt(i);
}
}
}
}
/// <summary>
/// 從集合中獲取串口接收到的數據(數據已通過驗證)
/// </summary>
/// <param name="type">消息類型</param>
/// <param name="subtype">消息子類型</param>
/// <returns>從串口接收到的數據(數據已通過驗證)</returns>
public byte[] Get(byte type, byte subtype)
{
lock (_lock)
{
ReceiveData receiveData = null;
for (int i = 0; i < m_ReceiveDataList.Count; i++)
{
if (m_ReceiveDataList[i].Type == type && m_ReceiveDataList[i].Subtype == subtype)
{
receiveData = m_ReceiveDataList[i];
m_ReceiveDataList.RemoveAt(i);
return receiveData.Data;
}
}
return null;
}
}
/// <summary>
/// 從集合中獲取串口接收到的數據(數據已通過驗證)
/// </summary>
/// <param name="type">消息類型</param>
/// <returns>從串口接收到的數據(數據已通過驗證)</returns>
public byte[] Get(byte type)
{
lock (_lock)
{
ReceiveData receiveData = null;
for (int i = 0; i < m_ReceiveDataList.Count; i++)
{
if (m_ReceiveDataList[i].Type == type)
{
receiveData = m_ReceiveDataList[i];
m_ReceiveDataList.RemoveAt(i);
return receiveData.Data;
}
}
return null;
}
}
}
}
View Code
Models:
StatusRpt類:

![]()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MachineJPDll;
using MachineJPDll.Enums;
using MachineJPDll.Utils;
namespace MachineJPDll.Models
{
/// <summary>
/// VMC狀態報告
/// </summary>
public class StatusRpt
{
/// <summary>
/// 從串口讀取的通過驗證的數據
/// </summary>
private byte[] m_data;
/// <summary>
/// VMC狀態報告
/// </summary>
/// <param name="data">從串口讀取的通過驗證的數據</param>
public StatusRpt(byte[] data)
{
m_data = data;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("出貨檢測設備狀態:{0}\r\n", check_st.ToString());
sb.AppendFormat("紙幣器狀態:{0}\r\n", bv_st.ToString());
sb.AppendFormat("硬幣器狀態:{0}\r\n", cc_st.ToString());
sb.AppendFormat("VMC狀態:{0}\r\n", vmc_st.ToString());
sb.AppendFormat("展示位狀態:{0} {1} {2} {3}\r\n", pos_st[0].ToString(), pos_st[1].ToString(), pos_st[2].ToString(), pos_st[3].ToString());
sb.AppendFormat("機器中可用的找零量總金額(包括硬幣和紙幣):{0}\r\n", change.ToString());
sb.AppendFormat("貨倉1貨倉2貨倉3貨倉4溫度:{0} {1} {2} {3}\r\n", tem1.ToString(), tem2.ToString(), tem3.ToString(), tem4.ToString());
sb.AppendFormat("貨倉狀態設置值:{0} {1} {2} {3}\r\n", tem_st[0].ToString(), tem_st[1].ToString(), tem_st[2].ToString(), tem_st[3].ToString());
if (this.自動退幣 == 255)
{
sb.AppendFormat("自動退幣時間:永不自動退幣\r\n");
}
else
{
sb.AppendFormat("自動退幣時間:{0}\r\n", 自動退幣.ToString());
}
sb.AppendFormat("找零余量(1#--6#):{0} {1} {2} {3} {4} {5}\r\n", this.找零余量1, this.找零余量2, this.找零余量3, this.找零余量4, this.找零余量5, this.找零余量6);
return sb.ToString();
}
/// <summary>
/// 出貨檢測設備狀態
/// </summary>
public CheckSt check_st
{
get
{
byte val = m_data[5];
return (CheckSt)CommonUtil.GetFromByte(val, 0, 2);
}
}
/// <summary>
/// 紙幣器狀態
/// </summary>
public DeviceSt bv_st
{
get
{
byte val = m_data[5];
return (DeviceSt)CommonUtil.GetFromByte(val, 2, 2);
}
}
/// <summary>
/// 硬幣器狀態
/// </summary>
public DeviceSt cc_st
{
get
{
byte val = m_data[5];
return (DeviceSt)CommonUtil.GetFromByte(val, 4, 2);
}
}
/// <summary>
/// VMC狀態
/// </summary>
public VmcSt vmc_st
{
get
{
byte val = m_data[5];
return (VmcSt)CommonUtil.GetFromByte(val, 6, 2);
}
}
/// <summary>
/// 展示位狀態
/// </summary>
public List<DeviceSt> pos_st
{
get
{
List<DeviceSt> deviceStList = new List<DeviceSt>();
byte val = m_data[6];
for (int i = 0; i < 4; i++)
{
DeviceSt deviceSt = (DeviceSt)CommonUtil.GetFromByte(val, i * 2, 2);
deviceStList.Add(deviceSt);
}
return deviceStList;
}
}
/// <summary>
/// 機器中,可用的找零量總金額(包括硬幣和紙幣)
/// </summary>
public int change
{
get
{
return CommonUtil.ByteArray2Int(m_data, 7, 2);
}
}
/// <summary>
/// 貨倉1 溫度,8 位有符號數,該溫度通過貨倉內傳感器獲取,單位:℃
/// </summary>
public TemSub tem1
{
get
{
return new TemSub(m_data[9]);
}
}
/// <summary>
/// 貨倉2 溫度,8 位有符號數,該溫度通過貨倉內傳感器獲取,單位:℃
/// </summary>
public TemSub tem2
{
get
{
return new TemSub(m_data[10]);
}
}
/// <summary>
/// 貨倉3 溫度,8 位有符號數,該溫度通過貨倉內傳感器獲取,單位:℃
/// </summary>
public TemSub tem3
{
get
{
return new TemSub(m_data[11]);
}
}
/// <summary>
/// 貨倉4 溫度,8 位有符號數,該溫度通過貨倉內傳感器獲取,單位:℃
/// </summary>
public TemSub tem4
{
get
{
return new TemSub(m_data[12]);
}
}
/// <summary>
/// 貨倉狀態設置值,共支持4 個貨倉
/// </summary>
public List<TemSt> tem_st
{
get
{
List<TemSt> temStList = new List<TemSt>();
for (int i = 0; i < 4; i++)
{
TemSt temSt = (TemSt)CommonUtil.GetFromByte(m_data[13], i * 2, 2);
temStList.Add(temSt);
}
return temStList;
}
}
/// <summary>
/// 自動退幣時間。
/// 0:表示商品出貨後,立即自動退幣
/// 255:表示永不自動退幣
/// 1-254:表示商品出貨後,自動退幣時間(單位:秒)
/// </summary>
public int 自動退幣
{
get
{
return m_data[14];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數量;
/// * 找零量最大為255,超過255 時上報為255;
/// * 找零量單位為“個”,代表可找零硬幣的個數。
/// </summary>
public int 找零余量1
{
get
{
return m_data[15];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數量;
/// * 找零量最大為255,超過255 時上報為255;
/// * 找零量單位為“個”,代表可找零硬幣的個數。
/// </summary>
public int 找零余量2
{
get
{
return m_data[16];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數量;
/// * 找零量最大為255,超過255 時上報為255;
/// * 找零量單位為“個”,代表可找零硬幣的個數。
/// </summary>
public int 找零余量3
{
get
{
return m_data[17];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數量;
/// * 找零量最大為255,超過255 時上報為255;
/// * 找零量單位為“個”,代表可找零硬幣的個數。
/// </summary>
public int 找零余量4
{
get
{
return m_data[18];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數量;
/// * 找零量最大為255,超過255 時上報為255;
/// * 找零量單位為“個”,代表可找零硬幣的個數。
/// </summary>
public int 找零余量5
{
get
{
return m_data[19];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數量;
/// * 找零量最大為255,超過255 時上報為255;
/// * 找零量單位為“個”,代表可找零硬幣的個數。
/// </summary>
public int 找零余量6
{
get
{
return m_data[20];
}
}
}
}
View Code
GitHub地址:https://github.com/0611163/Machine.git