• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#骏鹏自动售货机接口

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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,  
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#如何禁用Form窗口的最大化按钮发布时间:2022-07-13
下一篇:
C#的Delegate和Event发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap