繁体   English   中英

如何从Arduino Xbee发送数据并从C#接收数据

[英]How to send data from Arduino Xbee and receive it from C#

我有一个Arduino XBee 防护板 ,还有一个Sparkfun XBee USB资源管理器。 我想发送来自Ardunio XBee的数据(温度传感器)并在我的C#程序中接收它。

现在,假设我要向C#程序发送45、100。

我没有收到来自XBee防护板的任何数据。 我在代码中缺少任何内容吗?

以下代码是Arduino XBee屏蔽的发送方:

SoftwareSerial mySerial(4,5);
void setup()
{
    mySerial.begin(9600);
}


void loop()
{
    if (mySerial.available() > 0)
    {
        mySerial.write(45);
        mySerial.write(',');
        mySerial.write(100);
        mySerial.write('\n');
    }
}

C#中的USB XBee Explorer的接收器代码:

SerialPort port = new SerialPort();

public Form1()
{
    try
        {
            port.PortName = "COM8";
            port.BaudRate = 9600;
            port.DataBits = 8;
            port.Parity = Parity.None;
            port.StopBits = StopBits.One;
            port.Open();
            Console.WriteLine("Opened");
        }
        catch(Exception ex)
        {
            Console.WriteLine("Sorry! " + ex);
        }

        // Handler for receiving data
        port.DataReceived += serialPort1_DataReceived;
    }

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        if (port.IsOpen == true)
        {
            string RxString = port.ReadLine();
            Console.WriteLine(RxString);
        }
    }

XBee配置:

  • 一个XBee是:Coordinator AT模式-连接到USB Sparkfun Explorer
  • 另一个XBee是:路由器AT模式-连接到Arduino屏蔽板

当tomlogic同时在Stack Overflow问题XBee双向通信(发送方和接收方)中回答了我的问题

我知道了 问题出在我的void loop()方法上。 mySerial应该像

mySerial.println(temperature);
  • 但是,您必须检查mySerial Rx,Tx引脚是否正确

您的XBee防护罩使用Arduino上的引脚0和1。 不需要软件串行,只需使用:

Serial.begin(9600); // In void setup() routine

要发送温度,请在功能loop使用以下命令:

Serial.print(temperature); // Need a variable 'temperature' of course...

使用Arduino IDE中的内置终端测试Arduino代码,以查看端口是否实际可以接收和发送(首先移除XBee防护板)。 完成后,测试XBee通信。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM