USBハブを介してRaspberry Piに1本のFTDIケーブル(ttyUSB0およびttyUSB1)で接続された2つのAdafruit XBee 2モジュールがあります。両方の XBee モジュールを独立して同じ PAN に配置するように構成し、一方を while ループで読み取り、もう一方を while ループで単純なメッセージを書き込もうとします。何らかの理由で、2 番目のポートからは何も読み取れませんでした。
これが私が使用しているテストコードです。
構成、設定:
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 を構成するための出力例を次に示します。
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...
1 つの while ループで ttyUSB1 デバイスに書き込みを行っていることがわかりますが、ttyUSB0 では何も受信していません。
なぜこれが起こっているのかについてのアイデアはありますか?? ありがとう!