极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9807|回复: 2

如何将发向串口的数据分解

[复制链接]
发表于 2014-7-9 16:25:33 | 显示全部楼层 |阅读模式
将一个命令发到下位机上,然后下位机解析命令
比如命令格式为:"Command -Pin -Max -Min"  ,
Command:命令,字符串
Pin:pin脚,字符串
Max:最大值,浮点数
Min:最小值,浮点数
空格+-,“ -”是分隔符
不知道如何将这四个部分从命令中解析出来
自己试了一下,总是得不到想要的结果
  1. String comdata="";
  2. int  mark = 0;
  3. void setup()
  4. {  
  5.   Serial.begin(9600);     //使用9600的波特率进行串口通讯/
  6. }
  7. void loop()
  8. {  
  9.   String StrComm,StrPort,StrMax,StrMin;
  10.   while  (Serial.available() > 0)  
  11.   {
  12.     comdata += char(Serial.read());
  13.     delay(2);
  14.     mark = 1;
  15.   }
  16.   if(mark == 1)
  17.   {
  18.     int count=0,SI=0;
  19.     Serial.println(comdata);
  20.     Serial.println(comdata.length());
  21.     for(int i = 0; i < comdata.length() ; i++)
  22.     {
  23.       //get command
  24.       if (comdata[i] == ' ' && comdata[i+1] == '-')
  25.       {
  26.         if (count=0)
  27.         {
  28.           for(int j=0;j<i;j++)
  29.           {StrComm+=comdata[j];count=1;SI=i;}
  30.           Serial.println("0:");
  31.           continue;        
  32.         }
  33.         if (count=1)
  34.         {
  35.          for(int j=SI+2;j<i;j++)
  36.          {StrPort+=comdata[j];count=2;SI=i;}   
  37.          Serial.println("1:");      
  38.          continue;   
  39.         }
  40.         if (count=2)
  41.         {
  42.          for(int j=SI+2;j<i;j++)
  43.          {StrMax+=comdata[j];count=3;SI=i;}
  44.          Serial.println("2:");
  45.          continue;   
  46.         }   
  47.         if (count=3)
  48.         {
  49.          for(int j=SI+2;j<i;j++)
  50.          {StrMin+=comdata[j];count=4;SI=i;}  
  51.          Serial.println("3:");
  52.          continue;  
  53.         }         
  54.       }     
  55.     }
  56.     mark = 0;
  57.     Serial.println(StrComm);
  58.     Serial.println(StrPort);
  59.     Serial.println(StrMax);
  60.     Serial.println(StrMin);
  61.   }
  62. }
复制代码
回复

使用道具 举报

发表于 2014-7-9 16:43:27 | 显示全部楼层
1、将下列代码下载到arudino里
  1. String inputString = "";         // a string to hold incoming data
  2. boolean stringComplete = false;  // whether the string is complete

  3. void setup()
  4. {
  5.   // initialize serial:
  6.   Serial.begin(9600);
  7.   // reserve 200 bytes for the inputString:
  8.   inputString.reserve(200);
  9. }

  10. void loop()
  11. {
  12.   String ss[4];// print the string when a newline arrives:
  13.   if (stringComplete)
  14.   {
  15.     for(int i=0;i<3;i++)
  16.     {
  17.       ss[i]=inputString.substring(0,inputString.indexOf("-"));
  18.       Serial.println(ss[i]);
  19.       inputString=inputString.substring(inputString.indexOf("-")+1,inputString.length());
  20.     }
  21.     ss[3]=inputString.substring(0,inputString.length()-1);
  22.     Serial.println(ss[3]);
  23.     // clear the string:
  24.     inputString = "";
  25.     stringComplete = false;
  26.   }
  27. }

  28. /*
  29.   SerialEvent occurs whenever a new data comes in the
  30. hardware serial RX.  This routine is run between each
  31. time loop() runs, so using delay inside loop can delay
  32. response.  Multiple bytes of data may be available.
  33. */
  34. void serialEvent()
  35. {
  36.   while (Serial.available()) {
  37.     // get the new byte:
  38.     char inChar = (char)Serial.read();
  39.     // add it to the inputString:
  40.     inputString += inChar;
  41.     // if the incoming character is a newline, set a flag
  42.     // so the main loop can do something about it:
  43.     if (inChar == '\n') {
  44.       stringComplete = true;
  45.     }
  46.   }
  47. }

复制代码
2、打开arduino的串口监视器,波特率选9600,发送方式选Newline
3、在串口监视器中输入“Command -Pin -Max -Min”后回车,观察返回的值
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-7-14 20:27:44 | 显示全部楼层
sinbadcool 发表于 2014-7-9 16:43
1、将下列代码下载到arudino里2、打开arduino的串口监视器,波特率选9600,发送方式选Newline
3、在串口监 ...

发送后,一点反应都没有
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 20:23 , Processed in 0.035136 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表