读取 Arduino 的串口

Posted

技术标签:

【中文标题】读取 Arduino 的串口【英文标题】:Reading over Arduino's Serial port 【发布时间】:2015-11-26 08:33:40 【问题描述】:

我正在尝试使用处理脚本将信号数据转储到 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);
    

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

【问题讨论】:

您确定您的 arduino 已连接到 COM3 吗?编程时可以在控制面板或Arduino IDE中查看吗? 【参考方案1】:

尝试以下我写的经验:

    尝试将 USB 插入其他端口 重启电脑 确保其他软件(如处理)未与 arduino 一起使用 尝试不同的电线 换个电脑试试 你的 arduino 可能被烧毁了(不幸的是)

祝你好运

【讨论】:

【参考方案2】:

好的,所以我设法解决了这个问题,意识到这个问题只发生在串行监视器在机器上运行时。关闭它,Processing 就可以使用通道了。

【讨论】:

以上是关于读取 Arduino 的串口的主要内容,如果未能解决你的问题,请参考以下文章

arduino读取一个block值

arduino如何串口输出变量的值

读取 Arduino 的串口

arduino用来发送串口数组

求助关于arduino串口字符串提取浮点数

Arduino 利用串口缓冲区监听的方式读取数据