使用 Arduino 上的软件串行接收两个端口 [英] Two port receive using software serial on Arduino

查看:83
本文介绍了使用 Arduino 上的软件串行接收两个端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用带有 arduino 板的两个软件串行端口从两个传感器获取数据.我注意到之前可能有人问过类似的问题,但答案表明它无法完成,我完全知道它可以基于此处的示例 (http://arduino.cc/en/Tutorial/TwoPortReceive)!

我使用的是 arduino 以太网.我试图从中获取数据的设备包括来自 sparkfun 的 GPS 和 IMU.

我可以仅使用软件串行端口从任一设备获取数据,但是一旦我添加了第二个软件串行端口,两个端口都将无法工作.我无法使用硬件串口,因为它正被另一台设备使用.

我的代码与示例完全相似:

#include 软件串口一个(7,8);软件串口二(5,6);无效设置(){Serial.begin(9600);portOne.begin(9600);portTwo.begin(9600);}空循环(){portOne.listen();而 (portOne.available() > 0) {char inByte = portOne.read();Serial.write(inByte);}延迟(500);portTwo.listen();而 (portTwo.available() > 0) {char inByte = portTwo.read();Serial.write(inByte);}Serial.println();}

有人有什么想法吗?

解决方案

此代码将无法运行,或者如果它运行正常,将无法正常运行.SoftwareSerial 只有一个内部缓冲区.是的,您可以存在多个 SoftwareSerial 对象,但只有其中一个控制内部缓冲区.当任何 RX 引脚被置位时,就会产生一个中断,但只有监听 () 的 RX 引脚会被检查是否有起始位.

真正需要的是当中断从起始位出现时检查多个引脚的能力.然后您必须设置指向适当数据结构的指针.这会很复杂,但可能.

或者可能只是放弃中断驱动接收,并检查两个/所有 RX 引脚,然后根据您看到的引脚启动接收.请注意,此代码有很多头发,您将需要示波器才能使其工作.

我遇到了类似的问题,这就是我找到您的传感器的原因.在与我的同事讨论之后,我们决定按轮换顺序读取我们的传感器.我们的传感器报告传感器的当前状态,而不是特定事件,所以如果我们丢失一些报告也没关系.因此,我们将从端口 1 读取,然后从端口 2 读取,然后是端口 1,依此类推.我们的传感器会输出文本行,因此我们知道何时切换到下一个传感器.

i am having trouble getting data from two sensors using two software serial ports with an arduino board. I noticed a similar question might have been asked before but the answers suggest it can't be done and I know fully well it can based on the example here (http://arduino.cc/en/Tutorial/TwoPortReceive)!

I am using an arduino ethernet. The devices I am trying to get data from include a GPS and an IMU both from sparkfun.

I can get data from either devices using just on software serial port but as soon as I add the second software serial port, neither ports will work. I can't use the hardware serial port because that is being used byt another device.

My code is exactly similar to the example:

#include <SoftwareSerial.h>

SoftwareSerial portOne(7,8);
SoftwareSerial portTwo(5,6);

void setup()
{
  Serial.begin(9600);


  portOne.begin(9600);
  portTwo.begin(9600);
}

void loop()
{

  portOne.listen();
  while (portOne.available() > 0) {
    char inByte = portOne.read();
    Serial.write(inByte);
  }

  delay(500);

  portTwo.listen();
  while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
  }

  Serial.println();
}

Anyone with any ideas?

解决方案

This code will not work, or will work poorly if it works at all. SoftwareSerial only has one internal buffer. Yes, you can have multiple SoftwareSerial objects in existence, but only one of them controls the internal buffer. When any RX pin gets asserted, that generates an interrupt, but only the listen()ing RX pin gets checked for a start bit.

What's really needed is the ability to check on multiple pins when an interrupt comes along from the start bit. Then you'd have to set up pointers to the appropriate data structures. It would be complicated, but possible.

Or maybe just give up on interrupt-driven reception, and spin on checking both/all of the RX pins, and start the receive based on the pin you see. Be forwarned that this code has much hair, and you WILL need an oscilloscope to make it work.

I'm having a similar problem, which is why I found your sensor. After talking it over with my co-workers, we've decided to read our sensors in rotating order. Our sensors report the current state of the sensor, and not specific events, so it's okay if we lose some reports. So we'll read from port 1, then read from port 2, then port 1, etc. Our sensors spit out lines of text, so we know when to switch to the next sensor.

这篇关于使用 Arduino 上的软件串行接收两个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