Read the data from tcp server

2 views (last 30 days)
david
david on 5 Nov 2014
I want to read bvh data from Mocap server and display it in Matlab in realtime, the Mocap server will keep push realtime data to a tcp client if you are connected. With C# I will use networkstream to read the bvh data like this.
string ip=ConfigurationManager.AppSettings["mocap_ip"];
int port=Convert.ToInt32( ConfigurationManager.AppSettings["mocap_port"]);
bool showlog = debug;
Socket socket;
IPEndPoint maxPort = new IPEndPoint(IPAddress.Parse(ip), port);
socket =
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(maxPort);
}
catch (Exception e)
{
if (!socket.Connected)
{
//Console.WriteLine("Cant connect to mocap server, please restart this application when mocap server started.\n exception is" + e.ToString());
return;
}
}
myNetworkStream = new NetworkStream(socket);
if (myNetworkStream.CanRead)
{
while (myNetworkStream.CanRead && sending)
{
byte[] myReadBuffer = new byte[1024];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
// Incoming message may be larger than the buffer size.
do
{
numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while (myNetworkStream.DataAvailable);
myCompleteMessage will be realtime bvh data back of each frame. But with Matlab, I used tcp connection to do it.
t = tcpip('localhost', 6001);
fopen(t);
pause(1)
t.ValuesReceived
while (get(t, 'BytesAvailable') > 0)
t.BytesAvailable
DataReceived = fscanf(t)
end
close(t);
delete(t);
clear t
The normal data I want get is something like below,every time we should receive only 1 frame data , I use "copy resource" to copy it from the Instrument Control Toolbox, while I create a connection under TCPIP section and Click Identify button.
127.0.0.1:7001 (0 0 0 0 0 -0 109.06 95.28 55.14 3.94 66.38 -1.05 -10.16 -0.19 -0.31 -10.89 14.11 -28.74 0.06 -44.60 -0.32 13.07 -2.08 61.12 0.25 -44.76 -0.07 -3.08 -14.76 -3.45 10.13 0.02 0.05 -2.75 4.17 -15.59 0.13 -44.51 0.23 0.83 7.69 23.96 -0.38 -44.53 0.53 12.38 -21.78 -8.59 -0.01 16.93 -0.08 -4.51 7.22 -0.22 -0.00 10.34 -0.06 -1.12 1.81 0.09 -0.04 10.78 0.04 -1.86 2.99 0.21 -0.00 10.35 -0.03 0.10 1.39 -0.07 -0.03 11.06 -0.06 0.10 1.41 -0.07 -0.01 7.00 -0.09 1.47 26.19 -2.00 -3.36 7.38 0.02 7.45 -0.93 1.19 -16.71 0.01) But when I click Read then I will get feedback like. Every data should start with six 0 and the result is with Read several frames data will receive same time and breakdown to next read.
So I was wondering if Indentify button works, there should be solution in Matlab. Thanks for your advice.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!