|
|
发表于 2014-7-9 16:43:27
|
显示全部楼层
1、将下列代码下载到arudino里- String inputString = ""; // a string to hold incoming data
- boolean stringComplete = false; // whether the string is complete
- void setup()
- {
- // initialize serial:
- Serial.begin(9600);
- // reserve 200 bytes for the inputString:
- inputString.reserve(200);
- }
- void loop()
- {
- String ss[4];// print the string when a newline arrives:
- if (stringComplete)
- {
- for(int i=0;i<3;i++)
- {
- ss[i]=inputString.substring(0,inputString.indexOf("-"));
- Serial.println(ss[i]);
- inputString=inputString.substring(inputString.indexOf("-")+1,inputString.length());
- }
- ss[3]=inputString.substring(0,inputString.length()-1);
- Serial.println(ss[3]);
- // clear the string:
- inputString = "";
- stringComplete = false;
- }
- }
- /*
- SerialEvent occurs whenever a new data comes in the
- hardware serial RX. This routine is run between each
- time loop() runs, so using delay inside loop can delay
- response. Multiple bytes of data may be available.
- */
- void serialEvent()
- {
- while (Serial.available()) {
- // get the new byte:
- char inChar = (char)Serial.read();
- // add it to the inputString:
- inputString += inChar;
- // if the incoming character is a newline, set a flag
- // so the main loop can do something about it:
- if (inChar == '\n') {
- stringComplete = true;
- }
- }
- }
复制代码 2、打开arduino的串口监视器,波特率选9600,发送方式选Newline
3、在串口监视器中输入“Command -Pin -Max -Min”后回车,观察返回的值 |
|