Example #1
0
        /// <summary>
        /// 读取CIO区地址命令
        /// </summary>
        /// <param name="startAddr"></param>
        /// <param name="readAddrCount"></param>
        /// <param name="cmdBytes"></param>
        /// <param name="restr"></param>
        /// <returns></returns>
        public bool ReadCIOAddrCmd(float startAddr, short readAddrCount, ref byte[] cmdBytes, ref string restr)
        {
            try
            {
                string readDmStr = "";

                string addrStr      = DataConvert.ConvertCIOAddr(startAddr);
                string addrCountStr = Convert.ToString(readAddrCount, 16).PadLeft(4, '0');

                readDmStr = FINSHEADCODE + this.PLCNode + this.PCNode + SID
                            + READORDERTYPE + CIOADDRAREA + addrStr + addrCountStr;

                cmdBytes = DataConvert.StrToToHexByte(readDmStr);

                return(true);
            }
            catch (Exception ex)
            {
                restr = ex.Message;
                return(false);
            }
        }
Example #2
0
 public bool WriteDMAddrCmd(short startAddr, short[] shortValue, ref byte[] writeDmCmdBytes, ref string restr)
 {
     try
     {
         string writeDMStr   = "";
         string addrCountStr = Convert.ToString((short)shortValue.Length, 16).PadLeft(4, '0');
         writeDMStr += FINSHEADCODE + this.PLCNode + this.PCNode + SID + WRITEORDERTYPE + DMADDRAREA
                       + DataConvert.ConvertDMAddr(startAddr) + addrCountStr;
         for (int i = 0; i < shortValue.Length; i++)
         {
             string valueStr = Convert.ToString(shortValue[i], 16).PadLeft(4, '0');
             writeDMStr += valueStr;
         }
         writeDmCmdBytes = DataConvert.StrToToHexByte(writeDMStr);
         return(true);
     }
     catch (Exception ex)
     {
         restr = ex.Message;
         return(false);
     }
 }
Example #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);
            }
        }
Example #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);
            }
        }
Example #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);
            }
        }
Example #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);
                }
            }
        }
Example #7
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);
            }
        }
Example #8
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);
                }
            }
        }