Beispiel #1
0
        private void thRequestData()
        {
            while (true)
            {
                AsyncMasterFrame ao = new AsyncMasterFrame(12);

                //헤더구성
                ao.buffer[0] = 0x00;
                ao.buffer[1] = 0x01;
                ao.buffer[2] = 0x00;
                ao.buffer[3] = 0x00; //여기서 TCP RTU ASCII 구분
                ao.buffer[4] = 0x00;
                ao.buffer[5] = 0x06; //프레임길이

                //프레임구성

                ao.buffer[6] = 0x01; //slaveid
                ao.buffer[7] = 0x03; //functioncode

                int startaddr = Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value);
                ao.buffer[8] = Convert.ToByte(startaddr / 256);
                ao.buffer[9] = Convert.ToByte(startaddr % 256);

                int datacount = dataGridView1.Rows.Count;

                ao.buffer[10] = Convert.ToByte(datacount / 256);
                ao.buffer[11] = Convert.ToByte(datacount % 256);

                ao.workSocket = modbusMaster;
                modbusMaster.BeginSend(ao.buffer, 0, ao.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
                Thread.Sleep(1000);
            }
        }
Beispiel #2
0
        private void AsyncHandleMasterConnect(IAsyncResult ar)
        {
            Socket master = (Socket)ar.AsyncState;

            master.EndConnect(ar);

            if (master.Connected)
            {
                AsyncMasterFrame ao = new AsyncMasterFrame(1024);
                ao.workSocket = master;

                threadRequestData = new Thread(thRequestData);
                threadRequestData.Start();

                master.BeginReceive(ao.buffer, 0, ao.buffer.Length, SocketFlags.None, asyncCbRespond, ao);

                labelState.Text = "Connect";
            }
        }
