Vicon C Sharp

<< Back to the Vicon Toolkit page

Download Vicon Toolkit

Vicon RealTime Data: How to access/use the Vicon data stream.

As a prerequisite please see the Vicon Real Time tutorial.

The first step is to open a socket connection and send a request for EInfo

        //create a new client socket ...
	m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
	String szIPSelected  = txtIPAddress.Text;
	String szPort = txtPort.Text;
	int  alPort = System.Convert.ToInt16 (szPort,10);

	System.Net.IPAddress	remoteIPAddress	 = System.Net.IPAddress.Parse(szIPSelected);
	System.Net.IPEndPoint	remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
	m_socClient.Connect(remoteEndPoint);
        byte[] sendBytes = new byte[8];
        sendBytes[0] = (byte) ClientCodes.EPacket.EInfo;
        for (int i = 1; i < 8; ++i) {
             sendBytes[i] = 0;
        }
        int sentBytes = m_socClient.Send(sendBytes);

The next step is to obtain the real time data by recieving packets though the same socket.

                //send request for vicon Data
                byte[] sendBytes = new byte[8];
                sendBytes[0] = (byte)ClientCodes.EPacket.EData;
                for (int i = 1; i < 8; ++i) {
                    sendBytes[i] = 0;
                }
                int sentBytes = m_socClient.Send(sendBytes);
                //Now receive the vicon data
                byte[] buffer = new byte[4];
                int iRx = m_socClient.Receive(buffer);
                int packet = System.BitConverter.ToInt32(buffer, 0);
                iRx = m_socClient.Receive(buffer);
                int type = System.BitConverter.ToInt32(buffer, 0);

                if (type != (int)ClientCodes.EType.EReply) {
                    throw new SocketException();
                }
                if (packet != (int)ClientCodes.EPacket.EData){
                    throw new SocketException();
                }
                iRx = m_socClient.Receive(buffer);
                int size = System.BitConverter.ToInt32(buffer, 0);
                if (size != iViconInfoSize){
                    throw new SocketException();
                }
                buffer = new byte[8];                

                iRx = m_socClient.Receive(buffer);
                double value = System.BitConverter.ToDouble(buffer, 0);
                txtData.Text = "Frame " + value + "\r\n";

                foreach (ViconChannel v in viconArray)
                {
                    if (v.Type == ChannelType.Marker)
                    {
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.PX = value;
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.PY = value;
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.PZ = value;
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        if (value > 0.5)
                            v.Visible = false;
                        else
                            v.Visible = true;

                        txtData.Text += v.ParentName + ":" + v.Name + " " + v.Visible.ToString() + " " + v.PX + " " + v.PY + " " + v.PZ + "\r\n";
                    }
                    else if (v.Type == ChannelType.Body)
                    {
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.AX = value;
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.AY = value;
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.AZ = value;
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.TX = value;
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.TY = value;
                        iRx = m_socClient.Receive(buffer);
                        value = System.BitConverter.ToDouble(buffer, 0);
                        v.TZ = value;
                        v.ComputeEulerAngles();
                        txtData.Text += v.ParentName + ":" + v.Name + " " + v.TX + " " + v.TY + " " + v.TZ + " " + v.AX + " " + v.AY + " " + v.AZ + "\r\n";
                    }
                }