예제 #1
0
파일: PLCManager.cs 프로젝트: dackachki/PLC
        private CDataObject GetReadOKFrame(CDataObject data, byte[] readdata)
        {
            CDataObject ret = new CDataObject(11 + readdata.Length);

            if (data.Count == 21)
            {
                ret.m_Data[0] = 0x50;
                ret.m_Data[1] = 0x00;   // Subheader

                ret.m_Data[2] = 0xFF;   // Network No. (Self station)
                ret.m_Data[3] = 0x00;   // PC No.

                ret.m_Data[4] = 0xFF;
                ret.m_Data[5] = 0x03;           // Specific value
                ret.m_Data[6] = data.m_Data[6]; // Specific value 2

                int wReqLen = readdata.Length + 2;
                ret.m_Data[7] = (byte)(wReqLen & 0x00FF);
                ret.m_Data[8] = (byte)((wReqLen >> 8) & 0x00FF); // Request data length

                ret.m_Data[9]  = 0x00;                           // 0x04;
                ret.m_Data[10] = 0x00;                           // CPU monitoring timer (unit is 250ms)
                Array.Copy(readdata, 0, ret.m_Data, 11, readdata.Length);
            }
            return(ret);
        }
예제 #2
0
 public void Copy(CDataObject ob)
 {
     Array.Copy(ob.m_Data, m_Data, 4097);
     Count     = ob.Count;
     ExtraInfo = ob.ExtraInfo;
     SendTo    = ob.SendTo;
 }
예제 #3
0
 void AddReceiveData(CDataObject obj)
 {
     lock (this)
     {
         ArrayBuffer.Add(obj);
     }
 }
예제 #4
0
 public CDataObject GetReceiveData()
 {
     lock (this)
     {
         if (ArrayBuffer.Count != 0)
         {
             CDataObject obj = (CDataObject)ArrayBuffer[0];
             ArrayBuffer.RemoveAt(0);
             return(obj);
         }
     }
     return(null);
 }
예제 #5
0
파일: PLCManager.cs 프로젝트: dackachki/PLC
        public void ServerAction(object arg)
        {
            string[]      fullText = Convert.ToString(arg).Split('/');
            string        ip       = fullText[0];
            int           portno   = Convert.ToInt32(fullText[1]);
            int           id       = int.Parse(fullText[2]);
            int           index    = -1;
            CSocketServer server   = new CSocketServer();

            bool x = server.StartListen(ip, portno);    // 서버.대기시작(포트번호)

            if (x == false)
            {
                MessageBox.Show(portno.ToString() + " 통신 Port를 확인하세요");
                Environment.Exit(1);
            }


            while (SocketLive && PLCName != null)
            {
                Thread.Sleep(1);
                CDataObject data = server.GetReceiveData();

                //0번 배열 길이
                for (int i = 0; i < SocketConnect.GetLength(0); i++)
                {
                    if (SocketConnect[i, 0] == portno)
                    {
                        index = i;
                        if (server.m_workerSocket[0] != null && SocketConnect[index, 0] != 0)
                        {
                            if (index != -1 && server.m_workerSocket[0].Connected)
                            {
                                SocketConnect[index, 1] = 1;
                            }
                            else
                            {
                                SocketConnect[index, 1] = 0;
                            }
                        }
                        break;
                    }
                }

                if (data != null)               // 서버에서, 수신 데이터가 Null이 아니면
                {
                    PLCMethod(server, data);
                }
            }
            server.CloseSockets();
        }
예제 #6
0
 public bool SendMessage(CDataObject sendobj)
 {
     try
     {
         if (m_clientSocket != null && m_clientSocket.Connected)
         {
             m_clientSocket.Send(sendobj.m_Data, sendobj.Count, SocketFlags.None);
             return(true);
         }
     }
     catch (SocketException se)
     {
         //Debug.WriteLine(se.Message);
     }
     return(false);
 }
예제 #7
0
파일: PLCManager.cs 프로젝트: dackachki/PLC
        private CDataObject GetErrorFrame(UInt16 errorcode)
        {
            CDataObject ret = new CDataObject(11);

            ret.m_Data[0] = 0x50;
            ret.m_Data[1] = 0x00;   // Subheader

            ret.m_Data[2] = 0xFF;   // Network No. (Self station)
            ret.m_Data[3] = 0x00;   // PC No.

            ret.m_Data[4] = 0xFF;
            ret.m_Data[5] = 0x03;   // Specific value
            ret.m_Data[6] = 0x00;   // Specific value 2

            ret.m_Data[7] = 0x02;
            ret.m_Data[8] = 0x00;                               // Request data length

            ret.m_Data[9]  = (byte)(errorcode & 0x00FF);        // 0x04;
            ret.m_Data[10] = (byte)((errorcode >> 8) & 0x00FF); // CPU monitoring timer (unit is 250ms)
            return(ret);
        }
