Exemplo n.º 1
0
        public DeltaReturnedData SendLongSizeRequest(DeltaMsgType bCommand, DeltaMemType bType, int iAddress, int iSize)
        {
            // Size is bigger then allowed - we have to send few messages
            DeltaReturnedData Result = new DeltaReturnedData();

            Result.IntValue = new int[iSize];
            int TmpSize = iSize;

            while (TmpSize > MAX_16BIT_SIZE)
            {
                DeltaReturnedData Incoming = this.SendNewMessage(bCommand, bType, (iAddress + iSize - TmpSize), MAX_16BIT_SIZE);
                if (Incoming.IntValue == null)
                {
                    return(Result);
                }
                Array.Copy(Incoming.IntValue, 0, Result.IntValue, iSize - TmpSize, MAX_16BIT_SIZE);
                TmpSize -= MAX_16BIT_SIZE;
            }
            if (TmpSize > 0)
            {
                DeltaReturnedData Incoming = this.SendNewMessage(bCommand, bType, (byte)(iAddress + iSize - TmpSize), (byte)TmpSize);
                Array.Copy(Incoming.IntValue, 0, Result.IntValue, iSize - TmpSize, TmpSize);
            }
            return(Result);
        }
Exemplo n.º 2
0
        public DeltaReturnedData SendNewMessage(DeltaMsgType bCommand, DeltaMemType bType, int iAddress, byte bSize, List <Int16> oDataArrayToWrite = null)
        {
            byte              bcheckSum;
            string            stSendBuf;
            DeltaReturnedData Incoming = new DeltaReturnedData();

            bcheckSum = 0;
            stSendBuf = ":01";
            bcheckSum = 1; //Hex

            //kind of command
            switch (bCommand)
            {
            case DeltaMsgType.ReadCoilStatus:
                bcheckSum += (byte)DeltaMsgType.ReadCoilStatus;
                stSendBuf += Convert.ToString((byte)DeltaMsgType.ReadCoilStatus, 16).PadLeft(2, '0');
                break;

            case DeltaMsgType.ReadInputStatus:
                bcheckSum += (byte)DeltaMsgType.ReadInputStatus;
                stSendBuf += Convert.ToString((byte)DeltaMsgType.ReadInputStatus, 16).PadLeft(2, '0');
                break;

            case DeltaMsgType.ReadHoldingRegisters:
                bcheckSum += (byte)DeltaMsgType.ReadHoldingRegisters;
                stSendBuf += Convert.ToString((byte)DeltaMsgType.ReadHoldingRegisters, 16).PadLeft(2, '0');
                break;

            case DeltaMsgType.ForceSingleCoil:
                bcheckSum += (byte)DeltaMsgType.ForceSingleCoil;
                stSendBuf += Convert.ToString((byte)DeltaMsgType.ForceSingleCoil, 16).PadLeft(2, '0');
                break;

            case DeltaMsgType.PresetSingleRegister:
                bcheckSum += (byte)DeltaMsgType.PresetSingleRegister;
                stSendBuf += Convert.ToString((byte)DeltaMsgType.PresetSingleRegister, 16).PadLeft(2, '0');
                break;

            case DeltaMsgType.ForceMultipleCoils:
                bcheckSum += (byte)DeltaMsgType.ForceMultipleCoils;
                stSendBuf += Convert.ToString((byte)DeltaMsgType.ForceMultipleCoils, 16).PadLeft(2, '0');
                break;

            case DeltaMsgType.PresetMultipleRegister:
                bcheckSum += (byte)DeltaMsgType.PresetMultipleRegister;
                stSendBuf += Convert.ToString((byte)DeltaMsgType.PresetMultipleRegister, 16).PadLeft(2, '0');
                break;

            case DeltaMsgType.ReportSlaveID:
                bcheckSum += (byte)DeltaMsgType.ReportSlaveID;
                stSendBuf += Convert.ToString((byte)DeltaMsgType.ReportSlaveID, 16).PadLeft(2, '0');
                break;
            }

            //hi and lo address
            switch (bType)
            {
            case DeltaMemType.C:
                //Hibyte
                stSendBuf += Convert.ToString(((iAddress + C_OFFSET) / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + C_OFFSET) / 256);
                //Lowbyte
                stSendBuf += Convert.ToString(((iAddress + C_OFFSET) & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + C_OFFSET) & 255);
                break;

            case DeltaMemType.X:
                //Hibyte
                stSendBuf += Convert.ToString(((iAddress + X_OFFSET) / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + X_OFFSET) / 256);
                //Lowbyte
                stSendBuf += Convert.ToString(((iAddress + X_OFFSET) & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + X_OFFSET) & 255);
                break;

            case DeltaMemType.Y:
                //Hibyte
                stSendBuf += Convert.ToString(((iAddress + Y_OFFSET) / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + Y_OFFSET) / 256);
                //Lowbyte
                stSendBuf += Convert.ToString(((iAddress + Y_OFFSET) & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + Y_OFFSET) & 255);
                break;

            case DeltaMemType.T:
                //Hibyte
                stSendBuf += Convert.ToString(((iAddress + T_OFFSET) / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + T_OFFSET) / 256);
                //Lowbyte
                stSendBuf += Convert.ToString(((iAddress + T_OFFSET) & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + T_OFFSET) & 255);
                break;

            case DeltaMemType.M:
                //Hibyte
                stSendBuf += Convert.ToString(((iAddress + M_OFFSET) / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + M_OFFSET) / 256);
                //Lowbyte
                stSendBuf += Convert.ToString(((iAddress + M_OFFSET) & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + M_OFFSET) & 255);
                break;

            case DeltaMemType.D:
                //Hibyte
                stSendBuf += Convert.ToString(((iAddress + D_OFFSET) / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + D_OFFSET) / 256);
                //Lowbyte
                stSendBuf += Convert.ToString(((iAddress + D_OFFSET) & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)((iAddress + D_OFFSET) & 255);
                break;
            }

            switch (bCommand)
            {
            case DeltaMsgType.ReadInputStatus:
                //Hibyte Size
                stSendBuf += Convert.ToString((bSize / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)(bSize / 256);
                //Lowbyte Size
                stSendBuf += Convert.ToString(((bSize) & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)((bSize) & 255);
                break;

            case DeltaMsgType.ReadHoldingRegisters:
                //Hibyte Size
                stSendBuf += Convert.ToString((bSize / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)(bSize / 256);
                //Lowbyte Size
                stSendBuf += Convert.ToString((bSize & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)(bSize & 255);
                break;

            case DeltaMsgType.PresetSingleRegister:
                //Hibyte Size
                stSendBuf += Convert.ToString((oDataArrayToWrite[0] / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)(oDataArrayToWrite[0] / 256);
                //Lowbyte Size
                stSendBuf += Convert.ToString((oDataArrayToWrite[0] & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)(oDataArrayToWrite[0] & 255);
                break;

            case DeltaMsgType.PresetMultipleRegister:
                //Hibyte Size
                int registersNum = bSize;
                bSize += bSize;

                stSendBuf += Convert.ToString((registersNum / 256), 16).PadLeft(2, '0');
                bcheckSum += (byte)(registersNum / 256);
                //Lowbyte Size
                stSendBuf += Convert.ToString((registersNum & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)(registersNum & 255);

                stSendBuf += Convert.ToString((bSize & 255), 16).PadLeft(2, '0');
                bcheckSum += (byte)(bSize & 255);


                for (int i = 0; i < oDataArrayToWrite.Count; i++)
                {
                    stSendBuf += Convert.ToString((oDataArrayToWrite[i] / 256), 16).PadLeft(2, '0');
                    bcheckSum += (byte)(oDataArrayToWrite[i] / 256);
                    //Lowbyte Size
                    stSendBuf += Convert.ToString((oDataArrayToWrite[i] & 255), 16).PadLeft(2, '0');
                    bcheckSum += (byte)(oDataArrayToWrite[i] & 255);
                }

                break;

            case DeltaMsgType.ForceSingleCoil:
                if (bSize == 0)     //false
                {
                    //Hibyte Size
                    stSendBuf += "0000";
                }
                else        //true
                {
                    stSendBuf += "FF00";
                    bcheckSum += 0xFF;
                }
                break;
            }

            stSendBuf += Convert.ToString((0x100 - bcheckSum), 16).PadLeft(2, '0');
            stSendBuf  = stSendBuf.ToUpper();

            RxMsg = SendAndRecieveData(stSendBuf);

#if  DELTA_14SS
#else       //DELTA_12SA2
            RxMsg = ":0" + RxMsg;
#endif

            if (RxMsg == "")
            {
                incomingInfo.listDebugInfo.Add("No Response From unit -Check The Connection");
                return(Incoming);
            }
            // exit method if message is not valid
            //System.Diagnostics.Debug.Print(stSendBuf);
            //System.Diagnostics.Debug.Print(RxMsg);
            if (!CheckValidMessage(RxMsg))
            {
                return(Incoming);
            }

            //kind of command
            switch (bCommand)
            {
            case DeltaMsgType.ReadInputStatus:
                Incoming = ParseReceivedBuf(RxMsg, bSize, 1);
                break;

            case DeltaMsgType.ReadCoilStatus:
                Incoming = ParseReceivedBuf(RxMsg, bSize, 1);
                break;

            case DeltaMsgType.ReadHoldingRegisters:
                Incoming = ParseReceivedBuf(RxMsg, bSize, 0);
                newPressureTableReceive = true;
                break;

            case DeltaMsgType.ForceSingleCoil:
                if (!stSendBuf.Equals(RxMsg.Substring(0, RxMsg.Length - 1)))
                {
                    incomingInfo.listDebugInfo.Add("Received message is different then expected");
                }
                break;

            case DeltaMsgType.PresetSingleRegister:

                break;

            case DeltaMsgType.ForceMultipleCoils:

                break;

            case DeltaMsgType.PresetMultipleRegister:

                break;

            case DeltaMsgType.ReportSlaveID:

                break;
            }

            return(Incoming);
        }