상세 컨텐츠

본문 제목

LG PLC 통신기능 프레임 in Java

Java

by xarfox 2015. 2. 13. 13:18

본문




   byte _ENQ = 0x05; //요청 프레임의 시작코드

    byte _ACK = 0x06; //ack 응답 프레임의 시작코드

    byte _NAK = 0x15; //nak 응답 프레임의 시작코드

    byte _EOT = 0x04; //요구용 프레임 마감 ascii코드

    byte _ETX = 0x03; //응답용 프레임 마감 ascii코드

    




    public byte[] GET_rSS_COMMAND(String plc_addr, String valueLength, String valueName, Boolean bcc )

    {

    String tmpStr = plc_addr + "rSS01" + valueLength + valueName;

    byte[] tmpCommand = tmpStr.getBytes();

   

    String bcc_value;

   

    byte[] rtnValue;

   

   

    if ( bcc )

    rtnValue = new byte[tmpCommand.length + 4];    

    else

    rtnValue = new byte[tmpCommand.length + 2];

   

        rtnValue[0] = _ENQ;

        

        int sidx = 1;

        try

        {

            for (int idx = 0; idx < tmpCommand.length; idx++)

            {

                rtnValue[sidx] = tmpCommand[idx];

                sidx++;

            }

            rtnValue[sidx++] = _EOT;

        }

        catch (Exception err)

        {

        rtnValue = null;

        }

        

        if ( bcc )

        {

        bcc_value = BCC(rtnValue, rtnValue.length - 2);

       

            byte[] tmpBcc = bcc_value.getBytes();

            rtnValue[sidx++] = tmpBcc[0];

            rtnValue[sidx++] = tmpBcc[1];

        }

        

        return rtnValue;

    }

    

    public String BCC(byte[] recvByte, int length)

    {

        byte rtnValue = 0x0;


        int Sum = 0x0;


        for (int idx = 0; idx < length; idx++)

        {

            Sum += recvByte[idx];

        }

        rtnValue = (byte)(Sum);

        

        return String.format("%X", rtnValue);        

    }

    



============================================================================

                byte [] messByte = GET_rSS_COMMAND("20","06","%MW100",true);

                String message = new String(messByte);

관련글 더보기