示例#1
0
        /// <summary>
        /// 读取DM地址反馈
        /// </summary>
        /// <param name="readDMResponse"></param>
        /// <param name="readAddrCount"></param>
        /// <param name="readValues"></param>
        /// <param name="restr"></param>
        /// <returns></returns>
        public bool ReadDMAddrCmdResponse(byte[] readDMBytes, short readCount, ref short[] values, ref string restr)
        {
            string rdDMStr = DataConvert.ByteToHexStr(readDMBytes);

            //rdDMStr = "46494E530000001A0000000200000000C0000200C000002100000101000011223344";
            if (rdDMStr.Length != 60 + readCount * 4)
            {
                restr = "返回命令长度错误!";
                return(false);
            }

            //比对下地址是否一致,加上返回码0000
            string finsHeader = rdDMStr.Substring(0, 8).ToUpper();

            if (finsHeader != FINSHEAD)
            {
                restr = "FINS协议头错误!";
                return(false);
            }
            string orderType = rdDMStr.Substring(52, 4).ToUpper();

            if (orderType != READORDERTYPE)
            {
                restr = "命令类型错误!";
                return(false);
            }
            string response = rdDMStr.Substring(56, 4).ToUpper();

            if (response != RIGHTRESPONSE)
            {
                restr = "命令返回错误!";
                return(false);
            }
            values = new short[readCount];
            for (int i = 0; i < readCount; i++)
            {
                string hexValue = rdDMStr.Substring(60 + i * 4, 4);
                values[i] = Convert.ToInt16(hexValue, 16);
            }
            return(true);
        }
