Beispiel #1
0
        //writes variable value combination to KUKA server
        public void Write_VariableValueString(String VarToWrite, String ValueToWrite)
        {
            Status    = "";
            Exception = "";

            // Create Local objects
            TcpClient         TCP_Client;
            TCP_StringHandler StringHandler = new TCP_StringHandler();

            String[] StringArray_toWrite = new String[2 + 2];
            String   String_toWrite      = "";

            try
            {   // Update Status texbox
                Status = Status + ("Trying to connect..." + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                //Create TCP client and connect
                TCP_Client = new TcpClient(IP_address, Convert.ToInt32(Port));

                while (TCP_Client.Connected)
                {
                    Status = Status + ("TCP client connected" + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                    Status = Status + ("writing..." + " " + DateTime.Now.TimeOfDay.ToString() + "\n");

                    StringArray_toWrite = StringHandler.CombineStringsToArray_WriteRead(VarToWrite, ValueToWrite, "write");
                    String_toWrite      = StringHandler.StringArrayToString(StringArray_toWrite);

                    byte[] bytesToSend = Encoding.ASCII.GetBytes(String_toWrite);

                    NetworkStream ns = TCP_Client.GetStream();
                    ns.Write(bytesToSend, 0, bytesToSend.Length);
                    String StringSent = Encoding.ASCII.GetString(bytesToSend);

                    Status = Status + (StringSent + " At time:" + DateTime.Now.TimeOfDay.ToString() + "\n");

                    String_toWrite = "";
                    TCP_Client.Close();
                }
                TCP_Client.Close();
            }
            catch (Exception exc)
            {
                //update excecption textboxes
                Exception = Exception + (exc.ToString() + " " + DateTime.Now.TimeOfDay.ToString());
                Exception = Exception + ("\n");
                Exception = Exception + ("CHECK IS SERVER IS RUNNING AND LISTENING" + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                Exception = Exception + ("CHECK IP ADDRESS" + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                Exception = Exception + ("\n");
            }
        }
Beispiel #2
0
        public String Read_VariableValueString(String VarToRead)
        {
            Status    = "";
            Exception = "";

            // Create Local objects
            TcpClient              TCP_Client;
            TCP_StringHandler      StringHandler = new TCP_StringHandler();
            clientWPFObjectHandler ObjectHandler = new clientWPFObjectHandler();

            String[] StringArray_toRead   = new String[2 + 2];
            String[] a_ReceivedReadVarVal = new String[2 + 2];
            String   String_toRead        = "";

            try
            {
                Status     = Status + ("Trying to connect..." + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                TCP_Client = new TcpClient(IP_address, Convert.ToInt32(Port));

                while (TCP_Client.Connected)
                {
                    //Update Status TextBox
                    Status = Status + ("TCP client connected" + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                    Status = Status + ("reading..." + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                    // Combine strins to array using a separator
                    StringArray_toRead = StringHandler.CombineStringsToArray_WriteRead(VarToRead, "ReadOnly", "read");
                    // convert string array to string which will be sent to the Server
                    String_toRead = StringHandler.StringArrayToString(StringArray_toRead);

                    //print in a status text box variables which you want to read
                    Status = Status + (VarToRead + " " + DateTime.Now.TimeOfDay.ToString() + "\n");

                    byte[]        byteToSend = Encoding.ASCII.GetBytes(String_toRead);
                    NetworkStream ns         = TCP_Client.GetStream();
                    ns.Write(byteToSend, 0, byteToSend.Length);
                    String StringSent = Encoding.ASCII.GetString(byteToSend);
                    String_toRead = "";

                    //getting read values back from the server
                    try
                    {
                        byte[] bytesReceived   = new byte[1024];
                        int    i_bytesReceived = ns.Read(bytesReceived, 0, bytesReceived.Length);

                        String readValues = Encoding.ASCII.GetString(bytesReceived, 0, i_bytesReceived);

                        a_ReceivedReadVarVal = StringHandler.StringToStringArray(readValues, ',');

                        Status = Status + ("READ VALUES FROM THE SERVER " + DateTime.Now.TimeOfDay.ToString() + "\n");

                        //print in status text box read variables and values
                        Status = Status + (StringHandler.ReadVariableFromServer(a_ReceivedReadVarVal, 1) + DateTime.Now.TimeOfDay.ToString() + "\n");

                        TCP_Client.Close();
                    }
                    catch (Exception exc)
                    {
                        //Update exception TextBox
                        Exception = Exception + (exc.ToString() + " " + DateTime.Now.TimeOfDay.ToString());
                        Exception = Exception + ("CANNOT READ VALUES\n");
                        Exception = Exception + ("CHECK IS SERVER IS RUNNING AND LISTENING" + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                        Exception = Exception + ("CHECK IP ADDRESS" + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                        Exception = Exception + ("\n");
                    }
                }
            }
            catch (Exception exc)
            {
                //Update exception TextBox
                Exception     = Exception + (exc.ToString() + " " + DateTime.Now.TimeOfDay.ToString());
                Exception     = Exception + ("\n");
                Exception     = Exception + ("CHECK IS SERVER IS RUNNING AND LISTENING" + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                Exception     = Exception + ("CHECK IP ADDRESS" + " " + DateTime.Now.TimeOfDay.ToString() + "\n");
                Exception     = Exception + ("\n");
                String_toRead = "";
            }
            return(StringHandler.ReadVariableFromServer(a_ReceivedReadVarVal, 2));
        }