栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

通过C#实现自动售货机接口

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过C#实现自动售货机接口

下面分几部分介绍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
{
  /// 
  /// 售货机接口(接口)
  /// 
  public partial class MachineJP
  {
    #region 变量
    /// 
    /// 串口资源
    /// 
    private SerialPort m_SerialPort = null;
    /// 
    /// 待发送给串口的命令列表
    /// 
    private List m_CommandList = new List();
    /// 
    /// 等待ACK_RPT或NAK_RPT的PC端向VMC端发送的消息列表
    /// 
    private List m_WaitResultMTList = new List();
    /// 
    /// 从串口接收的数据集合(数据已通过验证)
    /// 
    private ReceiveDataCollection m_ReceiveDataCollection = new ReceiveDataCollection();
    #endregion

    #region 构造函数与析构函数
    /// 
    /// 售货机接口(接口)
    /// 
    public MachineJP()
    {

    }

    ~MachineJP()
    {
      if (m_SerialPort != null)
      {
 m_SerialPort.Close();
 m_SerialPort.Dispose();
 m_SerialPort = null;
      }
    }
    #endregion

    #region 读取串口数据
    /// 
    /// 读取串口数据
    /// 
    /// 从串口读取的数据
    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 recList = new List();
      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 向串口发送数据
    /// 
    /// 向串口发送数据
    /// 
    /// 待发送的命令
    /// 序列号
    private void WritePort(Cmd cmd, byte SN)
    {
      //发送数据
      List 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消息
    /// 
    /// 发送ACK消息
    /// 
    /// 序列号
    private void SendACK(byte SN)
    {
      List sendData = new List() { 0xE5, 0x00, 0x00, 0x40, 0x80 };
      WritePort(new Cmd(sendData), SN);
    }
    #endregion

    #region Init 初始化
    /// 
    /// 初始化
    /// 
    /// 串口号(例:COM1)
    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 关闭连接
    /// 
    /// 关闭连接
    /// 
    public void Close()
    {
      m_SerialPort.Close();
    }
    #endregion

    #region 接收串口数据
    /// 
    /// 接收串口数据
    /// 
    /// 消息类型
    /// 消息子类型
    public byte[] Receive(byte type, byte subtype)
    {
      return m_ReceiveDataCollection.Get(type, subtype);
    }

    /// 
    /// 接收串口数据
    /// 
    /// 消息类型
    /// 消息子类型
    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);
      }
    }

    /// 
    /// 接收串口数据
    /// 
    /// 消息类型
    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 判断消息是否发送成功
    /// 
    /// 判断消息是否发送成功
    /// 
    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部分:接收串口数据,并响应货机,向货机发送数据 

 

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using MachineJPDll.Models;
using MachineJPDll.Utils;



namespace MachineJPDll
{
  partial class MachineJP
  {
    #region serialPort_DataReceived
    /// 
    /// 数据接收事件的方法
    /// 
    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

  }
}

 第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;



