Raspberry Pi上的XBee Linux串行端口 [英] XBee Linux Serial Port on Raspberry Pi

查看:79
本文介绍了Raspberry Pi上的XBee Linux串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个Adafruit XBee 2模块,每个模块都通过1条FTDI电缆(ttyUSB0和ttyUSB1)通过USB集线器连接到Raspberry Pi.我将两个XBee模块分别配置为在同一PAN上,然后尝试在一个循环中读取while循环,并在另一个循环中编写一条简单消息.出于某种原因,我从未从第二个端口读取任何内容.

I have 2 Adafruit XBee 2 Modules each connected with 1 FTDI cable (ttyUSB0 and ttyUSB1)to the Raspberry Pi via a USB hub. I configure both XBee modules independently to be on the same PAN and then I try to read in a while loop with one, and write a simple message in a while loop in the other. For some reason I never read anything from the second port.

这是我正在使用的测试代码.

Here is the test code I am using.

配置:

xbee::xbee(char* usbPort) {

    /*
    -> AT (check if xbee modem is responding)
    <- OK
    -> ATID (get current PAN)
    <- 3332 (default, or something else)
    -> ATID 3137 (set new id)
    <- OK
    -> ATID (check again)
    <- 3137
    -> ATWR (write the change to flash)
    <- OK
    */

    // Sleep for a little bit
    QWaitCondition waitCondition;
    QMutex mutex;

    // Get a util object
    Linuxutils util;

    // Open a serial port
    qDebug() << "Opening Serial Port:" << QString(usbPort);
    char port[] = "ttyUSB1";
    int fd = util.openSerialPort(port);
    qDebug() << "Done opening Serial Port " << QString(usbPort) << ": " << fd;
    int didConfigurePort = util.configureSerialPort(fd,9600);
    qDebug() << "Did configure port successfully? " << didConfigurePort;

    // Receive buffer
    char rxBuffer[24];

    /////////////////////////////////////////////////////////////////////////////
    // Config Mode
    memset(rxBuffer, 0, sizeof(rxBuffer));
    char *msg = "+++";
    qDebug() << "Writing config string to XBee ( +++ )";
    util.writeToSerialPort(fd,msg);
    qDebug() << "XBee written to, waiting for response of 'OK'";
    while (true) {
        int readNumberOfBytes = util.readFromSerialPort(fd,rxBuffer,4096);
        printf("Received ( %d bytes ): %s\n", readNumberOfBytes,rxBuffer);
        break;
    }

    /////////////////////////////////////////////////////////////////////////////
    // AT (check if xbee modem is responding)
    memset(rxBuffer, 0, sizeof(rxBuffer));
    char *msg2 = "AT\n";
    qDebug() << "Check if XBee is responding ( AT )";
    util.writeToSerialPort(fd,msg2);
    qDebug() << "XBee written to, waiting for response of 'OK'";
    while (true) {
        int readNumberOfBytes = util.readFromSerialPort(fd,rxBuffer,4096);
        printf("Received ( %d bytes ): %s\n", readNumberOfBytes,rxBuffer);
        break;
    }

    /////////////////////////////////////////////////////////////////////////////
    // AT (get current PAN ID)
    memset(rxBuffer, 0, sizeof(rxBuffer));
    char *msg3 = "ATID\n";
    qDebug() << "Get XBee PAN ID ( ATID )";
    util.writeToSerialPort(fd,msg3);
    qDebug() << "XBee written to, waiting for response which is integer of current PAN";
    while (true) {
        int readNumberOfBytes = util.readFromSerialPort(fd,rxBuffer,4096);
        printf("Received ( %d bytes ): %s\n", readNumberOfBytes,rxBuffer);
        break;
    }

    /////////////////////////////////////////////////////////////////////////////
    // AT (get current PAN ID <VALUE>)
    memset(rxBuffer, 0, sizeof(rxBuffer));
    char *msg4 = "ATID 3137\n";
    qDebug() << "Check if XBee is responding ( ATID 3137 )";
    util.writeToSerialPort(fd,msg4);
    qDebug() << "XBee written to, waiting for response after telling it to change to PAN 3137";
    while (true) {
        int readNumberOfBytes = util.readFromSerialPort(fd,rxBuffer,4096);
        printf("Received ( %d bytes ): %s\n", readNumberOfBytes,rxBuffer);
        break;
    }

    /////////////////////////////////////////////////////////////////////////////
    // AT (get current PAN ID)
    memset(rxBuffer, 0, sizeof(rxBuffer));
    char *msg5 = "ATID\n";
    qDebug() << "Get XBee PAN ID ( ATID )";
    util.writeToSerialPort(fd,msg5);
    qDebug() << "XBee written to, waiting for response which is integer of current PAN";
    while (true) {
        int readNumberOfBytes = util.readFromSerialPort(fd,rxBuffer,4096);
        printf("Received ( %d bytes ): %s\n", readNumberOfBytes,rxBuffer);
        break;
    }

    /////////////////////////////////////////////////////////////////////////////
    // AT (get current PAN ID <VALUE>)
    memset(rxBuffer, 0, sizeof(rxBuffer));
    char *msg6 = "ATWR\n";
    qDebug() << "Write new settings to XBee Flash ( ATWR )";
    util.writeToSerialPort(fd,msg6);
    qDebug() << "XBee written to, waiting for it to write to flash...";
    while (true) {
        int readNumberOfBytes = util.readFromSerialPort(fd,rxBuffer,4096);
        printf("Received ( %d bytes ): %s\n", readNumberOfBytes,rxBuffer);
        break;
    }

    // Close the file descriptor
    close(fd);

}

阅读:

void xbee::xbeeRead(char* usbPort) {

    // Sleep
    QWaitCondition waitCondition;
    QMutex mutex;
    waitCondition.wait(&mutex, 5000);

    // Utils
    Linuxutils util;

    // File descriptor
    int fd = util.openSerialPort(usbPort);

    // Continually Read
    char buffer[4096];
    while (true) {

        // Sleep
        waitCondition.wait(&mutex, 1000);

        qDebug() << "Waiting for data...";

        int readNumberOfBytes = util.readFromSerialPort(fd,buffer,4096);

        // Print results
        printf("Read ( %d bytes ): %s\n", readNumberOfBytes,buffer);

    }

    // Close
    close(fd);

}

写:

void xbee::xbeeWrite(char *usbPort) {

    // Sleep
    QWaitCondition waitCondition;
    QMutex mutex;
    waitCondition.wait(&mutex, 5000);

    // Utils
    Linuxutils util;

    // File descriptor
    int fd = util.openSerialPort(usbPort);

    // Continually Write
    char *buffer =  "Hello World!\n";
    while (true) {

        // Sleep
        waitCondition.wait(&mutex, 1000);

        int readNumberOfBytes = util.writeToSerialPort(fd,buffer);
        // Print results
        printf("Wrote ( %d bytes ): %s\n", readNumberOfBytes,buffer);

    }

}

我绝对可以配置每个模块-这是配置ttyUSB0的示例输出:

I can definitely configure each module - here is the example output for configuring ttyUSB0:

Input test type:  4 
Opening Serial Port: "/dev/ttyUSB0" 
Done opening Serial Port  "/dev/ttyUSB0" :  6 
Did configure port successfully?  0 
Writing config string to XBee ( +++ ) 
XBee written to, waiting for response of 'OK' 
Received ( 3 bytes ): OK

Check if XBee is responding ( AT ) 
XBee written to, waiting for response of 'OK' 
Received ( 3 bytes ): OK

Get XBee PAN ID ( ATID ) 
XBee written to, waiting for response which is integer of current PAN 
Received ( 5 bytes ): 3137

Check if XBee is responding ( ATID 3137 ) 
XBee written to, waiting for response after telling it to change to PAN 3137 
Received ( 3 bytes ): OK

Get XBee PAN ID ( ATID ) 
XBee written to, waiting for response which is integer of current PAN 
Received ( 5 bytes ): 3137

Write new settings to XBee Flash ( ATWR ) 
XBee written to, waiting for it to write to flash... 
Received ( 3 bytes ): OK

Waiting for data... 

尽管我可以看到我正在一个while循环中写入ttyUSB1设备,但在ttyUSB0上却什么也没收到.

Even though I can see that I am writing to the ttyUSB1 device in one while loop, I am not receiving anything at ttyUSB0.

关于为什么发生这种情况的任何想法?谢谢!

Any ideas as to why this is happening?? Thanks!

推荐答案

看起来您两个XBees仍处于命令模式,这将阻止这些无线电模块之间的任何RF数据传输.除非该行以"AT"命令前缀开头,否则所有发送到XBee的串行数据都将被忽略.

Looks like you still have both XBees in command mode which will prevent any RF data transfer between these radio modules. Any serial data sent to the XBee will be ignored unless the line begins with the "AT" command prefix.

完成每个XBee的配置后,发出"ATCN"命令以退出命令模式(应该没有"OK"响应).
然后,您发送到XBee的任何串行数据都将通过其无线电进行传输,(其他)XBee接收到的RF数据将在其串行端口上输出,以供您的程序读取.

After you are done configuring each XBee, issue the "ATCN" command to exit command mode (there should be no "OK" response).
Then any serial data you send to the XBee will be transmitted by its radio, and the RF data received by the (other) XBee will be output on its serial port for reading by your program.

要在XBee模块上重新进入命令模式,一秒钟(这是默认的保护时间)什么都不应发送到XBee串行端口,发送三个字符串"+++"(三个加号) 1秒钟内),然后再沉默一秒钟. XBee应该以确定"提示进行响应.

To re-enter command mode on an XBee module, nothing should be sent to the XBee serial port for one second (which is the default guard time), send a three character string of "+++" (three plus signs within 1 second), and then silence for another one second. The XBee should respond with an "OK" prompt.

所有这些都是标准的Hayes调制解调器AT命令行为.

All of this is standard Hayes modem AT command behavior.

这篇关于Raspberry Pi上的XBee Linux串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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