using System;
using System.Collections.Generic;
using System.Text;
using SimensSerialPort;
using CommonClassLib;
using Microsoft.VisualBasic;
namespace Symbol3204
{
    public class CScan
    {
        //private bool _ifStart = false;
        CSimensSerialPort csp;
        public CScan(short port,string settings)

        {
            try
            {
                csp = new CSimensSerialPort(port, settings);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public bool StartSession()
        {
            //START-SESSION:
            //HOST发04E40400FF14（16进制）       
            //DECODER 发04D00000FF2C（16进制）反馈信息后扫描数据
            //若未正确读取发4E52(16进制)未读反馈信息
            try
            {

                byte[] sd = new byte[6] { 0x04, 0xE4, 0x04, 0x00, 0xFF, 0x14 };
                if (csp.WriteCom(sd) == true)
                {
                    System.Threading.Thread.Sleep(100);
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool StopSession()
        {
            //STOP-SESSION
            //HOST发04E50400FF13（16进制）       
            //DECODER发04D00000FF2C（16进制）反馈信息
            try
            {

                byte[] sd = new byte[6] { 0x04,0xE5,0x04,0x00,0xFF,0x13};

                if (csp.WriteCom(sd) == true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                


            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }
        public string ScanBarCode(short len)
        {
            try
            {
                string strdata = "";

                len = (short)(len + 6);
                byte[] gdata = csp.ReadCom(len);
                if (gdata == null)
                    return null;
                else
                    strdata = Encoding.ASCII.GetString(gdata, 6, 8);
                return strdata;
            }
            catch (Exception ex)
            {
                throw ex;
            }
                 
        }
        public string ScanBarCode()
        {
            try
            {
                string strdata = "";
                strdata = csp.ReadCom();
                return strdata;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }
        public void StopScan()
        {
            csp.CloseCom();
        }
    }
}
