Discussion

Left ArrowBack to discussions page
manusahunmanusahun Posts: 3 Apprentice
Hi, 

Best Answers

  • matthewd92matthewd92 Posts: 931 Handy
    Accepted Answer
    You have to unpack the stream, can’t just convert to a string. The docs on the UR website show you how each packet of the stream has been packed, it’s different depending on the message type.  Also, port 30004 is the RTDE port, you have to actually send the setup to the port before it will stream anything back to you. There are good docs on the support site with a python example that we used to base our rtde client on. 

Comments

  • manusahunmanusahun Posts: 3 Apprentice
    edited August 26
    Hi, 
    Im having trouble to read data from port 30002 and 30004, I am already able to use port 29999 and send petitions and read data. Not sure if its done in a different way.

    Following I have my client code in C# to connect to 29999 port and by changing its location to 30004/2 it does connect but the commands I send don't return the values specified.

    CODE:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using UnityEngine;

    public class TCPRobotMode : MonoBehaviour
    {
    #region private members     
    private TcpClient socketConnection;
    private Thread clientReceiveThread;
    #endregion
    private int m = 50;

    // Use this for initialization  
    void Start()
    {
    ConnectToTcpServer();
    }
    // Update is called once per frame
    void Update()
    {
    m++;
    if (m == 100)
    {
                SendPetition("robotmode\n");
                m = 0;
    }
        }
    /// <summary>   
    /// Setup socket connection.    
    /// </summary>  
    private void ConnectToTcpServer()
    {
    try
    {
    clientReceiveThread = new Thread(new ThreadStart(ListenForData));
    clientReceiveThread.IsBackground = true;
    clientReceiveThread.Start();
    }
    catch (Exception e)
    {
    Debug.Log("On client connect exception " + e);
    }
    }
    /// <summary>   
    /// Runs in background clientReceiveThread; Listens for incomming data.     
    /// </summary>     
    private void ListenForData()
    {
    try
    {
                socketConnection = new TcpClient("192.168.10.175", 29999); 
                //
                Byte[] bytes = new Byte[1024];
                while (true)
    {
                    // Get a stream object for reading              
                    using (NetworkStream stream = socketConnection.GetStream())
    {
                        int length;
                        // Read incomming stream into byte arrary.                  
                        while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
    {
                            var incommingData = new byte[length];
    Array.Copy(bytes, 0, incommingData, 0, length);
    // Convert byte array to string message.                        
    string serverMessage = Encoding.ASCII.GetString(incommingData);
                            GlobalParameters.robotMode = serverMessage;
                            // PUM PUM PUM Guardar cositas
                        }
                    }
    }
    }
    catch (SocketException socketException)
    {
    Debug.Log("Socket exception: " + socketException);
    }
    }
    /// <summary>   
    /// Send message to server using socket connection.     
    /// </summary>  
    private void SendPetition(String peticion)
    {
    if (socketConnection == null)
    {
    return;
    }
    try
    {
    // Get a stream object for writing.             
    NetworkStream stream = socketConnection.GetStream();
    if (stream.CanWrite)
    {
    // Convert string message to byte array.                 
    byte[] clientMessageAsByteArray = Encoding.ASCII.GetBytes(peticion);
    // Write byte array to socketConnection stream.                 
    stream.Write(clientMessageAsByteArray, 0, clientMessageAsByteArray.Length);
                }
    }
    catch (SocketException socketException)
    {
    Debug.Log("Socket exception: " + socketException);
    }
    }
    }
  • manusahunmanusahun Posts: 3 Apprentice
    You have to unpack the stream, can’t just convert to a string. The docs on the UR website show you how each packet of the stream has been packed, it’s different depending on the message type.  Also, port 30004 is the RTDE port, you have to actually send the setup to the port before it will stream anything back to you. There are good docs on the support site with a python example that we used to base our rtde client on. 
    Thanks Matthew for your time. Ill try with port 30002 (seems easier), do you know of any open source code for the unpacking of the output stream from the robot?

    Thks again. 
Sign In or Register to comment.
Left ArrowBack to discussions page