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

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

问题描述

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

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

我可以仅使用软件串行端口从任一设备获取数据,但是一旦添加第二个软件串行端口,这两个端口均无法使用.我不能使用硬件串行端口,因为该端口正被另一台设备使用.

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

#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();
}

有什么想法吗?

解决方案

此代码将无法正常工作,或者如果完全无法正常工作,则将无法正常工作. 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天全站免登陆