如何解析收到通过TCP数字字符串 [英] How to parse numeric strings recieved over TCP

查看:741
本文介绍了如何解析收到通过TCP数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从通过TCP我的传感器接收数据,并输出如下:
< -0.040000,-0.005000,0,025000,0,990000,-0,000500,0.033000>

I'm receiving data from my sensor via TCP and the output looks like this: <-0.040000 , -0.005000 , 0,025000 , 0,990000 , -0,000500 , 0.033000 >

这是一个6次双精度值。我只需要前三。在X,Y和Z方向上的力,以获得他们的合力。有人告诉我,我reciving传感器流串重的双presentation',我应该使用ATOF函数,它接受一个字符串,再一个浮点数presenting并返回一个double。

It's a 6 times double value. I need only first three. Forces in X,Y and Z direction to get their resultant force. I was told I'm reciving 'sensor streams string representation of double' and that I should use atof function which takes a string representing of a floating point number and returns a double.

所以,问题是。我用下面的code接收来自传感器的数据。

So, the problem is. I'm using following code to receive data from sensor

char recvbuf[DEFAULT_BUFFER_LENGTH];
int iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);

double n;
n = atof (recvbuf);

输出永远是错的,无论是我得到错误的数据,而不是30000 0.1414,或我读到0.15为0,我应该如何得到所有3个数据有什么建议?我用BUFFER = 50,因为我并不需要阅读更多,我甚至不知道过了多久共有来自传感器字符串。

Output is always wrong, either I get wrong data, 30000 instead of 0.1414, or I read 0.15 as 0. Any tips on how should I get all 3 data? I use BUFFER=50, cuz I don't need to read more and I don't even know how long in total, string from sensor is.

推荐答案

您需要打破这种分解成较小的步骤。例如:

You need to break this down into smaller steps. For example:


  1. 接收来自插座一个任意大小的数据包。

  2. 在数据包缓冲区搜索( recvbuf )的起动用数据,标记'&LT;

  3. 复制从包缓冲器来解析缓冲区中的所有以下字符(您需要创建它,让大到足以容纳6个数据点的最长可能数据重新presentation)。

    • 3A。如果你发现数据结束,一个标记'&GT;'。,转到第4步

    • 3B。如果您运行的数据,接收下一个数据包并重复步骤3。

  1. Receive an arbitrary sized packet from socket.
  2. Search in the packet buffer (recvbuf) for a start-of-data marker '<'.
  3. Copy all the following characters from the packet buffer to a parse buffer (which you need to create. Make it big enough to hold the longest possible data representation of the 6 data points).
    • 3A. If you find an end-of-data marker '>', go to step 4.
    • 3B. If you run out of data, receive the next packet and repeat step 3.

这篇关于如何解析收到通过TCP数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