示例#2
0
        public bool WriteDMAddrCmdResponse(byte[] writeDMResponse, ref string restr)
        {
            try
            {
                string wrDMStr = DataConvert.ByteToHexStr(writeDMResponse);
                //wrDMStr  = "C0000200C0000003000001020000";
                if (wrDMStr.Length != 28)
                {
                    restr = "返回命令长度错误!";
                    return(false);
                }

                //比对下地址是否一致,加上返回码0000
                string finsHeader = wrDMStr.Substring(0, 6).ToUpper();
                if (finsHeader != RESPONSEHEAD)
                {
                    restr = "FINS协议头错误!";
                    return(false);
                }
                string orderType = wrDMStr.Substring(20, 4).ToUpper();
                if (orderType != WRITEORDERTYPE)
                {
                    restr = "命令类型错误!";
                    return(false);
                }
                string response = wrDMStr.Substring(24, 4).ToUpper();
                if (response != RIGHTRESPONSE)
                {
                    restr = "命令返回错误!";
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
示例#3
0
        public bool HandCmdResponse(byte[] hanCmdResponse, ref string restr)
        {
            try
            {
                string wrDMStr = DataConvert.ByteToHexStr(hanCmdResponse);

                if (wrDMStr.Length != 48)
                {
                    restr = "返回命令长度错误!";
                    return(false);
                }

                //比对下地址是否一致,加上返回码0000
                string finsHeader = wrDMStr.Substring(0, 8).ToUpper();
                if (finsHeader != FINSHEAD)
                {
                    restr = "FINS协议头错误!";
                    return(false);
                }
                string plcIP = wrDMStr.Substring(40, 8).ToUpper();

                int recPlcNode = Convert.ToInt32(plcIP, 16);
                if (this.PLCNodeNum != recPlcNode)
                {
                    restr = "IP地址错误!";
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
示例#4
0
        public bool WriteMultiDB(string addr, int blockNum, short[] vals)
        {
            lock (lockObj)
            {
                //int reworkNum = 0;
                //EnumAddrArea addrArea = EnumAddrArea.DM;
                //string restr = "";
                //bool isAddr = AddrCheck(addr, ref addrArea);
                //byte[] cmdBytes = null;
                //if (isAddr == false)
                //{
                //    return false;
                //}

                string addrArea = addr.Substring(0, 1).ToUpper();
                addr = addr.Substring(1, addr.Length - 1);
                string restr     = "";
                byte[] cmdBytes  = null;
                int    reworkNum = 0;
                if (addrArea == EnumAddrArea.D.ToString())
                {
                    //获取读取地址指令
                    bool readCmd = this.omlFinsPtl.WriteDMAddrCmd(short.Parse(addr), vals, ref cmdBytes, ref restr);
                    if (readCmd == false)
                    {
                        return(false);
                    }
                    this.recBuffer.Clear();
                    bool sendStatus = this.mySocket.Send(cmdBytes, ref restr);

                    if (sendStatus == false)
                    {
                        return(false);
                    }
                    this.OnLog("协议:" + DataConvert.ByteToHexStr(cmdBytes));
                    this.recvAutoEvent.Reset();
                    if (this.recvAutoEvent.WaitOne(TIMEWAITOUT) == true)
                    {
                        while (reworkNum < MAXREWORKNUM)
                        {
                            if (this.omlFinsPtl.WriteDMAddrCmdResponse(this.recBuffer.ToArray(), ref restr) == false)
                            {
                                reworkNum++;
                                OnLog("接收反馈数据:" + DataConvert.ByteToHexStr(this.recBuffer.ToArray()));
                                System.Threading.Thread.Sleep(WAITTIME);
                            }
                            else
                            {
                                return(true);
                            }
                        }
                        if (reworkNum >= MAXREWORKNUM)
                        {
                            OnLog("写入数据反馈数据错误!");
                            return(false);
                        }
                    }
                    else
                    {
                        OnLog("写入超时!");
                        return(false);
                    }
                    //指令发出后要等待接收可用信号
                }
                else if (addrArea == EnumAddrArea.W.ToString())
                {
                    //获取读取地址指令

                    bool readCmd = this.omlFinsPtl.WriteCIOAddrCmd(float.Parse(addr), vals, ref cmdBytes, ref restr);
                    if (readCmd == false)
                    {
                        return(false);
                    }
                    this.recBuffer.Clear();
                    bool sendStatus = this.mySocket.Send(cmdBytes, ref restr);
                    if (sendStatus == false)
                    {
                        return(false);
                    }
                    this.OnLog("协议:" + DataConvert.ByteToHexStr(cmdBytes));
                    this.recvAutoEvent.Reset();
                    if (this.recvAutoEvent.WaitOne(TIMEWAITOUT) == true)
                    {
                        while (reworkNum < MAXREWORKNUM)
                        {
                            if (this.omlFinsPtl.WriteDMAddrCmdResponse(this.recBuffer.ToArray(), ref restr) == false)
                            {
                                reworkNum++;
                                OnLog("接收反馈数据:" + DataConvert.ByteToHexStr(this.recBuffer.ToArray()));
                                System.Threading.Thread.Sleep(WAITTIME);
                            }
                            else
                            {
                                return(true);
                            }
                        }
                        if (reworkNum >= MAXREWORKNUM)
                        {
                            OnLog("写入数据反馈数据错误!");
                            return(false);
                        }
                    }
                    else
                    {
                        OnLog("接收超时!");
                        return(false);
                    }
                }
                return(true);
            }
        }
示例#5
0
        public bool ReadMultiDB(string addr, int blockNum, ref short[] vals)
        {
            lock (lockObj)
            {
                if (addr.Length < 1)
                {
                    return(false);
                }
                string addrArea = addr.Substring(0, 1).ToUpper();
                addr = addr.Substring(1, addr.Length - 1);
                string restr     = "";
                byte[] cmdBytes  = null;
                int    reworkNum = 0;
                if (addrArea == EnumAddrArea.D.ToString())
                {
                    //获取读取地址指令
                    bool readCmd = this.omlFinsPtl.ReadDMAddrCmd(short.Parse(addr), (short)blockNum, ref cmdBytes, ref restr);
                    if (readCmd == false)
                    {
                        return(false);
                    }
                    this.recBuffer.Clear();
                    bool sendStatus = this.mySocket.Send(cmdBytes, ref restr);

                    if (sendStatus == false)
                    {
                        return(false);
                    }
                    this.OnLog("协议:" + DataConvert.ByteToHexStr(cmdBytes));
                    this.recvAutoEvent.Reset();
                    if (this.recvAutoEvent.WaitOne(TIMEWAITOUT) == true)
                    {
                        while (reworkNum < MAXREWORKNUM)
                        {
                            if (this.omlFinsPtl.ReadDMAddrCmdResponse(this.recBuffer.ToArray(), (short)blockNum, ref vals, ref restr) == false)
                            {
                                reworkNum++;
                                OnLog("接收反馈数据:" + DataConvert.ByteToHexStr(this.recBuffer.ToArray()));
                                System.Threading.Thread.Sleep(WAITTIME);
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (reworkNum >= MAXREWORKNUM)
                        {
                            OnLog("写入数据反馈数据错误!");
                            return(false);
                        }
                        return(true);
                    }
                    else
                    {
                        OnLog("读取超时!");
                        //if(!PingIp(this.PLCIP) )//在外部控制断开连接
                        //{
                        //    Console.WriteLine("PLC:{0}网络不通,将尝试重连", this.PLCIP);
                        //}
                        //string reStr = "";
                        //if(!ConnectPLC(ref reStr))
                        //{
                        //    Console.WriteLine("连接PLC{0}失败", this.PLCIP);
                        //}
                        //else
                        //{
                        //    Console.WriteLine("连接PLC{0}成功", this.PLCIP);
                        //}
                        return(false);
                    }
                    //指令发出后要等待接收可用信号
                }
                else if (addrArea == EnumAddrArea.W.ToString())
                {
                    //获取读取地址指令
                    bool readCmd = this.omlFinsPtl.ReadCIOAddrCmd(float.Parse(addr), (short)blockNum, ref cmdBytes, ref restr);
                    if (readCmd == false)
                    {
                        return(false);
                    }
                    this.recBuffer.Clear();
                    bool sendStatus = this.mySocket.Send(cmdBytes, ref restr);

                    if (sendStatus == false)
                    {
                        return(false);
                    }
                    this.OnLog("协议:" + DataConvert.ByteToHexStr(cmdBytes));
                    this.recvAutoEvent.Reset();
                    if (this.recvAutoEvent.WaitOne(TIMEWAITOUT) == true)
                    {
                        while (reworkNum < MAXREWORKNUM)
                        {
                            if (this.omlFinsPtl.ReadCIOAddrCmdResponse(this.recBuffer.ToArray(), (short)blockNum, ref vals, ref restr) == false)
                            {
                                reworkNum++;
                                OnLog("接收反馈数据:" + DataConvert.ByteToHexStr(this.recBuffer.ToArray()));
                                System.Threading.Thread.Sleep(WAITTIME);
                            }
                            else
                            {
                                return(true);
                            }
                        }
                        if (reworkNum >= MAXREWORKNUM)
                        {
                            OnLog("写入数据反馈数据错误!");
                            return(false);
                        }
                    }
                    else
                    {
                        OnLog("读取超时!");
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
                return(true);
            }
        }
示例#6
0
        /// <summary>
        /// 连接PLC
        /// </summary>
        /// <param name="plcAddr">plc地址</param>
        /// <param name="reStr"></param>
        /// <returns></returns>
        public bool ConnectPLC(ref string reStr)
        {
            lock (lockObj)
            {
                int reworkNum = 0;
                if (this.mySocket == null)
                {
                    reStr = "通讯对象为空!连接失败!";
                    return(false);
                }
                if (this.PlcType == EnumPLCType.OML_TCP)
                {
                    bool connStatus = this.mySocket.Connect(this.PLCIP, this.PLCPort, ref reStr);
                    if (connStatus == false)
                    {
                        return(false);
                    }
                    byte[] handCmd = null;
                    //发送fins握手协议
                    bool handCmdSta = this.omlFinsPtl.HandCmd(ref handCmd, ref reStr);
                    if (handCmdSta == false)
                    {
                        reStr += ":获取握手协议失败!";

                        return(false);
                    }
                    this.OnLog("发送握手协议:" + DataConvert.ByteToHexStr(handCmd));
                    this.recBuffer.Clear();//每次发送前要清空缓存
                    bool sendSta = this.mySocket.Send(handCmd, ref reStr);
                    if (sendSta == false)
                    {
                        reStr += ":数据发送失败!";
                        return(false);
                    }
                    if (this.recvAutoEvent.WaitOne(TIMEWAITOUT) == true)
                    {
                        while (reworkNum < MAXREWORKNUM)
                        {
                            if (this.omlFinsPtl.HandCmdResponse(this.recBuffer.ToArray(), ref reStr) == false)
                            {
                                reworkNum++;
                                OnLog("接收握手数据:" + DataConvert.ByteToHexStr(this.recBuffer.ToArray()));
                                System.Threading.Thread.Sleep(WAITTIME);
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (reworkNum >= MAXREWORKNUM)
                        {
                            OnLog("握手协议反馈数据错误!");
                            return(false);
                        }
                        return(true);
                    }
                    else
                    {
                        reStr = "FINS 握手反馈超时!";
                        return(false);
                    }
                }
                else if (this.PlcType == EnumPLCType.OML_UDP)
                {
                    bool connStatus = this.mySocket.Connect(this.PLCIP, this.PLCPort, ref reStr);//不需要握手了
                    if (connStatus == false)
                    {
                        return(false);
                    }
                    reStr = "连接成功!";
                    return(true);
                }
                else
                {
                    reStr = "plc类型错误!";
                    return(false);
                }
            }
        }
示例#7
0
        public bool WriteMultiDB(string addr, int blockNum, int[] vals)
        {
            try
            {
                lock (lockObj)
                {
                    string addrArea = addr.Substring(0, 1).ToUpper();
                    addr = addr.Substring(1, addr.Length - 1);
                    string restr     = "";
                    byte[] cmdBytes  = null;
                    int    reworkNum = 0;
                    if (addrArea == EnumAddrArea.D.ToString())
                    {
                        //获取读取地址指令
                        bool readCmd = this.omlFinsPtl.WriteDMAddrCmd(short.Parse(addr), vals, ref cmdBytes, ref restr);
                        if (readCmd == false)
                        {
                            return(false);
                        }
                        this.recBuffer.Clear();
                        bool sendStatus = this.mySocket.Send(cmdBytes, ref restr);

                        if (sendStatus == false)
                        {
                            return(false);
                        }
                        this.OnLog("协议:" + DataConvert.ByteToHexStr(cmdBytes));
                        this.recvAutoEvent.Reset();
                        if (this.recvAutoEvent.WaitOne(TIMEWAITOUT) == true)
                        {
                            while (reworkNum < MAXREWORKNUM)
                            {
                                if (this.omlFinsPtl.WriteDMAddrCmdResponse(this.recBuffer.ToArray(), ref restr) == false)
                                {
                                    reworkNum++;
                                    OnLog("接收反馈数据:" + DataConvert.ByteToHexStr(this.recBuffer.ToArray()));
                                    System.Threading.Thread.Sleep(WAITTIME);
                                }
                                else
                                {
                                    return(true);
                                }
                            }
                            if (reworkNum >= MAXREWORKNUM)
                            {
                                OnLog("写入数据反馈数据错误!");
                                return(false);
                            }
                        }
                        else
                        {
                            OnLog("写入超时!");
                            return(false);
                        }
                        //指令发出后要等待接收可用信号
                    }
                    else if (addrArea == EnumAddrArea.W.ToString())
                    {
                        return(true);
                        //暂未实现
                    }
                    else
                    {
                        return(false);
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("底层PLC扩展写接口错误:" + ex.Message);
                return(false);
            }
        }
示例#8
0
        public string ReadUID()//读电子标签的label ID
        {
            string barCode = "";

            try
            {
                lock (lockObj)
                {
                    string restr = "";
                    if (this.mySocket.IsConnected == false)
                    {
                        Disconnect();
                        Connect();
                    }

                    if (Request() == false)
                    {
                        return(string.Empty);
                    }
                    byte[] cmdBytes  = GetAnticollCmd().ToArray();
                    int    reworkNum = 0;
                    this.recBuffer.Clear();
                    bool sendStatus = this.mySocket.Send(cmdBytes, ref restr);

                    if (sendStatus == false)
                    {
                        return(string.Empty);
                    }
                    //this.OnLog("协议:" + DataConvert.ByteToHexStr(cmdBytes));
                    this.recvAutoEvent.Reset();
                    if (this.recvAutoEvent.WaitOne(TIMEWAITOUT) == true)
                    {
                        while (reworkNum < MAXREWORKNUM)
                        {
                            if (CheckAnticollCmd(this.recBuffer, ref barCode) == false)
                            {
                                reworkNum++;
                                OnLog("读卡接受" + reworkNum + "次,接收反馈数据:" + DataConvert.ByteToHexStr(this.recBuffer.ToArray()));
                                System.Threading.Thread.Sleep(WAITTIME);
                            }
                            else
                            {
                                OnLog("读卡成功!");
                                return(barCode);
                            }
                        }
                        if (reworkNum >= MAXREWORKNUM)
                        {
                            OnLog("读卡超时,写入请求读卡反馈数据错误!");
                            return(string.Empty);
                        }
                    }
                    else
                    {
                        OnLog("读卡超时!");
                        return(string.Empty);
                    }
                }
                return(barCode);
            }
            catch
            {
                Disconnect();
                Connect();
                return(string.Empty);
            }
        }
示例#9
0
        public string ReadBarcode()
        {
            lock (this.lockObj)
            {
                try
                {
                    string reStr     = "";
                    string barCode   = "";
                    int    reworkNum = 0;
                    this.recBuffer.Clear();
                    byte[] readCodeCmd       = null;
                    bool   readCodeCmdStatus = sr750Dev.ReadCodeCmd(ref readCodeCmd);
                    if (readCodeCmdStatus == false)
                    {
                        return(string.Empty);
                    }
                    this.recvAutoEvent.Reset();
                    if (this.mySocket.Send(readCodeCmd, ref reStr) == false)
                    {
                        ShowLog("发送读取条码命令失败!");
                    }
                    ShowLog("发送读取条码命令:" + DataConvert.ByteToHexStr(readCodeCmd));

                    if (this.recvAutoEvent.WaitOne(TIMEWAITOUT) == true)
                    {
                        while (reworkNum < MAXREWORKNUM)
                        {
                            if (this.sr750Dev.ReadCodeCmdReponse(this.recBuffer.ToArray(), ref barCode, ref reStr) == false)
                            {
                                reworkNum++;
                                ShowLog("接收条码数据数据:" + DataConvert.AsciiBytesToHex(this.recBuffer.ToArray()));
                                System.Threading.Thread.Sleep(WAITTIME);
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (reworkNum >= MAXREWORKNUM)
                        {
                            ShowLog("接收条码数据数据错误!");
                            return(string.Empty);
                        }
                        CloseScan();
                        return(barCode);
                    }
                    else
                    {
                        CloseScan();
                        reStr = "接收条码数据超时!";
                        return(string.Empty);
                    }
                }
                catch (Exception ex)
                {
                    CloseScan();
                    Console.WriteLine(ex.StackTrace.ToString());
                    return(string.Empty);
                }
            }
        }