예제 #8
0
        public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
                int          iRx       = theSockId.thisSocket.EndReceive(asyn);

                CDataObject obj = new CDataObject();
                obj.Copy(theSockId.dataBuffer, iRx);
                AddReceiveData(obj);
                WaitForData();
            }
            catch (ObjectDisposedException)
            {
                //System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
                //Debug.WriteLine("\nOnDataReceived: Socket has been closed\n");
            }
            catch (SocketException se)
            {
                //Debug.WriteLine(se.Message);
            }
        }
예제 #9
0
파일: PLCManager.cs 프로젝트: dackachki/PLC
        private CDataObject GetWrtieOKFrame(CDataObject data)
        {
            CDataObject ret = new CDataObject(11);

            if (data.Count > 11)
            {
                ret.m_Data[0] = 0x50;
                ret.m_Data[1] = 0x00;   // Subheader

                ret.m_Data[2] = 0xFF;   // Network No. (Self station)
                ret.m_Data[3] = 0x00;   // PC No.

                ret.m_Data[4] = 0xFF;
                ret.m_Data[5] = 0x03;           // Specific value
                ret.m_Data[6] = data.m_Data[6]; // Specific value 2

                ret.m_Data[7] = 0x02;
                ret.m_Data[8] = 0x00;  // Request data length

                ret.m_Data[9]  = 0x00; // 0x04;
                ret.m_Data[10] = 0x00; // CPU monitoring timer (unit is 250ms)
            }
            return(ret);
        }
예제 #10
0
파일: PLCManager.cs 프로젝트: dackachki/PLC
        private void PLCMethod(CSocketServer server, CDataObject data)
        {
            if (data.Count >= 21)
            {
                if (data.m_Data[0] == 0x50 &&
                    data.m_Data[1] == 0x00 &&
                    data.m_Data[2] == 0x00 &&
                    data.m_Data[3] == 0xFF &&
                    data.m_Data[4] == 0xFF &&
                    data.m_Data[5] == 0x03 &&
                    (data.m_Data[9] == 0x04 || data.m_Data[9] == 0x08 || data.m_Data[9] == 0x10 || data.m_Data[9] == 0x0A) &&
                    data.m_Data[11] == 0x01)
                {
                    if (data.m_Data[12] == 0x14) // write frame
                    {
                        int size         = ex.GetByteToInt(data.m_Data, 19, 2);
                        int startaddress = ex.GetByteToInt(data.m_Data, 15, 3);
                        if (data.Count == 21 + size * 2)
                        {
                            if (size < 2000)
                            {
                                if (data.m_Data[18] == Device_Def.DEVICE_CODE_D)
                                {
                                    Byte[] writedata = new Byte[size * 2];
                                    Array.Copy(data.m_Data, 21, writedata, 0, writedata.Length);
                                    WriteForByte(Device_Def.DEVICE_CODE_D, startaddress, writedata);
                                    server.BroadCastingMsg(GetWrtieOKFrame(data));
                                }
                                else if (data.m_Data[18] == Device_Def.DEVICE_CODE_R)
                                {
                                    Byte[] writedata = new Byte[size * 2];
                                    Array.Copy(data.m_Data, 21, writedata, 0, writedata.Length);
                                    WriteForByte(Device_Def.DEVICE_CODE_R, startaddress, writedata);
                                    server.BroadCastingMsg(GetWrtieOKFrame(data));
                                }
                                else if (data.m_Data[18] == Device_Def.DEVICE_CODE_W)
                                {
                                    Byte[] writedata = new Byte[size * 2];
                                    Array.Copy(data.m_Data, 21, writedata, 0, writedata.Length);
                                    WriteForByte(Device_Def.DEVICE_CODE_W, startaddress, writedata);
                                    server.BroadCastingMsg(GetWrtieOKFrame(data));
                                }
                                else if (data.m_Data[18] == Device_Def.DEVICE_CODE_ZR)
                                {
                                    Byte[] writedata = new Byte[size * 2];
                                    Array.Copy(data.m_Data, 21, writedata, 0, writedata.Length);
                                    WriteForByte(Device_Def.DEVICE_CODE_ZR, startaddress, writedata);
                                    server.BroadCastingMsg(GetWrtieOKFrame(data));
                                }
                                else
                                {
                                    server.BroadCastingMsg(GetErrorFrame(1001));
                                }
                            }
                            else
                            {
                                server.BroadCastingMsg(GetErrorFrame(1011));
                            }
                        }
                        else
                        {
                            server.BroadCastingMsg(GetErrorFrame(1002));
                        }
                    }
                    else if (data.m_Data[12] == 0x04) // read frame
                    {
                        if (data.Count == 21)
                        {
                            int size         = ex.GetByteToInt(data.m_Data, 19, 2);
                            int startaddress = ex.GetByteToInt(data.m_Data, 15, 3);
                            if (size <= 2000)
                            {
                                if (data.m_Data[18] == Device_Def.DEVICE_CODE_D)
                                {
                                    Byte[] readdata = ReadForByte(Device_Def.DEVICE_CODE_D, startaddress, size);
                                    server.BroadCastingMsg(GetReadOKFrame(data, readdata));
                                }
                                else if (data.m_Data[18] == Device_Def.DEVICE_CODE_R)
                                {
                                    Byte[] readdata = ReadForByte(Device_Def.DEVICE_CODE_R, startaddress, size);
                                    server.BroadCastingMsg(GetReadOKFrame(data, readdata));
                                }
                                else if (data.m_Data[18] == Device_Def.DEVICE_CODE_W)
                                {
                                    Byte[] readdata = ReadForByte(Device_Def.DEVICE_CODE_W, startaddress, size);
                                    server.BroadCastingMsg(GetReadOKFrame(data, readdata));
                                }
                                else if (data.m_Data[18] == Device_Def.DEVICE_CODE_ZR)
                                {
                                    Byte[] readdata = ReadForByte(Device_Def.DEVICE_CODE_ZR, startaddress, size);
                                    server.BroadCastingMsg(GetReadOKFrame(data, readdata));
                                }
                                else
                                {
                                    server.BroadCastingMsg(GetErrorFrame(1003));
                                }
                            }
                            else
                            {
                                server.BroadCastingMsg(GetErrorFrame(1013));
                            }
                        }
                        else
                        {
                            server.BroadCastingMsg(GetErrorFrame(1004));
                        }
                    }
                    else
                    {
                        server.BroadCastingMsg(GetErrorFrame(1005));

                        Thread.Sleep(200);
                    }
                }
                else
                {
                    server.BroadCastingMsg(GetErrorFrame(1006));
                }
            }
            else
            {
                server.BroadCastingMsg(GetErrorFrame(1007));

                Thread.Sleep(15);
            }
        }
