阅读Arduino的串口

时间:2015-11-26 08:33:40

标签: arduino serial-port processing

我试图使用Processing脚本在arduino的频道上转储信号数据。目前我的arduino代码通过COM3读取数据,并且我在处理代码中遇到了类似的内容;"打开串口COM3时出错:未找到端口"。 加速度计/陀螺仪信号和处理脚本的arduino代码可以从Sparkfun的网站上找到:

https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/discuss

以下是我正在处理的代码段:

Arduino信号阅读 -

    #include "SparkFunLSM6DS3.h"
    #include "Wire.h"
    #include "SPI.h"

    LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B

    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      delay(1000); //relax...
      Serial.println("Processor came out of reset.\n");
    //  Serial.println(value);

      //Call .begin() to configure the IMU
      myIMU.begin();
      //Over-ride default settings if desired
      myIMU.settings.gyroEnabled = 1;  //Can be 0 or 1
      myIMU.settings.gyroRange = 2000;   //Max deg/s.  Can be: 125, 245
      myIMU.settings.gyroSampleRate = 833;   //Hz.  Can be: 13, 26, 52, 104,  208, 416, 833, 1666
      myIMU.settings.gyroBandWidth = 200;  //Hz.  Can be: 50, 100, 200, 400;
      myIMU.settings.gyroFifoEnabled = 1;  //Set to include gyro in FIFO  
      myIMU.settings.gyroFifoDecimation = 1;  //set 1 for on /1

    myIMU.settings.accelEnabled = 1;
    myIMU.settings.accelRange = 16;      //Max G force readable.  Can be: 2, 4, 8, 16
    myIMU.settings.accelSampleRate = 833;  //Hz.  Can be: 13, 26, 52, 104, 208, 416, 833, 1666, 3332, 6664, 13330
    myIMU.settings.accelBandWidth = 200;  //Hz.  Can be: 50, 100, 200, 400;
    myIMU.settings.accelFifoEnabled = 1;  //Set to include accelerometer in the FIFO
    myIMU.settings.accelFifoDecimation = 1;  //set 1 for on /1
    myIMU.settings.tempEnabled = 1;

    //Non-basic mode settings
    myIMU.settings.commMode = 1;

    //FIFO control settings
    myIMU.settings.fifoThreshold = 100;  //Can be 0 to 4096 (16 bit bytes)
    myIMU.settings.fifoSampleRate = 50;  //Hz.  Can be: 10, 25, 50, 100, 200, 400, 800, 1600, 3300, 6600
    myIMU.settings.fifoModeWord = 6;  //FIFO mode.
    //FIFO mode.  Can be:
    //  0 (Bypass mode, FIFO off)
    //  1 (Stop when full)
    //  3 (Continuous during trigger)
    //  4 (Bypass until trigger)
    //  6 (Continous mode)
    }

    void loop()
    {
      //Get all parameters
      Serial.print("\nAccelerometer:\n");
      Serial.print(" X = ");
      Serial.println(myIMU.readFloatAccelX(), 4);
      Serial.print(" Y = ");
      Serial.println(myIMU.readFloatAccelY(), 4);
      Serial.print(" Z = ");
      Serial.println(myIMU.readFloatAccelZ(), 4);

      Serial.print("\nGyroscope:\n");
      Serial.print(" X = ");
      Serial.println(myIMU.readFloatGyroX(), 4);
      Serial.print(" Y = ");
      Serial.println(myIMU.readFloatGyroY(), 4);
      Serial.print(" Z = ");
      Serial.println(myIMU.readFloatGyroZ(), 4);

      Serial.print("\nThermometer:\n");
      Serial.print(" Degrees C = ");
      Serial.println(myIMU.readTempC(), 4);
      Serial.print(" Degrees F = ");
      Serial.println(myIMU.readTempF(), 4);

      delay(1000);
    }

处理脚本 -

    import processing.serial.*;
    Serial myPort;
    String val;

    void setup() {


      size(500,500);
      //String portName = Serial.list()[2];

      myPort = new Serial(this , "COM3", 9600);

    }
    void draw()
    {
      if (myPort.available() > 0)
      {val = myPort.readStringUntil('\n');}
      println(val);
    }

不确定我的代码实现是否有问题,或者我是否缺少通道兼容性。任何提供的帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试以下我写的经验:

  1. 尝试将USB插入另一个端口
  2. 重启电脑
  3. 确保其他软件(如Processing)未使用arduino
  4. 处于活动状态
  5. 尝试不同的电线
  6. 尝试不同的电脑
  7. 你的arduino可能被烧毁(不幸的是)
  8. 祝你好运

答案 1 :(得分:0)

好的所以我设法通过认识到这个问题仅在串行监视器在计算机上运行时才发生。关闭它后,Processing可以使用该频道。