2

我正在制作一个 arduino 项目,该项目从一些Atlas Scientific探测器中读取一些值。有关于如何使用串行监视器仅与一个设备通信的示例代码,我正在尝试将其移植到“选择”我想使用串行监视器与之交谈的探测器。问题是示例代码只是等待循环()中的串行输入。它看起来像这样:

void loop(){                                                                   //here we go....

  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
 }

所以基本上,你输入一个命令并按回车,它会将命令发送到探测器。探测器发挥了作用,并向您报告了一个值。很简单。我正在尝试使用案例语句来选择我想与之交谈的探测器(还有一个不使用此协议的温度传感器)。我正在尝试在 case 语句中使用 while 循环来与我的外围设备对话。

void loop(){                                                                  
// read the selection
 if (Serial.available()>0) {
   int inByte = Serial.read();
     switch(inByte) {
       case '1':
      //read pH
  Serial.print("pH");
  while (inputstring != "break"){
  if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
  Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
  Serial.print(inputstring);
  inputstring = "";                                                        //clear the string:
  input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
  }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
      }

      case '2':
      //read EC
Serial.print("EC");      
while (inputstring != "break"){
          if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
          Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
          Serial.print(inputstring);                                               //debug
          inputstring = "";                                                        //clear the string:
          input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
          }

     if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
          Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
          sensorstring = "";                                                       //clear the string:
          sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
          }
          }
    .
    .
    .
    }

就像我说的,我正在尝试使用 while 循环来运行与各个传感器的各个终端串行通信,并且能够在终端中键入“break”以返回我的案例语句并选择另一个传感器。while 循环的想法根本不起作用。我添加了调试行来回显我的命令,但它甚至没有这样做。有没有更好的方法来做这些嵌套循环?

这是完整的程序供参考。

/*
This software was made to demonstrate how to quickly get your Atlas Scientific product running on the Arduino platform.
An Arduino MEGA 2560 board was used to test this code.
This code was written in the Arudino 1.0 IDE
Modify the code to fit your system.
**Type in a command in the serial monitor and the Atlas Scientific product will respond.**
**The data from the Atlas Scientific product will come out on the serial monitor.**
Code efficacy was NOT considered, this is a demo only.
The TX3 line goes to the RX pin of your product.
The RX3 line goes to the TX pin of your product.
Make sure you also connect to power and GND pins to power and a common ground.
Open TOOLS > serial monitor, set the serial monitor to the correct serial port and set the baud rate to 38400.
Remember, select carriage return from the drop down menu next to the baud rate selection; not "both NL & CR".
*/
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 30 on the Arduino
#define ONE_WIRE_BUS 32
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress;

void setup(void){                                                                //set up the hardware
    Serial.begin(9600);                                                       //set baud rate for the hardware serial port_0 to 38400
    Serial3.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
    Serial2.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
    inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
    sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific produc
    // locate devices on the bus
    Serial.print("Locating temperature devices...");
    sensors.begin();
    // Grab a count of devices on the wire
    numberOfDevices = sensors.getDeviceCount();
    Serial.print("Found ");
    Serial.print(sensors.getDeviceCount(), DEC);
    Serial.println(" devices.");
     for(int i=0;i<numberOfDevices; i++)
  {
    if(sensors.getAddress(tempDeviceAddress, i))
    {
        sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
        }
  }   
    Serial.println("Type 1 to read pH, 2 to read EC, or 3 to read temperature.");
  }
//for reading pH/EC
void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
               input_stringcomplete = true;
              }  
void serialEvent3(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }
void serialEvent2(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial2.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }

//for reading temp
void printTemperature(DeviceAddress deviceAddress)
{
  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}



void loop(){                                                                   //here we go....
// read the selection
 int r = 0; 
 if (Serial.available()>0) {
   int inByte = Serial.read();
     switch(inByte) {
       case '1':
      //read pH
      Serial.print("pH");
      while (inputstring != "break"){
      if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial.print(inputstring);                                               //debug
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
      }

      case '2':
      //read EC
      Serial.print("EC");
      while (inputstring != "break"){
      if (input_stringcomplete){                                               //if a string from the PC has been recived in its entierty 
      Serial2.print(inputstring);                                              //send that string to the Atlas Scientific product
      Serial.print(inputstring);                                               //debug
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
      }

      case '3':
      // call sensors.requestTemperatures() to issue a global temperature 
      // request to all devices on the bus
      Serial.print("Requesting temperatures...");
      sensors.requestTemperatures(); // Send the command to get temperatures
      Serial.println("DONE");
      // Loop through each device, print out temperature data
      for(int i=0;i<numberOfDevices; i++)
      {
        // Search the wire for address
        if(sensors.getAddress(tempDeviceAddress, i))
        {
        // Output the device ID
        Serial.print("Temperature for device: ");
        Serial.println(i,DEC);

        // It responds almost immediately. Let's print out the data
        printTemperature(tempDeviceAddress); // Use a simple function to print out the data
        } 
    //else ghost device! Check your power requirements and cabling

        }
      break;
    }
   }
} 

谢谢,迈克

4

3 回答 3

0

我不熟悉 Arduino 使用的语言,但看起来你只有一个用于 switch 的 case 3 的 break 语句。如果确实需要休息,这将导致案例 1 和案例 2 流入案例 3。有时这是有用的行为,但我猜在这种情况下不是。

于 2013-07-19T04:31:56.580 回答
0

此示例演示除非使用 break 语句,否则控制如何“通过”:

BOOL fClosing = FALSE;

...

switch( wParam )
{
  case IDM_F_CLOSE:       // File close command.
    fClosing = TRUE;
    // fall through

  case IDM_F_SAVE:        // File save command.
    if( document->IsDirty() )
      if( document->Name() == "UNTITLED" )
        FileSaveAs( document );
      else
        FileSave( document );

    if( fClosing )
      document->Close();

    break;
}

前面的代码展示了如何利用案例标签不阻碍控制流这一事实。如果 switch 语句将控制权转移到IDM_F_SAVEfClosing则为FALSE。因此,文件保存后,文档并没有关闭。但是,如果 switch 语句将控制权转移到IDM_F_CLOSEfClosing则设置为TRUE,并执行保存文件的代码。

于 2013-10-26T15:11:53.423 回答
0

我知道 arduino 的一个很好的库是传感器和 pc 之间的中间件

看看这里 https://code.google.com/p/arduino-serial-packet/

在这里 https://code.google.com/p/arduino-serial-configuration-menu/

于 2013-07-24T13:46:58.837 回答