Ejemplo n.º 1
0
 public static void RTU2ASCII(byte[] nRtu, int Size, byte[] nAscii)
 {
     for (var i = 0; i < Size; i++)
     {
         nAscii[1 + i * 2]     = Num2Ascii(ByteAccess.HI4BITS(nRtu[i]));
         nAscii[1 + i * 2 + 1] = Num2Ascii(ByteAccess.LO4BITS(nRtu[i]));
     }
 }
Ejemplo n.º 2
0
    private Result TxRxAscii(byte[] TXBuf, int QueryLength, byte[] RXBuf, int ResponseLength)
    {
        var num    = 0;
        var array  = new byte[531];
        var array2 = new byte[523];

        Ascii.RTU2ASCII(TXBuf, QueryLength, array);
        var n = Ascii.LRC(TXBuf, QueryLength);

        array[0] = 58;
        array[QueryLength * 2 + 1] = Ascii.Num2Ascii(ByteAccess.HI4BITS(n));
        array[QueryLength * 2 + 2] = Ascii.Num2Ascii(ByteAccess.LO4BITS(n));
        array[QueryLength * 2 + 3] = 13;
        array[QueryLength * 2 + 4] = 10;
        this.port.DiscardInBuffer();
        this.port.DiscardOutBuffer();
        try
        {
            this.port.Write(array, 0, QueryLength * 2 + 5);
        }
        catch (Exception ex)
        {
            this.Error = ex.Message;
            return(Result.WRITE);
        }

        this._txBufSize = QueryLength * 2 + 5;
        Array.Copy(array, this._txBuf, this._txBufSize);
        if (TXBuf[0] == 0)
        {
            return(Result.SUCCESS);
        }
        if (this._removeEcho)
        {
            try
            {
                do
                {
                    var num2 = this.port.Read(RXBuf, 0, this._txBufSize - num);
                    num += num2;
                }while (this._txBufSize - num > 0);
            }
            catch (TimeoutException ex2)
            {
                this.Error = ex2.Message;
                return(Result.RESPONSE_TIMEOUT);
            }
            catch (Exception ex3)
            {
                this.Error = ex3.Message;
                return(Result.READ);
            }

            num = 0;
        }

        try
        {
            do
            {
                var num3 = this.port.Read(array2, num, 11 - num);
                num += num3;
            }while (11 - num > 0);
        }
        catch (TimeoutException ex4)
        {
            this.Error = ex4.Message;
            return(Result.RESPONSE_TIMEOUT);
        }
        catch (Exception ex5)
        {
            this.Error = ex5.Message;
            return(Result.READ);
        }
        finally
        {
            this._rxBufSize = num;
            Array.Copy(array2, this._rxBuf, this._rxBufSize);
        }

        if (Ascii.HiLo4BitsToByte(Ascii.Ascii2Num(array2[3]), Ascii.Ascii2Num(array2[4])) > 128)
        {
            if (!Ascii.VerifyRespLRC(array2, 11))
            {
                return(Result.CRC);
            }
            return((Result)Ascii.HiLo4BitsToByte(Ascii.Ascii2Num(array2[5]), Ascii.Ascii2Num(array2[6])));
        }

        if (ResponseLength == int.MaxValue)
        {
            if (Ascii.HiLo4BitsToByte(Ascii.Ascii2Num(array2[3]), Ascii.Ascii2Num(array2[4])) != 17)
            {
                return(Result.RESPONSE);
            }
            ResponseLength = Ascii.HiLo4BitsToByte(Ascii.Ascii2Num(array2[5]), Ascii.Ascii2Num(array2[6])) + 3;
        }

        try
        {
            var num4 = ResponseLength * 2 + 5;
            do
            {
                var num5 = this.port.Read(array2, num, num4 - num);
                num += num5;
            }while (num4 - num > 0);
        }
        catch (TimeoutException ex6)
        {
            this.Error = ex6.Message;
            return(Result.RESPONSE_TIMEOUT);
        }
        catch (Exception ex7)
        {
            this.Error = ex7.Message;
            return(Result.READ);
        }
        finally
        {
            this._rxBufSize = num;
            Array.Copy(array2, this._rxBuf, this._rxBufSize);
        }

        if (!Ascii.VerifyRespLRC(array2, num))
        {
            return(Result.CRC);
        }
        if (array2[num - 2] != 13 || array2[num - 1] != 10)
        {
            return(Result.RESPONSE);
        }
        var num6 = (num - 5) / 2;

        for (var i = 0; i < num6; i++)
        {
            RXBuf[i] = Ascii.HiLo4BitsToByte(Ascii.Ascii2Num(array2[1 + i * 2]), Ascii.Ascii2Num(array2[2 + i * 2]));
        }
        return(Result.SUCCESS);
    }