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 < )
{
Thread.Sleep(); if (DateTime.Now.Subtract(dt).TotalMilliseconds > ) //超时
{
return new byte[];
}
}
List<byte> recList = new List<byte>();
byte[] recData = new byte[m_SerialPort.BytesToRead];
m_SerialPort.Read(recData, , recData.Length);
recList.AddRange(recData);
int length = recData[] + ; //报文数据总长度
while (recList.Count < length)
{
if (m_SerialPort.BytesToRead > )
{
recData = new byte[m_SerialPort.BytesToRead];
m_SerialPort.Read(recData, , recData.Length);
recList.AddRange(recData);
}
Thread.Sleep();
} 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[] = (byte)sendData.Count;
sendData[] = 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(), , 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, , Parity.None, , StopBits.One);
m_SerialPort.ReadBufferSize = ;
m_SerialPort.WriteBufferSize = ;
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(); //设置小数点位数
}
#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 > ) return null; Thread.Sleep();
}
} /// <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 > ) return null; Thread.Sleep();
}
}
#endregion #region 判断消息是否发送成功
/// <summary>
/// 判断消息是否发送成功
/// </summary>
public bool SendSuccess(byte type, byte subtype)
{
DateTime time = DateTime.Now;
while (true)
{
if (DateTime.Now.Subtract(time).TotalMinutes > )
{
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();
}
}
#endregion }
}
第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 > )
{
WritePort(m_CommandList[], SN);
m_CommandList.RemoveAt();
}
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 > )
{
m_ReceiveDataCollection.Add(m_WaitResultMTList[].Type, m_WaitResultMTList[].Subtype, receiveData);
m_WaitResultMTList.RemoveAt();
}
}
#endregion #region INFO_RPT 数据报告
if (mt.Type == 0x11)
{
#region 纸币器信息
if (mt.Subtype == )
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion #region 硬币器信息
if (mt.Subtype == )
{
m_ReceiveDataCollection.Add(mt.Type, mt.Subtype, receiveData);
}
#endregion #region 用户投币余额
if (mt.Subtype == )
{
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 }
}
第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 > )
{
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(, 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, ));
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 = ; i < huodao.Count; i++)
{
if (huodao[i] > )
{
huodao[i] = ;
}
}
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, ));
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, ));
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 = )
{
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, ));
sendData.AddRange(CommonUtil.Int2ByteArray(Reserved, ));
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(, new byte[] { });
}
else
{
return CONTROL_IND(, new byte[] { });
}
}
#endregion #region 找零
/// <summary>
/// 找零
/// 与手工拨弄物理找零开关相同
/// </summary>
public bool MakeChange()
{
return CONTROL_IND(, new byte[] { });
}
#endregion #region 获取硬币器信息
/// <summary>
/// 获取硬币器信息
/// </summary>
public InfoRpt_17 GetCoinInfo()
{
return new InfoRpt_17(GET_INFO());
}
#endregion #region 获取纸币器信息
/// <summary>
/// 获取纸币器信息
/// </summary>
public InfoRpt_16 GetPaperInfo()
{
return new InfoRpt_16(GET_INFO());
}
#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 }
}
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;
}
}
}
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 = ;
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 - ; i >= ; 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 = ; 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 = ; 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状态报告
/// </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[].ToString(), pos_st[].ToString(), pos_st[].ToString(), pos_st[].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[].ToString(), tem_st[].ToString(), tem_st[].ToString(), tem_st[].ToString());
if (this.自动退币 == )
{
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[];
return (CheckSt)CommonUtil.GetFromByte(val, , );
}
} /// <summary>
/// 纸币器状态
/// </summary>
public DeviceSt bv_st
{
get
{
byte val = m_data[];
return (DeviceSt)CommonUtil.GetFromByte(val, , );
}
} /// <summary>
/// 硬币器状态
/// </summary>
public DeviceSt cc_st
{
get
{
byte val = m_data[];
return (DeviceSt)CommonUtil.GetFromByte(val, , );
}
} /// <summary>
/// VMC状态
/// </summary>
public VmcSt vmc_st
{
get
{
byte val = m_data[];
return (VmcSt)CommonUtil.GetFromByte(val, , );
}
} /// <summary>
/// 展示位状态
/// </summary>
public List<DeviceSt> pos_st
{
get
{
List<DeviceSt> deviceStList = new List<DeviceSt>(); byte val = m_data[];
for (int i = ; i < ; i++)
{
DeviceSt deviceSt = (DeviceSt)CommonUtil.GetFromByte(val, i * , );
deviceStList.Add(deviceSt);
} return deviceStList;
}
} /// <summary>
/// 机器中,可用的找零量总金额(包括硬币和纸币)
/// </summary>
public int change
{
get
{
return CommonUtil.ByteArray2Int(m_data, , );
}
} /// <summary>
/// 货仓1 温度,8 位有符号数,该温度通过货仓内传感器获取,单位:℃
/// </summary>
public TemSub tem1
{
get
{
return new TemSub(m_data[]);
}
} /// <summary>
/// 货仓2 温度,8 位有符号数,该温度通过货仓内传感器获取,单位:℃
/// </summary>
public TemSub tem2
{
get
{
return new TemSub(m_data[]);
}
} /// <summary>
/// 货仓3 温度,8 位有符号数,该温度通过货仓内传感器获取,单位:℃
/// </summary>
public TemSub tem3
{
get
{
return new TemSub(m_data[]);
}
} /// <summary>
/// 货仓4 温度,8 位有符号数,该温度通过货仓内传感器获取,单位:℃
/// </summary>
public TemSub tem4
{
get
{
return new TemSub(m_data[]);
}
} /// <summary>
/// 货仓状态设置值,共支持4 个货仓
/// </summary>
public List<TemSt> tem_st
{
get
{
List<TemSt> temStList = new List<TemSt>();
for (int i = ; i < ; i++)
{
TemSt temSt = (TemSt)CommonUtil.GetFromByte(m_data[], i * , );
temStList.Add(temSt);
}
return temStList;
}
} /// <summary>
/// 自动退币时间。
/// 0:表示商品出货后,立即自动退币
/// 255:表示永不自动退币
/// 1-254:表示商品出货后,自动退币时间(单位:秒)
/// </summary>
public int 自动退币
{
get
{
return m_data[];
}
} /// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每种钱币的找零数量;
/// * 找零量最大为255,超过255 时上报为255;
/// * 找零量单位为“个”,代表可找零硬币的个数。
/// </summary>
public int 找零余量1
{
get
{
return m_data[];
}
} /// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每种钱币的找零数量;
/// * 找零量最大为255,超过255 时上报为255;
/// * 找零量单位为“个”,代表可找零硬币的个数。
/// </summary>
public int 找零余量2
{
get
{
return m_data[];
}
} /// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每种钱币的找零数量;
/// * 找零量最大为255,超过255 时上报为255;
/// * 找零量单位为“个”,代表可找零硬币的个数。
/// </summary>
public int 找零余量3
{
get
{
return m_data[];
}
} /// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每种钱币的找零数量;
/// * 找零量最大为255,超过255 时上报为255;
/// * 找零量单位为“个”,代表可找零硬币的个数。
/// </summary>
public int 找零余量4
{
get
{
return m_data[];
}
} /// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每种钱币的找零数量;
/// * 找零量最大为255,超过255 时上报为255;
/// * 找零量单位为“个”,代表可找零硬币的个数。
/// </summary>
public int 找零余量5
{
get
{
return m_data[];
}
} /// <summary>
/// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
/// 1#”…“找零6#”中每种钱币的找零数量;
/// * 找零量最大为255,超过255 时上报为255;
/// * 找零量单位为“个”,代表可找零硬币的个数。
/// </summary>
public int 找零余量6
{
get
{
return m_data[];
}
} }
}
GitHub地址:https://github.com/0611163/Machine.git
C#骏鹏自动售货机接口的更多相关文章
- 玩转华为物联网IoTDA服务系列三-自动售货机销售分析场景示例
场景简介 通过收集自动售货机系统的销售数据,EI数据分析售货销量状况. 该场景主要描述的是设备可以通过MQTT协议与物联网平台进行交互,应用侧可以到物联网平台订阅设备侧变化的通知,用户可以在控制台或通 ...
- 使用NewLife网络库构建可靠的自动售货机Socket服务端(一)
最近有个基于tcp socket 协议和设备交互需求,想到了新生命团队的各种组件,所以决定用NewLife网络库作为服务端来完成一系列的信息交互. 第一,首先说一下我们需要实现的功能需求吧 1,首先客 ...
- YTU 2598: 编程题B-小平智斗自动售货机
2598: 编程题B-小平智斗自动售货机 时间限制: 1 Sec 内存限制: 128 MB 提交: 268 解决: 69 题目描述 LYH自动售货机在销售商品时,具有自动找钱功能.但是找零的最小单 ...
- 开发实践丨用小熊派STM32开发板模拟自动售货机
摘要:本文内容是讲述用小熊派开发板模拟自动售货机,基于论坛提供的工程代码,通过云端开发和设备终端开发,实现终端数据在的华为云平台显示. 本文内容是讲述用小熊派开发板模拟自动售货机,基于论坛提供的工程代 ...
- 09自动售货机综设实验(含按键消抖,led和状态机)
一设计功能 1.上次状态机的练习 2这次自动售货机综设 (一)对比两次的售货机 上次售货机的关键是画出状态转移图.明确输入分几种,输出是啥,有哪些状态.如下图所示 (二)系统或综合设计的经验: 既然这 ...
- Java开发自动售货机
1:先写一个类,包括商品的基本属性 package com.xt.java.base25; public class Goods { private int ID; private String na ...
- FSM自动售货机 verilog 实现及 code 细节讲解
1.题目: 饮料1.5 元, 可投入硬币1 元 0.5 元,输出饮料 零钱 2. 画出状态机. 3.仿真结果:coin=1 --> 0.5 元 coin=2-->1元 4.关键代码分析: ...
- 有限状态机FSM(自动售报机Verilog实现)
有限状态机FSM(自动售报机Verilog实现) FSM 状态机就是一种能够描述具有逻辑顺序和时序顺序事件的方法. 状态机有两大类:Mealy型和Moore型. Moore型状态机的输出只与当前状态有 ...
- 2016.12.5 在Eclipse中为实现类impl自动生成对应接口
参考来自:http://jingyan.baidu.com/article/ab69b270d63f572ca6189f51.html 在Spring应用中,常常会用到“接口+实现类”的形式,即要实现 ...
随机推荐
- 移动App开发需要更多的PaaS平台而不是IaaS
时代的变迁,创业的大潮,越来越多的人关注了有点开发,越来越多的人了解了互联网服务术语:PaaS.IaaS.SaaS.BaaS等.今天大家在开发App的时候这么多复杂的云服务如何来选择呢? IaaS服务 ...
- Linux sort命令
200 ? "200px" : this.width)!important;} --> 介绍 sort命令是一个文本排序命令,它能对标准输入和文本文件进行排序,并且能将结果通 ...
- HTML、CSS部分
要点:对Web标准的理解.浏览器差异.CSS基本功:布局.盒子模型.选择器优先级及使用.HTML5.CSS3.移动端开发 技术等 1.Doctype作用? 严格模式与混杂模式-如何触发这两种模式,区分 ...
- Oracle常见名词解析
创建用户 概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用. create user 用户名 identified by 密码; ...
- MyBatis学习总结(一)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- 每天一个linux命令(28):tar命令
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linux ...
- vuejs件同一个挂载点上切换组
vuejs件同一个挂载点上切换组 动态组件 http://cn.vuejs.org/guide/components.html#动态组件 多个组件可以使用同一个挂载点,然后动态地在它们之间切换.使用保 ...
- 为什么说基于TCP的移动端IM仍然需要心跳保活?
1.前言 很多人认为,TCP协议自身先天就有KeepAlive机制,为何基于它的通讯链接,仍然需要在应用层实现额外的心跳保活?本文将从移动端IM实践的角度告诉你,即使使用的是TCP协议,应用层的心跳保 ...
- winform 程序制作自己的数字签名(续)
在上一篇文章<winform 程序制作自己的数字签名>中我们已经可以得到我们程序定制的数字签名了,但是比较讨厌的是每次编译之后,数字签名需要重新手动添加. 我们需要的是在程序编译时自动添加 ...
- Object.create
var emptyObject = Object.create(null); var emptyObject = Object.create(null); var emptyObject = {}; ...