OKLEController.cs 5.7 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


public class OKLEController
{
    private static int bautRate = 9600;//波特率
    private static Parity parity = Parity.None;//校验位
    private static int dataBits = 8;//数据位
    private static StopBits stopBits = StopBits.One; //停止位
    private static Dictionary<string, AcSerialBean> SerialMap = new Dictionary<string, AcSerialBean>();
    private static Dictionary<string, double> ValueBuffer = new Dictionary<string, double>();
    private static string LogName = "";


    private static AcSerialBean GetSerialBean(string port)
    {
        if (SerialMap.ContainsKey(port))
        {
            return SerialMap[port];
        }
        return null;
    }
    public static void OpenAll(string[] ports)
    {
        foreach (string port in ports)
        {
            Init(port);
        }
    }
    public static bool Init(string port)
    {
        if (SerialMap.ContainsKey(port))
        {
            return true;
        }
        LogName = "[OKLE_" + port + "]";

        if (!ValueBuffer.ContainsKey(port))
            ValueBuffer.Add(port, 0);
        AcSerialBean sb = new AcSerialBean(port, bautRate, parity, dataBits, stopBits);

        try
        {
            if (sb.openPort())
            {
                if (SerialMap.ContainsKey(port))
                {
                    SerialMap.Remove(port);
                }
                SerialMap.Add(port, sb);
                return true;
            }
            else
            {
                LogUtil.error(LogName + "串口打开失败!");
                return false;
            }
        }
        catch (Exception ex)
        {
            LogUtil.error(LogName + "打开出错:" + ex.ToString());
        }
        return true;
    }

    public static bool IsRun(string port)
    {
        if (GetSerialBean(port) != null)
        {
            return true;
        }
        return false;
    }
    /// <summary>
    /// 释放资源
    /// </summary>
    public static void Release(params string[] ports)
    {
        if (ports.Length <= 0)
        {
            ports = (new List<string>(SerialMap.Keys)).ToArray();
        }
        foreach (string port in ports)
        {
            try
            {
                AcSerialBean sb = GetSerialBean(port);
                if (sb != null)
                {
                    sb.closePort();
                }
                //IsRun = false;
                SerialMap.Remove(port);
            }
            catch (Exception ex)
            {
                LogUtil.error("Release OKLE port [" + port + "] error :" + ex.ToString());
            }
        }
    }
    
    public static bool queryData(string port,out double weight)
    {        
        weight = 0d;
        AcSerialBean sb = GetSerialBean(port);
        if (sb == null)
        {
            return false;
        }

        if (!Monitor.TryEnter(sb))
            return false;

        try
        {
            LogName = "[OKLE_" + port + "]";
            //12..1.2读取力值(毛重)指令,指令格式:01 03 00 50 00 02 C4 1A
            //01 03 00 00 00 02 C4 0B
            byte[] sendData = new byte[8];
            sendData[0] = 0x01;
            sendData[1] = 0x03;
            sendData[2] = 0x00;
            sendData[3] = 0x00;
            sendData[4] = 0x00;
            sendData[5] = 0x02;
            sendData[6] = 0xC4;
            sendData[7] = 0x0B;
            //sendData = buildCheckData(sendData, sendData.Length - 2);
            string str = AcSerialBean.byteToHexStr(sendData, " ");
            byte[] reviceData = new byte[15];
            bool isOk = false;
            sb.SendCommand(sendData, ref reviceData, 100, out isOk);
            //LogUtil.debug(LogName + "发送数据:" + str + ",收到数据 :" + AcSerialBean.byteToHexStr(reviceData, " "));
            return getReviceData(reviceData,out weight);
        }
        catch(Exception e) {
            LogUtil.debug(LogName + e.ToString());
            return false;
        }
        finally {
            Monitor.Exit(sb);
        }
    }
    private static bool getReviceData(byte[] dataArray, out double value)
    {
        value = 0d;
        try
        {
            //3D 53 47 2B 30 30 32 30 2E 30 31 6B BE 0D 0A
            //=SG+0020.01k�

            var tempstr = Encoding.ASCII.GetString(dataArray);
            if (tempstr.StartsWith("="))
            {
                var numstr = tempstr.Substring(3, 8);
                if (!double.TryParse(numstr, out value))
                {
                    LogUtil.info(LogName + "转换出错:" + numstr.ToString());
                    return false;
                }
                return true;
            }
            else
            {
                LogUtil.info(LogName + "转换出错:" + tempstr.ToString());
                return false;
            }
        }
        catch (Exception ex)
        {
            LogUtil.info(LogName + "转换出错:" + ex.ToString());
        }
        return false;
    }



    private static byte[] buildCheckData(byte[] sendData, int length)
    {
        ushort pChecksum = 0;
        AcSerialBean.CalculateCRC(sendData, length, out pChecksum);
        string checkStr = Convert.ToString(pChecksum, 16);
        byte[] checkByte = AcSerialBean.StringToByte(checkStr);

        if (checkByte.Length == 1)
        {
            sendData[length] = checkByte[0];
            sendData[length + 1] = 0x00;
        }
        else
        {
            sendData[length + 1] = checkByte[0];
            sendData[length] = checkByte[1];
        }
        return sendData;
    }



}