国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C#駿鵬自動(dòng)售貨機(jī)接口

2019-11-17 02:13:50
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

C#駿鵬自動(dòng)售貨機(jī)接口

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ù)已通過(guò)驗(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ù)總長(zhǎng)度            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    }}
View Code

第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    {
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临汾市| 呼图壁县| 双江| 凤庆县| 宁波市| 唐海县| 唐山市| 盐源县| 深泽县| 连山| 库尔勒市| 改则县| 耒阳市| 于都县| 浦城县| 北安市| 黄骅市| 沙坪坝区| 图片| 海丰县| 新河县| 淮滨县| 平乐县| 海原县| 繁峙县| 瑞昌市| 东平县| 凤庆县| 全州县| 饶河县| 公主岭市| 安溪县| 福鼎市| 基隆市| 唐海县| 济阳县| 彭山县| 普安县| 海晏县| 武汉市| 师宗县|