namespace MachineJPDll
{
  partial class MachineJP
  {
    #region GET_SETUP
    /// 
    /// PC通知VMC发送VMC_SETUP
    /// 
    public VmcSetup GET_SETUP()
    {
      List sendData = new List() { 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控制售货机完成对应的动作
    /// 
    /// PC控制VMC
    /// 
    public bool CONTROL_IND(byte subtype, byte[] value)
    {
      List sendData = new List() { 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 设置小数点位数
    /// 
    /// 设置小数点位数
    /// 用于PC 通知VMC,双方的金额数据比例系数关系,PC 每次启动时,都会给
    /// VMC 下发一次type=18 的消息,VMC 需要自己永久保存该数据,直到被PC 再
    /// 次更新。
    /// 取值范围:0、1、2 分别表示以 元、 角 、分 为单位
    /// 
    public bool SetDecimalPlaces(int data)
    {
      return CONTROL_IND(18, new byte[] { (byte)data });
    }
    #endregion

    #region GET_STATUS PC通知VMC发送STATUS_RPT
    /// 
    /// PC通知VMC发送STATUS_RPT
    /// 
    public StatusRpt GET_STATUS()
    {
      List sendData = new List() { 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
    /// 
    /// PC通知VMC发送INFO_RPT
    /// 
    public byte[] GET_INFO(byte subtype)
    {
      List sendData = new List() { 0xE5, 0x00, 0x00, 0x40, 0x8C };
      sendData.Add(subtype);
      m_CommandList.Add(new Cmd(sendData));

      return WaitReceive(0x11);
    }
    #endregion

    #region VENDOUT_IND 出货
    /// 
    /// PC出货指示
    /// 
    /// 售货机的箱体号 例如柜1 为 0x01 以此类推
    /// method =1:VMC 通过商品ID 指示出货,如果商品ID 不存在,回复NAK_RPT method =2:VMC 通过货道ID 指示VMC 出货,如果货道ID 不存在,回复NAK_RPT
    /// sp_id:通过商品ID 指示VMC 出货 hd_id:通过货道ID 指示VMC 出货
    /// 如果type=0,cost 代表本次出货扣款金额 如果TYPE 不为0,则COST 必须为0
    /// cost 代表本次出货扣款金额
    public VendoutRpt VENDOUT_IND(byte device, byte method, byte sp_id_hd_id, byte type, int cost)
    {
      List sendData = new List() { 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 设置货道商品数量
    /// 
    /// PC通知VMC,当前货道对应商品的数量等信息
    /// 
    /// 表示箱柜号
    /// zyxxxxxx “z”固定填0 “y”固定填0 “xxxxxx”,表示商品余量,如果商品余量大于63,则统一为63
    public bool HUODAO_SET_IND(byte device, List huodao)
    {
      List sendData = new List() { 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(a => (byte)a));
      m_CommandList.Add(new Cmd(sendData, new MT(sendData)));

      return SendSuccess(0x8F, 0x00);
    }
    #endregion

    #region SALEPRICE_IND 设置当前商品售价
    /// 
    /// PC通知VMC,当前商品售价
    /// 
    /// 表示箱柜号
    /// 表示设置单价的方式;Type = 0:为按商品ID 发送单价,可以变长发送,商品种类最大不超过80 个;Type = 1: 为按货道号发送,固定发送80 个货道的单价信息
    /// 商品售价
    public bool SALEPRICE_IND(byte device, byte type, List sp_price)
    {
      List sendData = new List() { 0xE5, 0x00, 0x00, 0x41, 0x8E };
      sendData.Add(device);
      sendData.Add(type);
      sendData.AddRange(sp_price.ConvertAll(a => (byte)a));
      m_CommandList.Add(new Cmd(sendData, new MT(sendData)));

      return SendSuccess(0x8E, 0x00);
    }
    #endregion

    #region PAYOUT_IND PC指示VMC出币
    /// 
    /// PC指示VMC出币
    /// 
    /// 出币设备
    /// 本次出币总金额
    /// 出币类型 无需理解type 的含义,只需要在出币完成后的PAYOUT_RPT 中将该type 值回传给PC 即可
    public PayoutRpt PAYOUT_IND(PayoutType device, int value, byte type)
    {
      List sendData = new List() { 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扣款指示
    /// 
    /// PC扣款指示
    /// 
    /// device=0,从用户投币总额中扣款;优先从用户非暂存金额中扣除(纸币尽量滞后压钞),参见《现金扣款顺序》
    /// 扣款金额
    /// VMC 不用理解type 的含义,只需上报对应的COST_RPT 时回传即可
    public CostRpt COST_IND(byte device, int value, byte type)
    {
      List sendData = new List() { 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
    /// 
    /// PC通知VMC上报HUODAO_RPT
    /// 
    /// 箱柜号
    public HuoDaoRpt GET_HUODAO(byte device)
    {
      List sendData = new List() { 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发送配置货道信息
    /// 
    /// PC发送配置货道信息
    /// 
    /// 箱柜号
    /// 货道逻辑编号,十进制
    /// 商品ID号
    /// 货道剩余量
    /// 货道单价
    /// 保留字段VMC忽略此字段,PC最好将此字段置为0
    public bool SET_HUODAO(byte Cabinet, int hd_no, int Hd_id, int Hd_count, int Hd_price, int Reserved = 0)
    {
      List sendData = new List() { 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 开启纸硬币器
    /// 
    /// 现金收银模组(纸币器、硬币器)开关
    /// 
    /// true:开,false:关
    public bool CtrlCoinPaper(bool open)
    {
      if (open)
      {
 return CONTROL_IND(2, new byte[] { 1 });
      }
      else
      {
 return CONTROL_IND(2, new byte[] { 0 });
      }
    }
    #endregion

    #region 找零
    /// 
    /// 找零
    /// 与手工拨弄物理找零开关相同
    /// 
    public bool MakeChange()
    {
      return CONTROL_IND(6, new byte[] { 0 });
    }
    #endregion

    #region 获取硬币器信息
    /// 
    /// 获取硬币器信息
    /// 
    public InfoRpt_17 GetCoinInfo()
    {
      return new InfoRpt_17(GET_INFO(17));
    }
    #endregion

    #region 获取纸币器信息
    /// 
    /// 获取纸币器信息
    /// 
    public InfoRpt_16 GetPaperInfo()
    {
      return new InfoRpt_16(GET_INFO(16));
    }
    #endregion

    #region 获取现金投币报告
    /// 
    /// 获取现金投币报告
    /// 
    public PayinRpt GetPayinRpt()
    {
      byte[] receiveData = Receive(0x06, 0x00);
      if (receiveData != null)
      {
 return new PayinRpt(receiveData);
      }
      return null;
    }
    #endregion

    #region 获取用户投币余额
    /// 
    /// 获取用户投币余额
    /// 
    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
{
  /// 
  /// 从串口接收到的数据(数据已通过验证)
  /// 
  public class ReceiveData
  {
    /// 
    /// 从串口接收到的数据(数据已通过验证)
    /// 
    public byte[] Data { get; set; }
    /// 
    /// 添加到集合ReceiveDataCollection的时间
    /// 
    public DateTime AddTime { get; set; }
    /// 
    /// 消息类型
    /// 
    public byte Type { get; set; }
    /// 
    /// 消息子类型
    /// 
    public byte Subtype { get; set; }

    /// 
    /// 从串口接收到的数据(数据已通过验证)
    /// 
    /// 消息类型
    /// 消息子类型
    /// 从串口接收到的数据(数据已通过验证)
    /// 添加到集合ReceiveDataCollection的时间
    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
{
  /// 
  /// 从串口接收到的数据集合(数据已通过验证)
  /// 
  public class ReceiveDataCollection
  {
    /// 
    /// 从串口接收到的数据集合(数据已通过验证)
    /// 
    private List m_ReceiveDataList = new List();
    /// 
    /// 数据过期时间
    /// 
    private int m_Timeout = 3;
    private static object _lock = new object();

    /// 
    /// 添加到集合
    /// 
    /// 消息类型
    /// 消息子类型
    /// 从串口接收到的数据(数据已通过验证)
    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);
   }
 }
      }
    }

    /// 
    /// 从集合中获取串口接收到的数据(数据已通过验证)
    /// 
    /// 消息类型
    /// 消息子类型
    /// 从串口接收到的数据(数据已通过验证)
    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;
      }
    }

    /// 
    /// 从集合中获取串口接收到的数据(数据已通过验证)
    /// 
    /// 消息类型
    /// 从串口接收到的数据(数据已通过验证)
    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
{
  /// 
  /// VMC状态报告
  /// 
  public class StatusRpt
  {
    /// 
    /// 从串口读取的通过验证的数据
    /// 
    private byte[] m_data;

    /// 
    /// VMC状态报告
    /// 
    /// 从串口读取的通过验证的数据
    public StatusRpt(byte[] data)
    {
      m_data = data;
    }

    public override string ToString()
    {
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("出货检测设备状态:{0}rn", check_st.ToString());
      sb.AppendFormat("纸币器状态:{0}rn", bv_st.ToString());
      sb.AppendFormat("硬币器状态:{0}rn", cc_st.ToString());
      sb.AppendFormat("VMC状态:{0}rn", vmc_st.ToString());
      sb.AppendFormat("展示位状态:{0} {1} {2} {3}rn", pos_st[0].ToString(), pos_st[1].ToString(), pos_st[2].ToString(), pos_st[3].ToString());
      sb.AppendFormat("机器中可用的找零量总金额(包括硬币和纸币):{0}rn", change.ToString());
      sb.AppendFormat("货仓1货仓2货仓3货仓4温度:{0} {1} {2} {3}rn", tem1.ToString(), tem2.ToString(), tem3.ToString(), tem4.ToString());
      sb.AppendFormat("货仓状态设置值:{0} {1} {2} {3}rn", tem_st[0].ToString(), tem_st[1].ToString(), tem_st[2].ToString(), tem_st[3].ToString());
      if (this.自动退币 == 255)
      {
 sb.AppendFormat("自动退币时间:永不自动退币rn");
      }
      else
      {
 sb.AppendFormat("自动退币时间:{0}rn", 自动退币.ToString());
      }
      sb.AppendFormat("找零余量(1#--6#):{0} {1} {2} {3} {4} {5}rn", this.找零余量1, this.找零余量2, this.找零余量3, this.找零余量4, this.找零余量5, this.找零余量6);

      return sb.ToString();
    }

    /// 
    /// 出货检测设备状态
    /// 
    public CheckSt check_st
    {
      get
      {
 byte val = m_data[5];
 return (CheckSt)CommonUtil.GetFromByte(val, 0, 2);
      }
    }

    /// 
    /// 纸币器状态
    /// 
    public DeviceSt bv_st
    {
      get
      {
 byte val = m_data[5];
 return (DeviceSt)CommonUtil.GetFromByte(val, 2, 2);
      }
    }

    /// 
    /// 硬币器状态
    /// 
    public DeviceSt cc_st
    {
      get
      {
 byte val = m_data[5];
 return (DeviceSt)CommonUtil.GetFromByte(val, 4, 2);
      }
    }

    /// 
    /// VMC状态
    /// 
    public VmcSt vmc_st
    {
      get
      {
 byte val = m_data[5];
 return (VmcSt)CommonUtil.GetFromByte(val, 6, 2);
      }
    }

    /// 
    /// 展示位状态
    /// 
    public List pos_st
    {
      get
      {
 List deviceStList = new List();

 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;
      }
    }

    /// 
    /// 机器中,可用的找零量总金额(包括硬币和纸币)
    /// 
    public int change
    {
      get
      {
 return CommonUtil.ByteArray2Int(m_data, 7, 2);
      }
    }

    /// 
    /// 货仓1 温度,8 位有符号数,该温度通过货仓内传感器获取,单位:℃
    /// 
    public TemSub tem1
    {
      get
      {
 return new TemSub(m_data[9]);
      }
    }

    /// 
    /// 货仓2 温度,8 位有符号数,该温度通过货仓内传感器获取,单位:℃
    /// 
    public TemSub tem2
    {
      get
      {
 return new TemSub(m_data[10]);
      }
    }

    /// 
    /// 货仓3 温度,8 位有符号数,该温度通过货仓内传感器获取,单位:℃
    /// 
    public TemSub tem3
    {
      get
      {
 return new TemSub(m_data[11]);
      }
    }

    /// 
    /// 货仓4 温度,8 位有符号数,该温度通过货仓内传感器获取,单位:℃
    /// 
    public TemSub tem4
    {
      get
      {
 return new TemSub(m_data[12]);
      }
    }

    /// 
    /// 货仓状态设置值,共支持4 个货仓
    /// 
    public List tem_st
    {
      get
      {
 List temStList = new List();
 for (int i = 0; i < 4; i++)
 {
   TemSt temSt = (TemSt)CommonUtil.GetFromByte(m_data[13], i * 2, 2);
   temStList.Add(temSt);
 }
 return temStList;
      }
    }

    /// 
    /// 自动退币时间。
    /// 0:表示商品出货后,立即自动退币
    /// 255:表示永不自动退币
    /// 1-254:表示商品出货后,自动退币时间(单位:秒)
    /// 
    public int 自动退币
    {
      get
      {
 return m_data[14];
      }
    }

    /// 
    /// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
    /// 1#”…“找零6#”中每种钱币的找零数量;
    /// * 找零量最大为255,超过255 时上报为255;
    /// * 找零量单位为“个”,代表可找零硬币的个数。
    /// 
    public int 找零余量1
    {
      get
      {
 return m_data[15];
      }
    }

    /// 
    /// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
    /// 1#”…“找零6#”中每种钱币的找零数量;
    /// * 找零量最大为255,超过255 时上报为255;
    /// * 找零量单位为“个”,代表可找零硬币的个数。
    /// 
    public int 找零余量2
    {
      get
      {
 return m_data[16];
      }
    }

    /// 
    /// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
    /// 1#”…“找零6#”中每种钱币的找零数量;
    /// * 找零量最大为255,超过255 时上报为255;
    /// * 找零量单位为“个”,代表可找零硬币的个数。
    /// 
    public int 找零余量3
    {
      get
      {
 return m_data[17];
      }
    }

    /// 
    /// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
    /// 1#”…“找零6#”中每种钱币的找零数量;
    /// * 找零量最大为255,超过255 时上报为255;
    /// * 找零量单位为“个”,代表可找零硬币的个数。
    /// 
    public int 找零余量4
    {
      get
      {
 return m_data[18];
      }
    }

    /// 
    /// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
    /// 1#”…“找零6#”中每种钱币的找零数量;
    /// * 找零量最大为255,超过255 时上报为255;
    /// * 找零量单位为“个”,代表可找零硬币的个数。
    /// 
    public int 找零余量5
    {
      get
      {
 return m_data[19];
      }
    }

    /// 
    /// 找零余量“找零量1#”…“找零量6#”,分别对应硬币器信息INFO_RPT.type=17 的“找零
    /// 1#”…“找零6#”中每种钱币的找零数量;
    /// * 找零量最大为255,超过255 时上报为255;
    /// * 找零量单位为“个”,代表可找零硬币的个数。
    /// 
    public int 找零余量6
    {
      get
      {
 return m_data[20];
      }
    }

  }
}

以上就是C#实现自动售货机接口的代码,需要的朋友可以来学习。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/124886.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号