本帖最后由 ejiyuan1 于 2014-11-14 10:28 编辑
- 方法一:
-
- String comdata = "";
- float t1[6]={0};
- int mark =0;
- char *p=NULL;
- void setup()
- {
- Serial.begin(9600);
- }
- void loop()
- {
- int i=0;
- int j=0;
- comdata="";
- while (Serial.available() > 0)
- {
- comdata += char(Serial.read());
- delay(2);
- mark = 1;
- }
- if(mark == 1)
- {
- Serial.println(comdata);
- p=strtok((char*)comdata.c_str(),",");
- while(p)
- {
- t1[i]=atof(p);
- i++;
- p=strtok(NULL,",");
- }<div class="blockcode"><blockquote>方法二:
- void getdata()
- {
- int i=0;
- int j = 0;
- while (tcpClient.available() > 0)
- {
-
- comdata += char(tcpClient.read());
- delay(2);
- mark = 1;
- }
-
- if(mark == 1)
- {
- tcpClient.fastrprintln("receive data success");
- // Serial.println(comdata);
- // Serial.println(comdata.length());
-
- for(int i = 0; i < comdata.length() ; i++)
- {
- if(comdata[i] == ',')
- {
- j++;
- }
- else
- {
- numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
- }
- }
- //comdata的字符串已经全部转换到numdata了,清空comdata以便下一次使用,
- //如果不请空的话,本次结果极有可能干扰下一次。
- comdata = String("");
- mark=0;
-
- for( i=0;i<2;i++)
- {
- // Serial.println(numdata[i]);
- //numdata[i] = 0;
- }
-
- for( i=0;i<2;i++)
- {
- // Serial.println(numdata[i]);
- numdata[i] = 0;
- }
-
- }
- }
复制代码
comdata = String("");
mark=0;
for(int j=0;j<6;j++)
{
Serial.println(t1[j]);
}
}
}
|