Beispiel #3
0
        public void AsyncMultiRegister(string slaveid, string addr, string quantity, int[] data)
        {
            if(slaveid == "" && addr == "" && quantity == "")
            {
                Write_0x10.toform1 -= new Write_0x10.AsyncMultiRegister(AsyncMultiRegister);
                return;
            }

            int hdframelength = 13 + data.Length*2;
            AsyncMasterFrame ao = new AsyncMasterFrame(hdframelength);

            //헤더구성
            ao.buffer[0] = 0x00;
            ao.buffer[1] = 0x01;
            ao.buffer[2] = 0x00;
            ao.buffer[3] = 0x00; //여기서 TCP RTU ASCII 구분
            ao.buffer[4] = 0x00;
            ao.buffer[5] = Convert.ToByte(7 + data.Length*2); //프레임길이

            //프레임구성

            ao.buffer[6] = 0x01; //slaveid
            ao.buffer[7] = 0x10; //functioncode

            int startaddr = Convert.ToInt32(addr);
            ao.buffer[8] = Convert.ToByte(startaddr / 256);
            ao.buffer[9] = Convert.ToByte(startaddr % 256);

            int datacount = Convert.ToInt32(quantity);

            ao.buffer[10] = Convert.ToByte(datacount / 256);
            ao.buffer[11] = Convert.ToByte(datacount % 256);

            ao.buffer[12] = Convert.ToByte(data.Length * 2); //데이터 바이트 수

            for (int i = 0; i < data.Length;i++)
            {
                int j = i * 2;
                ao.buffer[13 + j] = Convert.ToByte(data[i] / 256);
                ao.buffer[14 + j] = Convert.ToByte(data[i] % 256);
            }

            ao.workSocket = modbusMaster;
            modbusMaster.BeginSend(ao.buffer, 0, ao.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
        }
Beispiel #4
0
        public void AsyncEditValue(string slaveid, string address, string value)
        {
            if(labelState.Text == "Connected")
            {
                AsyncMasterFrame ao = new AsyncMasterFrame(12);

                //헤더구성
                ao.buffer[0] = 0x00;
                ao.buffer[1] = 0x01;
                ao.buffer[2] = 0x00;
                ao.buffer[3] = 0x00; //여기서 TCP RTU ASCII 구분
                ao.buffer[4] = 0x00;
                ao.buffer[5] = 0x06; //프레임길이

                //프레임구성
                if (slaveid == "" && address == "" && value == "")
                {
                    Write_0x06.toform1 -= new Write_0x06.AsyncEditValue(AsyncEditValue);
                    return;
                }

                ao.buffer[6] = Convert.ToByte(slaveid); //slaveid
                ao.buffer[7] = 0x06; //functioncode

                int startaddr = Convert.ToInt32(address);
                ao.buffer[8] = Convert.ToByte(startaddr / 256);
                ao.buffer[9] = Convert.ToByte(startaddr % 256);

                int data = Convert.ToInt32(value);

                ao.buffer[10] = Convert.ToByte(data / 256);
                ao.buffer[11] = Convert.ToByte(data % 256);

                ao.workSocket = modbusMaster;
                modbusMaster.BeginSend(ao.buffer, 0, ao.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
            }
            else
            {
                return;
            }
        }
Beispiel #5
0
        private void AsyncHandleRespond(IAsyncResult ar)
        {
            AsyncMasterFrame ao = (AsyncMasterFrame)ar.AsyncState;
            Int32 recvBytes = ao.workSocket.EndReceive(ar);
            if(recvBytes > 0)
            {
                //프레임분석 후 다시 전송
                //header
                byte[] ti = new byte[2];
                ti[0] = ao.buffer[0];
                ti[1] = ao.buffer[1];
                byte[] pi = new byte[2];
                pi[0] = ao.buffer[2];
                pi[1] = ao.buffer[3];
                byte[] length = new byte[2];
                length[0] = ao.buffer[4];
                length[1] = ao.buffer[5];
                ////////////////////////

                //frame
                int nlength = 256 * ao.buffer[4] + ao.buffer[5];
                byte slaveid = ao.buffer[6];
                byte functioncode = ao.buffer[7];

                if(functioncode == 0x03)
                {
                    byte[] startaddr = new byte[2];
                    startaddr[0] = ao.buffer[8];
                    startaddr[1] = ao.buffer[9];

                    int nstartaddr = 256 * startaddr[0] + startaddr[1];

                    byte[] datacount = new byte[2];
                    datacount[0] = ao.buffer[10];
                    datacount[1] = ao.buffer[11];

                    int ndatacount = 256 * datacount[0] + datacount[1];

                    //조건이 일치할 경우 true값을 보냄
                    if ((nstartaddr == Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value)) && (ndatacount == Convert.ToInt32(dataGridView1.Rows.Count)))
                    {
                        int headerframelength = 6 + 3 + (ndatacount * 2);
                        int framelength = headerframelength - 6;
                        AsyncMasterFrame ao2 = new AsyncMasterFrame(headerframelength);

                        ao2.buffer[0] = ao.buffer[0];
                        ao2.buffer[1] = ao.buffer[1];
                        ao2.buffer[2] = ao.buffer[2];
                        ao2.buffer[3] = ao.buffer[3];
                        ao2.buffer[4] = Convert.ToByte(framelength / 256);
                        ao2.buffer[5] = Convert.ToByte(framelength % 256);

                        ao2.buffer[6] = ao.buffer[6];
                        ao2.buffer[7] = ao.buffer[7];
                        ao2.buffer[8] = Convert.ToByte(ndatacount * 2);
                        for (int i = 0; i < ndatacount; i++)
                        {
                            int j = i * 2;
                            ao2.buffer[j + 9] = Convert.ToByte(Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value) / 256);
                            ao2.buffer[j + 10] = Convert.ToByte(Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value) % 256);
                        }
                        ao2.workSocket = ModbusSlave;
                        ModbusSlave.BeginSend(ao2.buffer, 0, ao2.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
                    }
                    else //조건 불일치일 경우 82 에러코드 전송
                    {
                        AsyncMasterFrame ao2 = new AsyncMasterFrame(9);

                        ao2.buffer[0] = ao.buffer[0];
                        ao2.buffer[1] = ao.buffer[1];
                        ao2.buffer[2] = ao.buffer[2];
                        ao2.buffer[3] = ao.buffer[3];
                        ao2.buffer[4] = 0x00;
                        ao2.buffer[5] = 0x03;

                        ao2.buffer[6] = ao.buffer[6];
                        ao2.buffer[7] = 0x83;
                        ao2.buffer[8] = 0x02;

                        ao2.workSocket = ModbusSlave;
                        ModbusSlave.BeginSend(ao2.buffer, 0, ao2.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
                    }
                }
                else if(functioncode == 0x06)
                {
                    //원래 주소 검사, slave id검사 등을 통해서 일치하는지 검사하고 값을 대입
                    //일치하지 않을 경우 오류코드를 전송, 마스터에서도 오류코드 검사
                    //현재 주소 검사는 시행하는 중.
                    byte[] startaddr = new byte[2];
                    startaddr[0] = ao.buffer[8];
                    startaddr[1] = ao.buffer[9];

                    int nstartaddr = 256 * startaddr[0] + startaddr[1];

                    if((nstartaddr >= Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value)) && (nstartaddr <= Convert.ToInt32(dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[0].Value)))
                    {
                        byte[] data = new byte[2];
                        data[0] = ao.buffer[10];
                        data[1] = ao.buffer[11];

                        int ndata = 256 * data[0] + data[1];

                        int nindex = 0;
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            if (Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value) == nstartaddr)
                            {
                                nindex = i;
                            }
                        }

                        dataGridView1.Rows[nindex].Cells[2].Value = ndata;

                        AsyncMasterFrame ao2 = new AsyncMasterFrame(12);

                        for (int i = 0; i < ao2.buffer.Length; i++)
                        {
                            ao2.buffer[i] = ao.buffer[i];
                        }

                        ao2.workSocket = ModbusSlave;
                        ModbusSlave.BeginSend(ao2.buffer, 0, ao2.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
                    }
                    else
                    {
                        AsyncMasterFrame ao2 = new AsyncMasterFrame(9);

                        for (int i = 0; i < 5; i++)
                        {
                            ao2.buffer[i] = ao.buffer[i];
                        }
                        ao2.buffer[5] = 0x03;
                        ao2.buffer[6] = 0x01;
                        ao2.buffer[7] = 0x86;//에러코드
                        ao2.buffer[8] = 0x02;

                        ao2.workSocket = ModbusSlave;
                        ModbusSlave.BeginSend(ao2.buffer, 0, ao2.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
                    }
                }
                else if(functioncode == 0x10)
                {
                    byte[] startaddr = new byte[2];
                    startaddr[0] = ao.buffer[8];
                    startaddr[1] = ao.buffer[9];

                    int nstartaddr = 256 * startaddr[0] + startaddr[1];

                    byte[] datacount = new byte[2];
                    datacount[0] = ao.buffer[10];
                    datacount[1] = ao.buffer[11];

                    int ndatacount = 256 * datacount[0] + datacount[1];

                    //조건이 일치할 경우 true값을 보냄
                    if ((nstartaddr == Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value)) && (ndatacount == Convert.ToInt32(dataGridView1.Rows.Count)))
                    {

                        for(int i=0;i<dataGridView1.Rows.Count;i++)
                        {
                            int j = i*2;
                            int value = 256 * ao.buffer[13+j] + ao.buffer[14+j];

                            dataGridView1.Rows[i].Cells[2].Value = value;
                        }

                        AsyncMasterFrame ao2 = new AsyncMasterFrame(12);

                        ao2.buffer[0] = ao.buffer[0];
                        ao2.buffer[1] = ao.buffer[1];
                        ao2.buffer[2] = ao.buffer[2];
                        ao2.buffer[3] = ao.buffer[3];
                        ao2.buffer[4] = 0x00;
                        ao2.buffer[5] = 0x06;

                        ao2.buffer[6] = ao.buffer[6];
                        ao2.buffer[7] = ao.buffer[7];
                        ao2.buffer[8] = ao.buffer[8];
                        ao2.buffer[9] = ao.buffer[9];
                        ao2.buffer[10] = ao.buffer[10];
                        ao2.buffer[11] = ao.buffer[11];

                        ao2.workSocket = ModbusSlave;
                        ModbusSlave.BeginSend(ao2.buffer, 0, ao2.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
                    }
                    else //조건 불일치일 경우 90 에러코드 전송
                    {
                        AsyncMasterFrame ao2 = new AsyncMasterFrame(9);

                        ao2.buffer[0] = ao.buffer[0];
                        ao2.buffer[1] = ao.buffer[1];
                        ao2.buffer[2] = ao.buffer[2];
                        ao2.buffer[3] = ao.buffer[3];
                        ao2.buffer[4] = 0x00;
                        ao2.buffer[5] = 0x03;

                        ao2.buffer[6] = ao.buffer[6];
                        ao2.buffer[7] = 0x90;
                        ao2.buffer[8] = 0x02;

                        ao2.workSocket = ModbusSlave;
                        ModbusSlave.BeginSend(ao2.buffer, 0, ao2.buffer.Length, SocketFlags.None, asyncCbRequest, ao);
                    }
                }
            }

            Thread.Sleep(10);
            ao.inibuffer();
            ao.workSocket.BeginReceive(ao.buffer, 0, ao.buffer.Length, SocketFlags.None, asyncCbRespond, ao);
        }
Beispiel #6
0
        private void AsyncHandleAccept(IAsyncResult ar)
        {
            ModbusSlave = ModbusSlave.EndAccept(ar);

            AsyncMasterFrame ao = new AsyncMasterFrame(1024);
            ao.workSocket = ModbusSlave;

            ModbusSlave.BeginReceive(ao.buffer, 0, ao.buffer.Length, SocketFlags.None, asyncCbRespond, ao);

            buttonTCP.Text = "Disconnect";

            labelState.Text = "Connected";
        }