示例#1
0
        public bool init_plc_Connect() //与plc连接的初始化
        {
            bool status;               //判断返回值是否对的标志

            //SocketBase sk = new SocketBase();

            if (SocketBase.initSocketBase())
            {
                SocketBase.SocketSend(plcHead1);
                //Console.ReadKey();
                if (SocketBase.SocketRec().Length == 22)
                {
                    Logger.Infor("handshake first done");
                    SocketBase.SocketSend(plcHead2);
                    byte[] res = SocketBase.SocketRec();
                    if (res.Length == 27)
                    {
                        status = true;
                        Logger.Infor("handshake second done,init ok " + res);
                    }
                    else
                    {
                        status = false;
                        Logger.Error("handshake second failed");
                    }
                }
                else
                {
                    //Console.WriteLine("rec {0}", SocketBase.SocketRec());
                    Logger.Error("handshake first error,plc init failed");
                    status = false;
                }
            }
            else
            {
                status = false;
            }
            return(status);
        }
示例#2
0
        public bool ReadByteData(int DBNode, int ioffset, byte datablockType, int byteCnt, out byte[] receiveData)
        {
            bool isSuccess = false;

            try
            {
                #region 填充读取指令
                ReadStructCmd readStruct = new ReadStructCmd();
                readStruct.ReadCmdInit();
                List <byte> readCmd = new List <byte>();


                readCmd.AddRange(readStruct.headCmd);
                readCmd.AddRange(readStruct.msgLenth);
                readCmd.AddRange(readStruct.fixedCmd1);

                //标识序列号,自定义
                readStruct.serialFlag = new byte[] { 0x00, 0x05 };
                readCmd.AddRange(readStruct.serialFlag);
                readCmd.AddRange(readStruct.fixedCmd2);
                byte[] readCnt = IntToByte(byteCnt);
                if (readCnt.Length < 2)
                {
                    readCmd.Add(0x00);
                }
                readCmd.AddRange(readCnt);
                byte[] DBNo = IntToByte(DBNode);
                if (DBNo.Length < 2)
                {
                    readCmd.Add(0x00);
                }
                readCmd.AddRange(DBNo);

                readCmd.Add(datablockType);

                byte[] offset = IntToByte(ioffset * 8);

                if (offset.Length == 1)
                {
                    readCmd.Add(0x00);
                    readCmd.Add(0x00);
                }
                else if (offset.Length == 2)
                {
                    readCmd.Add(0x00);
                }
                readCmd.AddRange(offset);

                #endregion

                SocketBase.SocketSend(readCmd.ToArray());
                byte[] data = SocketBase.SocketRec();
                int    len  = BitConverter.ToInt32(data.Skip(2).Take(2).ToArray(), 0);
                if (len == 25 + byteCnt) //25+读取长度
                {
                    receiveData = data.Skip(24).Take(data.Length - 24).ToArray();
                    //双字、单字处理???
                    getLittleEndianBytes(receiveData);
                    isSuccess = true;
                }
                else
                {
                    receiveData = new byte[] { };
                    isSuccess   = false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("read byte error", ex);
                receiveData = new byte[] { };
                isSuccess   = false;
            }
            return(isSuccess);
        }
示例#3
0
        /// <summary>
        /// msg区分写入1byte,2byte
        /// </summary>
        /// <param name="wData"></param>
        /// <param name="WriteType"></param>
        /// <param name="DBNode"></param>
        /// <param name="ioffset"></param>
        /// <param name="writeWay"></param>
        /// <param name="datablockType"></param>
        /// <returns></returns>
        public bool writeByteData(byte[] wData, int WriteType, int DBNode, int ioffset, int writeWay, byte datablockType = 0x84)
        {
            bool isSuccess = false;

            try
            {
                //writeStructCmd writeStruct = new writeStructCmd();
                //writeStruct.writeCmdInit();

                writeCmdInit();
                List <byte> writeCmd = new List <byte>();

                byte[] msgLen = IntToByte(35 + wData.Length);
                if (msgLen.Length < 2)
                {
                    writeCmd.Add(0x00);
                }
                writeCmd.AddRange(msgLen);

                writeCmd.AddRange(fixedCmd1);
                writeCmd.AddRange(serialFlag);
                writeCmd.AddRange(fixedCmd2);

                byte[] wLen = IntToByte(wData.Length + 4);
                if (wLen.Length < 2)
                {
                    writeCmd.Add(0x00);
                }
                writeCmd.AddRange(wLen);

                writeCmd.AddRange(fixedCmd3);
                byte wtype = (byte)(WriteType & 0xFF);
                writeCmd.Add(wtype);    //写入方式

                byte[] len = new byte[] { };
                if (WriteType == 1) //bit
                {
                    len = IntToByte(wData.Length * 8);
                }
                else if (WriteType == 2)//byte
                {
                    len = IntToByte(wData.Length);
                }
                if (len.Length < 2)
                {
                    writeCmd.Add(0x00);
                }
                writeCmd.AddRange(len);

                byte[] db = IntToByte(DBNode);
                if (db.Length < 2)
                {
                    writeCmd.Add(0x00);
                }
                writeCmd.AddRange(db);

                writeCmd.Add(datablockType);

                byte[] offset = IntToByte(ioffset * 8);
                if (offset.Length == 1)
                {
                    writeCmd.Add(0x00);
                    writeCmd.Add(0x00);
                }
                else if (offset.Length == 2)
                {
                    writeCmd.Add(0x00);
                }
                writeCmd.AddRange(offset);

                byte[] wWay = IntToByte(writeWay);
                if (wWay.Length < 2)
                {
                    writeCmd.Add(0x00);
                }
                writeCmd.AddRange(wWay);

                byte[] bitCnt = IntToByte(wData.Length * 8);//写入bit的个数(以bit为单位)
                if (wWay.Length < 2)
                {
                    writeCmd.Add(0x00);
                }
                writeCmd.AddRange(bitCnt);

                //双字、单字处理???
                getLittleEndianBytes(wData);

                writeCmd.AddRange(wData);
                //发送数据
                SocketBase.SocketSend(writeCmd.ToArray());
                Thread.Sleep(500);
                byte[] recvData = SocketBase.SocketRec();
                if (recvData[0] == 0x03 && recvData[1] == 0x00 && recvData[recvData.Length - 1] == 0xFF)
                //if(true)
                {
                    Console.WriteLine(recvData);
                    Console.ReadKey();
                    isSuccess = true;
                    Logger.Infor("write ok");
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                Logger.Error("写入plc失败" + ex);
            }
            //Logger.Infor("写入plc成功");
            return(isSuccess);
        }