Esempio n. 1
0
        public ErrorCode setRegsValue(PLCInfo.RegType regType, int nRegNumber, int nWriteNumber, long[] naWriteValues)
        {
            // Example: naWriteValues = [1000, 12000] ==>  "03E8" + "2EE0" = "03E82EE0"
            string strType        = regType.ToString();
            string strWriteNumber = nWriteNumber.ToString("D2");
            string strRegNumber   = nRegNumber.ToString("D5");

            ErrorCode errorCode = ErrorCode.None;
            // long[] naResult = new long[0];

            int nDataLength = 0;

            switch (regType)
            {
            case PLCInfo.RegType.R:
            case PLCInfo.RegType.D:
                nDataLength = 4;
                break;

            case PLCInfo.RegType.DR:
            case PLCInfo.RegType.DD:
                nDataLength = 8;
                break;

            default:
                return(ErrorCode.PLCInfoTypeErr);
            }

            string strWriteValue = ByteArrayToHexString(naWriteValues, nDataLength);

            string strCmd = strWriteNumber + strType + strRegNumber + strWriteValue;

            byte[] baPLCAsciiRes = SendPLC(PLCInfo.FunctionID.WriteReg, strCmd);
            if (baPLCAsciiRes.Length > 0)
            {
                string strUsefulRes = Encoding.ASCII.GetString(baPLCAsciiRes);
                byte   nErrorCode   = byte.Parse(strUsefulRes[0].ToString());
                errorCode = myErrorCode(nErrorCode);
            }
            else
            {
                errorCode = ErrorCode.PLCSocketErr;
            }

            return(errorCode);
        }
Esempio n. 2
0
        public Tuple <byte, long[]> getRegsValue(PLCInfo.RegType regType, int nRegNumber, int nReadNumber)
        {
            string strType       = regType.ToString();
            string strReadNumber = nReadNumber.ToString("D2");
            string strRegNumber  = nRegNumber.ToString("D5");

            string strCmd = strReadNumber + strType + strRegNumber;

            byte[] baPLCAsciiRes = SendPLC(PLCInfo.FunctionID.ReadReg, strCmd);
            string strUsefulRes  = Encoding.ASCII.GetString(baPLCAsciiRes);

            Console.WriteLine("strUsefulRes: {0}, {1}", strUsefulRes, strUsefulRes.Length);

            // Example: R0 = 1000, strUsefulRes= 003E8  DR2 = 10,000,000, strUsefulRes = 000989680
            // Note: The first 0 is error code. so it actually is "03E8" for R0 = 1000.
            byte nErrorCode = byte.Parse(strUsefulRes[0].ToString());

            Console.WriteLine($"nErrorCode: {nErrorCode}");

            byte[] baUsefulRes = new byte[0];
            long[] naResult    = new long[0];
            if (nErrorCode == 0)
            {
                baUsefulRes = hexStringToHexByteArray(strUsefulRes.Substring(1));
                int nDataLength = 0;

                switch (regType)
                {
                case PLCInfo.RegType.R:
                case PLCInfo.RegType.D:
                    nDataLength = 2;
                    break;

                case PLCInfo.RegType.DR:
                case PLCInfo.RegType.DD:
                    nDataLength = 4;
                    break;

                default:
                    return(Tuple.Create(nErrorCode, naResult));
                }

                long[] naTemp = new long[baUsefulRes.Length / nDataLength];
                for (int i = 0; i < baUsefulRes.Length; i += nDataLength)
                {
                    byte[] baTemp = baUsefulRes.Skip(i * nDataLength).Take(nDataLength).ToArray <byte>();

                    // Since Fatek PLC is Little-endian-based, must reverse it first
                    Array.Reverse(baTemp);

                    if (nDataLength == 2)
                    {
                        Int16 nRegValue = BitConverter.ToInt16(baTemp, 0);
                        naTemp[i] = nRegValue;
                    }
                    else
                    {
                        Int32 nRegValue = BitConverter.ToInt32(baTemp, 0);
                        naTemp[i] = nRegValue;
                    }
                }
                naResult = naTemp;
            }

            return(Tuple.Create(nErrorCode, naResult));        // first one is error code, the second rest are useful info.
        }