简体   繁体   English

如何解析通过TCP收到的数字字符串

[英]How to parse numeric strings recieved over TCP

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 > 我正在通过TCP从传感器接收数据,并且输出看起来像这样:<-0.040000,-0.005000,0,025000,0,990000,-0,000500,0.033000>

It's a 6 times double value. 这是6倍的两倍。 I need only first three. 我只需要前三个。 Forces in X,Y and Z direction to get their resultant force. 沿X,Y和Z方向的力获得其合力。 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. 有人告诉我,我正在接收“传感器流字符串表示形式为double的表示形式”,我应该使用atof函数,该函数采用表示浮点数的字符串并返回一个double形式。

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);

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? 输出总是错误的,要么我得到了错误的数据,即30000而不是0.1414,要么我将0.15读为0。关于如何获取所有3个数据的任何提示? 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. 我使用BUFFER = 50,因为我不需要阅读更多,甚至不知道传感器发出的字符串总共有多长时间。

You need to break this down into smaller steps. 您需要将其分解为较小的步骤。 For example: 例如:

  1. Receive an arbitrary sized packet from socket. 从套接字接收任意大小的数据包。
  2. Search in the packet buffer ( recvbuf ) for a start-of-data marker '<' . 在数据包缓冲区( recvbuf )中搜索数据开始标记'<'
  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). 将以下所有字符从数据包缓冲区复制到解析缓冲区(您需要创建它。使其足够大以容纳6个数据点的最长的数据表示形式)。
    • 3A. 3A。 If you find an end-of-data marker '>' , go to step 4. 如果找到数据结束标记'>' ,请转到步骤4。
    • 3B. 3B。 If you run out of data, receive the next packet and repeat step 3. 如果数据用完,请接收下一个数据包并重复步骤3。
  4. Break parse buffer at commas into N value strings. 将解析缓冲区的逗号分隔为N个值字符串。
  5. Convert each value string into a number with atof or fscanf . 使用atoffscanf将每个值字符串转换为数字。
  6. Go to Step2, starting from the character after the last end-of-data. 从最后一个数据结尾之后的字符开始,转到步骤2。

You are calling atof at arbitrary points in the data stream. 您正在数据流中的任意点调用atof That will not work. 那不管用。

There are several steps needed to do this reliably. 要可靠地执行此操作,需要执行几个步骤。

  1. The data you get from recv can a partial data set that needs to be appended to the preceding and by the following recv calls's data. recv获取的数据可以是部分数据集,需要将其追加到前面和后面的recv调用的数据之后。 This is sometimes done in a pipeline fashion that reads characters from the recvbuf into a parsebuf . 有时这是通过管道方式完成的,该方式将从recvbuf中读取字符到parsebuf

  2. Your data looks to be framed with ( and ) so your copy routine can skip to the first ( and then copy data up to the following ) 您的数据看起来像用()框起来,因此您的复制例程可以跳到第一个(然后将数据复制到以下内容)

  3. When the copy routine hits the end of the recvbuf it should call recv again to fill the recvbuf and continue framing the data from the start of recvbuf where it left off in parsebuf 当复制例程到达recvbuf的末尾时,它应再次调用recv来填充recvbuf并继续在recvbuf的开头将数据recvbuf中以对其进行parsebuf

  4. At each ) the data in the parsebuf will always be <x> , <y> , <z> , ... so your atof call has something reasonable to deal with. 在每个)中, parsebuf的数据将始终为<x> , <y> , <z> , ...因此您的atof调用可以合理处理。 Consider using fscanf 考虑使用fscanf

  5. Reset the pointer to the parsebuf after each conversion. 每次转换后,将指针重置为parsebuf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM