通過C#實(shí)現(xiàn)自動(dòng)售貨機(jī)接口
下面分幾部分介紹C#實(shí)現(xiàn)自動(dòng)售貨機(jī)接口的方法,代碼寫的非常詳細(xì),不懂的地方有注釋可以參考下。
MachineJP類:
第1部分:串口初始化,串口數(shù)據(jù)讀寫
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>
/// 售貨機(jī)接口(接口)
/// </summary>
public partial class MachineJP
{
#region 變量
/// <summary>
/// 串口資源
/// </summary>
private SerialPort m_SerialPort = null;
/// <summary>
/// 待發(fā)送給串口的命令列表
/// </summary>
private List<Cmd> m_CommandList = new List<Cmd>();
/// <summary>
/// 等待ACK_RPT或NAK_RPT的PC端向VMC端發(fā)送的消息列表
/// </summary>
private List<MT> m_WaitResultMTList = new List<MT>();
/// <summary>
/// 從串口接收的數(shù)據(jù)集合(數(shù)據(jù)已通過驗(yàn)證)
/// </summary>
private ReceiveDataCollection m_ReceiveDataCollection = new ReceiveDataCollection();
#endregion
#region 構(gòu)造函數(shù)與析構(gòu)函數(shù)
/// <summary>
/// 售貨機(jī)接口(接口)
/// </summary>
public MachineJP()
{
}
~MachineJP()
{
if (m_SerialPort != null)
{
m_SerialPort.Close();
m_SerialPort.Dispose();
m_SerialPort = null;
}
}
#endregion
#region 讀取串口數(shù)據(jù)
/// <summary>
/// 讀取串口數(shù)據(jù)
/// </summary>
/// <returns>從串口讀取的數(shù)據(jù)</returns>
private byte[] ReadPort()
{
//讀取串口數(shù)據(jù)
DateTime dt = DateTime.Now;
while (m_SerialPort.BytesToRead < 2)
{
Thread.Sleep(1);
if (DateTime.Now.Subtract(dt).TotalMilliseconds > 1500) //超時(shí)
{
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; //報(bào)文數(shù)據(jù)總長度
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 向串口發(fā)送數(shù)據(jù)
/// <summary>
/// 向串口發(fā)送數(shù)據(jù)
/// </summary>
/// <param name="cmd">待發(fā)送的命令</param>
/// <param name="SN">序列號(hào)</param>
private void WritePort(Cmd cmd, byte SN)
{
//發(fā)送數(shù)據(jù)
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 發(fā)送ACK消息
/// <summary>
/// 發(fā)送ACK消息
/// </summary>
/// <param name="SN">序列號(hào)</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">串口號(hào)(例: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 }); //初始化完成標(biāo)志
GET_STATUS();
SetDecimalPlaces(2); //設(shè)置小數(shù)點(diǎn)位數(shù)
}
#endregion
#region Close 關(guān)閉連接
/// <summary>
/// 關(guān)閉連接
/// </summary>
public void Close()
{
m_SerialPort.Close();
}
#endregion
#region 接收串口數(shù)據(jù)
/// <summary>
/// 接收串口數(shù)據(jù)
/// </summary>
/// <param name="type">消息類型</param>
/// <param name="subtype">消息子類型</param>
public byte[] Receive(byte type, byte subtype)
{
return m_ReceiveDataCollection.Get(type, subtype);
}
/// <summary>
/// 接收串口數(shù)據(jù)
/// </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>
/// 接收串口數(shù)據(jù)
/// </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 判斷消息是否發(fā)送成功
/// <summary>
/// 判斷消息是否發(fā)送成功
/// </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
}
}
第2部分:接收串口數(shù)據(jù),并響應(yīng)貨機(jī),向貨機(jī)發(fā)送數(shù)據(jù)
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using MachineJPDll.Models;
using MachineJPDll.Utils;
/*
* VMC->PC數(shù)據(jù)的接收,貨機(jī)事件的分發(fā)
*/
namespace MachineJPDll
{
partial class MachineJP
{
#region serialPort_DataReceived
/// <summary>
/// 數(shù)據(jù)接收事件的方法
/// </summary>
public void serialPort_DataReceived(object obj, SerialDataReceivedEventArgs args)
{
byte[] receiveData = ReadPort();
if (CommonUtil.ValidReceiveData(receiveData)) //只處理驗(yàn)證正確的數(shù)據(jù),不正確的數(shù)據(jù)拋棄不處理
{
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
{
//發(fā)送ACK消息
SendACK(SN);
}
}
#endregion
#region 發(fā)送ACK消息
if (CommonUtil.NeedACK(receiveData))
{
SendACK(SN); //發(fā)送ACK消息
}
#endregion
#region VMC系統(tǒng)參數(shù)
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 數(shù)據(jù)報(bào)告
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 出貨報(bào)告
if (mt.Type == 0x08)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region STATUS_RPT VMC整機(jī)狀態(tài)報(bào)告
if (mt.Type == 0x0D)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region SALEPRICE_IND 設(shè)置當(dāng)前商品售價(jià)
if (mt.Type == 0x8E)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region PAYIN_RPT VMC發(fā)送現(xiàn)金投幣報(bào)告給PC
if (mt.Type == 0x06)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region PAYOUT_RPT 出幣報(bào)告
if (mt.Type == 0x07)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region COST_RPT VMC扣款報(bào)告
if (mt.Type == 0x10)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region ACTION_RPT 售貨機(jī)行為報(bào)告
if (mt.Type == 0x0B)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
#region HUODAO_RPT VMC貨道報(bào)告
if (mt.Type == 0x0E)
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion
}
else //接收到的數(shù)據(jù)沒有驗(yàn)證通過
{
LogHelper.LogException(LogMsgType.Error, false, "數(shù)據(jù)異常", receiveData);
}
}
#endregion
}
}
第3部分:貨機(jī)狀態(tài)、投幣、出貨等接口
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數(shù)據(jù)的發(fā)送(并非直接發(fā)送,只是添加到發(fā)送列表)
*/
namespace MachineJPDll
{
partial class MachineJP
{
#region GET_SETUP
/// <summary>
/// PC通知VMC發(fā)送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控制售貨機(jī)完成對應(yīng)的動(dòng)作
/// <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 設(shè)置小數(shù)點(diǎn)位數(shù)
/// <summary>
/// 設(shè)置小數(shù)點(diǎn)位數(shù)
/// 用于PC 通知VMC,雙方的金額數(shù)據(jù)比例系數(shù)關(guān)系,PC 每次啟動(dòng)時(shí),都會(huì)給
/// VMC 下發(fā)一次type=18 的消息,VMC 需要自己永久保存該數(shù)據(jù),直到被PC 再
/// 次更新。
/// 取值范圍:0、1、2 分別表示以 元、 角 、分 為單位
/// </summary>
public bool SetDecimalPlaces(int data)
{
return CONTROL_IND(18, new byte[] { (byte)data });
}
#endregion
#region GET_STATUS PC通知VMC發(fā)送STATUS_RPT
/// <summary>
/// PC通知VMC發(fā)送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發(fā)送INFO_RPT
/// <summary>
/// PC通知VMC發(fā)送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">售貨機(jī)的箱體號(hào) 例如柜1 為 0x01 以此類推</param>
/// <param name="method">method =1:VMC 通過商品ID 指示出貨,如果商品ID 不存在,回復(fù)NAK_RPT method =2:VMC 通過貨道ID 指示VMC 出貨,如果貨道ID 不存在,回復(fù)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 設(shè)置貨道商品數(shù)量
/// <summary>
/// PC通知VMC,當(dāng)前貨道對應(yīng)商品的數(shù)量等信息
/// </summary>
/// <param name="device">表示箱柜號(hào)</param>
/// <param name="huodao">zyxxxxxx “z”固定填0 “y”固定填0 “xxxxxx”,表示商品余量,如果商品余量大于63,則統(tǒng)一為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 設(shè)置當(dāng)前商品售價(jià)
/// <summary>
/// PC通知VMC,當(dāng)前商品售價(jià)
/// </summary>
/// <param name="device">表示箱柜號(hào)</param>
/// <param name="type">表示設(shè)置單價(jià)的方式;Type = 0:為按商品ID 發(fā)送單價(jià),可以變長發(fā)送,商品種類最大不超過80 個(gè);Type = 1: 為按貨道號(hào)發(fā)送,固定發(fā)送80 個(gè)貨道的單價(jià)信息</param>
/// <param name="sp_price">商品售價(jià)</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">出幣設(shè)備</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,從用戶投幣總額中扣款;優(yōu)先從用戶非暫存金額中扣除(紙幣盡量滯后壓鈔),參見《現(xiàn)金扣款順序》</param>
/// <param name="value">扣款金額</param>
/// <param name="type">VMC 不用理解type 的含義,只需上報(bào)對應(yīng)的COST_RPT 時(shí)回傳即可</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上報(bào)HUODAO_RPT
/// <summary>
/// PC通知VMC上報(bào)HUODAO_RPT
/// </summary>
/// <param name="device">箱柜號(hào)</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發(fā)送配置貨道信息
/// <summary>
/// PC發(fā)送配置貨道信息
/// </summary>
/// <param name="Cabinet">箱柜號(hào)</param>
/// <param name="hd_no">貨道邏輯編號(hào),十進(jìn)制</param>
/// <param name="Hd_id">商品ID號(hào)</param>
/// <param name="Hd_count">貨道剩余量</param>
/// <param name="Hd_price">貨道單價(jià)</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>
/// 現(xiàn)金收銀模組(紙幣器、硬幣器)開關(guān)
/// </summary>
/// <param name="open">true:開,false:關(guān)</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>
/// 找零
/// 與手工撥弄物理找零開關(guān)相同
/// </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 獲取現(xiàn)金投幣報(bào)告
/// <summary>
/// 獲取現(xiàn)金投幣報(bào)告
/// </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
}
}
ReceiveDataCollection類和ReceiveData類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MachineJPDll.Models
{
/// <summary>
/// 從串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)
/// </summary>
public class ReceiveData
{
/// <summary>
/// 從串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)
/// </summary>
public byte[] Data { get; set; }
/// <summary>
/// 添加到集合ReceiveDataCollection的時(shí)間
/// </summary>
public DateTime AddTime { get; set; }
/// <summary>
/// 消息類型
/// </summary>
public byte Type { get; set; }
/// <summary>
/// 消息子類型
/// </summary>
public byte Subtype { get; set; }
/// <summary>
/// 從串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)
/// </summary>
/// <param name="type">消息類型</param>
/// <param name="subtype">消息子類型</param>
/// <param name="data">從串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)</param>
/// <param name="addTime">添加到集合ReceiveDataCollection的時(shí)間</param>
public ReceiveData(byte type, byte subtype, byte[] data, DateTime addTime)
{
this.Type = type;
this.Subtype = subtype;
this.Data = data;
this.AddTime = addTime;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MachineJPDll.Models
{
/// <summary>
/// 從串口接收到的數(shù)據(jù)集合(數(shù)據(jù)已通過驗(yàn)證)
/// </summary>
public class ReceiveDataCollection
{
/// <summary>
/// 從串口接收到的數(shù)據(jù)集合(數(shù)據(jù)已通過驗(yàn)證)
/// </summary>
private List<ReceiveData> m_ReceiveDataList = new List<ReceiveData>();
/// <summary>
/// 數(shù)據(jù)過期時(shí)間
/// </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">從串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)</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>
/// 從集合中獲取串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)
/// </summary>
/// <param name="type">消息類型</param>
/// <param name="subtype">消息子類型</param>
/// <returns>從串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)</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>
/// 從集合中獲取串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)
/// </summary>
/// <param name="type">消息類型</param>
/// <returns>從串口接收到的數(shù)據(jù)(數(shù)據(jù)已通過驗(yàn)證)</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;
}
}
}
}
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狀態(tài)報(bào)告
/// </summary>
public class StatusRpt
{
/// <summary>
/// 從串口讀取的通過驗(yàn)證的數(shù)據(jù)
/// </summary>
private byte[] m_data;
/// <summary>
/// VMC狀態(tài)報(bào)告
/// </summary>
/// <param name="data">從串口讀取的通過驗(yàn)證的數(shù)據(jù)</param>
public StatusRpt(byte[] data)
{
m_data = data;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("出貨檢測設(shè)備狀態(tài):{0}\r\n", check_st.ToString());
sb.AppendFormat("紙幣器狀態(tài):{0}\r\n", bv_st.ToString());
sb.AppendFormat("硬幣器狀態(tài):{0}\r\n", cc_st.ToString());
sb.AppendFormat("VMC狀態(tài):{0}\r\n", vmc_st.ToString());
sb.AppendFormat("展示位狀態(tài):{0} {1} {2} {3}\r\n", pos_st[0].ToString(), pos_st[1].ToString(), pos_st[2].ToString(), pos_st[3].ToString());
sb.AppendFormat("機(jī)器中可用的找零量總金額(包括硬幣和紙幣):{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("貨倉狀態(tài)設(shè)置值:{0} {1} {2} {3}\r\n", tem_st[0].ToString(), tem_st[1].ToString(), tem_st[2].ToString(), tem_st[3].ToString());
if (this.自動(dòng)退幣 == 255)
{
sb.AppendFormat("自動(dòng)退幣時(shí)間:永不自動(dòng)退幣\r\n");
}
else
{
sb.AppendFormat("自動(dòng)退幣時(shí)間:{0}\r\n", 自動(dòng)退幣.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>
/// 出貨檢測設(shè)備狀態(tài)
/// </summary>
public CheckSt check_st
{
get
{
byte val = m_data[5];
return (CheckSt)CommonUtil.GetFromByte(val, 0, 2);
}
}
/// <summary>
/// 紙幣器狀態(tài)
/// </summary>
public DeviceSt bv_st
{
get
{
byte val = m_data[5];
return (DeviceSt)CommonUtil.GetFromByte(val, 2, 2);
}
}
/// <summary>
/// 硬幣器狀態(tài)
/// </summary>
public DeviceSt cc_st
{
get
{
byte val = m_data[5];
return (DeviceSt)CommonUtil.GetFromByte(val, 4, 2);
}
}
/// <summary>
/// VMC狀態(tài)
/// </summary>
public VmcSt vmc_st
{
get
{
byte val = m_data[5];
return (VmcSt)CommonUtil.GetFromByte(val, 6, 2);
}
}
/// <summary>
/// 展示位狀態(tài)
/// </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>
/// 機(jī)器中,可用的找零量總金額(包括硬幣和紙幣)
/// </summary>
public int change
{
get
{
return CommonUtil.ByteArray2Int(m_data, 7, 2);
}
}
/// <summary>
/// 貨倉1 溫度,8 位有符號(hào)數(shù),該溫度通過貨倉內(nèi)傳感器獲取,單位:℃
/// </summary>
public TemSub tem1
{
get
{
return new TemSub(m_data[9]);
}
}
/// <summary>
/// 貨倉2 溫度,8 位有符號(hào)數(shù),該溫度通過貨倉內(nèi)傳感器獲取,單位:℃
/// </summary>
public TemSub tem2
{
get
{
return new TemSub(m_data[10]);
}
}
/// <summary>
/// 貨倉3 溫度,8 位有符號(hào)數(shù),該溫度通過貨倉內(nèi)傳感器獲取,單位:℃
/// </summary>
public TemSub tem3
{
get
{
return new TemSub(m_data[11]);
}
}
/// <summary>
/// 貨倉4 溫度,8 位有符號(hào)數(shù),該溫度通過貨倉內(nèi)傳感器獲取,單位:℃
/// </summary>
public TemSub tem4
{
get
{
return new TemSub(m_data[12]);
}
}
/// <summary>
/// 貨倉狀態(tài)設(shè)置值,共支持4 個(gè)貨倉
/// </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>
/// 自動(dòng)退幣時(shí)間。
/// 0:表示商品出貨后,立即自動(dòng)退幣
/// 255:表示永不自動(dòng)退幣
/// 1-254:表示商品出貨后,自動(dòng)退幣時(shí)間(單位:秒)
/// </summary>
public int 自動(dòng)退幣
{
get
{
return m_data[14];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應(yīng)硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數(shù)量;
/// * 找零量最大為255,超過255 時(shí)上報(bào)為255;
/// * 找零量單位為“個(gè)”,代表可找零硬幣的個(gè)數(shù)。
/// </summary>
public int 找零余量1
{
get
{
return m_data[15];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應(yīng)硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數(shù)量;
/// * 找零量最大為255,超過255 時(shí)上報(bào)為255;
/// * 找零量單位為“個(gè)”,代表可找零硬幣的個(gè)數(shù)。
/// </summary>
public int 找零余量2
{
get
{
return m_data[16];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應(yīng)硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數(shù)量;
/// * 找零量最大為255,超過255 時(shí)上報(bào)為255;
/// * 找零量單位為“個(gè)”,代表可找零硬幣的個(gè)數(shù)。
/// </summary>
public int 找零余量3
{
get
{
return m_data[17];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應(yīng)硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數(shù)量;
/// * 找零量最大為255,超過255 時(shí)上報(bào)為255;
/// * 找零量單位為“個(gè)”,代表可找零硬幣的個(gè)數(shù)。
/// </summary>
public int 找零余量4
{
get
{
return m_data[18];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應(yīng)硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數(shù)量;
/// * 找零量最大為255,超過255 時(shí)上報(bào)為255;
/// * 找零量單位為“個(gè)”,代表可找零硬幣的個(gè)數(shù)。
/// </summary>
public int 找零余量5
{
get
{
return m_data[19];
}
}
/// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分別對應(yīng)硬幣器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每種錢幣的找零數(shù)量;
/// * 找零量最大為255,超過255 時(shí)上報(bào)為255;
/// * 找零量單位為“個(gè)”,代表可找零硬幣的個(gè)數(shù)。
/// </summary>
public int 找零余量6
{
get
{
return m_data[20];
}
}
}
}
以上就是C#實(shí)現(xiàn)自動(dòng)售貨機(jī)接口的代碼,需要的朋友可以來學(xué)習(xí)。
- C# 接口的隱式與顯示實(shí)現(xiàn)及適應(yīng)場景
- C#動(dòng)態(tài)webservice調(diào)用接口
- C#中實(shí)現(xiàn)判斷某個(gè)類是否實(shí)現(xiàn)了某個(gè)接口
- C#判斷一個(gè)類是否實(shí)現(xiàn)了某個(gè)接口3種實(shí)現(xiàn)方法
- C#中的ICustomFormatter及IFormatProvider接口用法揭秘
- C#接口interface用法實(shí)例
- C#開發(fā)微信公眾號(hào)接口開發(fā)
- C#接口實(shí)現(xiàn)方法實(shí)例分析
- C#隱式/顯示實(shí)現(xiàn)接口方法詳解
相關(guān)文章
C#記一次http協(xié)議multipart/form-data的boundary問題
這篇文章主要介紹了C#記一次http協(xié)議multipart/form-data的boundary問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
C#字符串內(nèi)存分配與駐留池學(xué)習(xí)分享
這篇文章主要介紹了C#字符串內(nèi)存分配與駐留池學(xué)習(xí)分享,大家參考使用吧2013-12-12
同時(shí)兼容JS和C#的RSA加密解密算法詳解(對web提交的數(shù)據(jù)加密傳輸)
這篇文章主要給大家介紹了關(guān)于同時(shí)兼容JS和C#的RSA加密解密算法,通過該算法可以對web提交的數(shù)據(jù)進(jìn)行加密傳輸,文中通過圖文及示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07
C#實(shí)現(xiàn)根據(jù)圖片的鏈接地址獲取圖片的后綴名
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)根據(jù)圖片的鏈接地址獲取圖片的后綴名,文中的實(shí)現(xiàn)方法講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02