예제 #11
0
        void DisplayGridData()
        {
            byte[] tmpdata = new byte[m_nReadLength * 3];

            SendFrame.MakeReadFrame(Device_Def.CMD_WORD_UNIT, Device_Def.SearchDeviceCode(DeviceName), iSelectedAddress, m_nReadLength);

            if (CL.SendMessage(SendFrame) == false)
            {
                return;
            }
            else
            {
                CDataObject data = CL.GetReceiveData();

                if (data != null)
                {
                    if (data.Count == (m_nReadLength * 2 + 11))
                    {
                        if (data.m_Data[9] == 0x00 && data.m_Data[10] == 0x00)
                        {
                            Array.Copy(data.m_Data, 11, tmpdata, 0, m_nReadLength * 2);
                        }
                    }
                    else
                    {
                    }
                }
            }
            //row만큼 반복
            for (int nRow = 0; nRow < Grid.RowCount - 1; nRow++)
            {
                int nColumnIndex = 0;
                // Address
                string szDeviceName = String.Format("{0}{1}", DeviceName, iSelectedAddress + nRow);
                Grid.Rows[nRow].Cells[nColumnIndex].Value = szDeviceName;
                nColumnIndex++;

                //low
                for (int nCol = 0; nCol < 8; nCol++)
                {
                    //데이터 배열값과 값이 같으면0
                    int v = (1 << (nCol % 8));
                    if ((tmpdata[nRow * 2 + 1] & (1 << (nCol % 8))) == 0)
                    {
                        Grid.Rows[nRow].Cells[8 - nCol].Value           = 0;
                        Grid.Rows[nRow].Cells[8 - nCol].Style.BackColor = Color.White;
                    }
                    //다르면 1
                    else
                    {
                        Grid.Rows[nRow].Cells[8 - nCol].Value           = 1;
                        Grid.Rows[nRow].Cells[8 - nCol].Style.BackColor = Color.Cyan;
                    }

                    nColumnIndex++;
                }
                //high
                for (int nCol = 0; nCol < 8; nCol++)
                {
                    if ((tmpdata[nRow * 2] & (1 << (nCol % 8))) == 0)
                    {
                        Grid.Rows[nRow].Cells[16 - nCol].Value           = 0;
                        Grid.Rows[nRow].Cells[16 - nCol].Style.BackColor = Color.White;
                    }
                    else
                    {
                        Grid.Rows[nRow].Cells[16 - nCol].Value           = 1;
                        Grid.Rows[nRow].Cells[16 - nCol].Style.BackColor = Color.Cyan;
                    }


                    nColumnIndex++;
                }
                //17
                Grid.Rows[nRow].Cells[nColumnIndex].Value = tmpdata[nRow * 2 + 1];
                nColumnIndex++;
                //18
                Grid.Rows[nRow].Cells[nColumnIndex].Value = tmpdata[nRow * 2];
                nColumnIndex++;

                // All
                Grid.Rows[nRow].Cells[nColumnIndex].Value = GetIntLengthWord(tmpdata, nRow * 2);
                nColumnIndex++;

                // ASCII
                Grid.Rows[nRow].Cells[nColumnIndex].Value = Encoding.Default.GetString(tmpdata, nRow * 2 + 1, 1) + Encoding.Default.GetString(tmpdata, nRow * 2, 1);
            }
        }