我正在尝试使用 C++ 与 LEGO Mindstorms EV3 积木进行通信。我克隆了ev3sources repo,它允许我通过蓝牙做到这一点 - 例如,启动连接到端口 A 的电机,我们可以这样做:
#include <unistd.h>
#include <fcntl.h>
#include "ev3sources/lms2012/c_com/source/c_com.h"
int main()
{
// start motor on port A at speed 20
unsigned const char start_motor[] {12, 0, 0, 0,
DIRECT_COMMAND_NO_REPLY,
0, 0,
opOUTPUT_POWER, LC0(0), LC0(0x01), LC0(20),
opOUTPUT_START, LC0(0), LC0(0x01)};
// send above command to EV3 via Bluetooth
int bt = open("/dev/tty.EV3-SerialPort", O_WRONLY);
write(bt, start_motor, 14);
close(bt);
}
但是如何从 EV3 程序块中取回数据?例如,假设我想读取连接到端口 1 的任何传感器捕获的数据。根据repo 示例,我知道我需要一些看起来像这样的东西:
#include <unistd.h>
#include <fcntl.h>
#include "ev3sources/lms2012/c_com/source/c_com.h"
int main()
{
// read sensor on port 1
unsigned const char read_sensor[] {11, 0, 0, 0,
DIRECT_COMMAND_REPLY,
0, 0,
opINPUT_READ, LC0(0), LC0(0), LC0(0), LC0(0), GV0(0)};
// send above command to EV3 via Bluetooth
int bt = open("/dev/tty.EV3-SerialPort", O_WRONLY);
write(bt, read_sensor, 13);
close(bt);
}
但是缺少一些东西 - 上面的代码片段没有返回任何错误,但我不知道传感器数据在哪里。那么,我该如何找回呢?我想它也是通过蓝牙发回的,但我该如何捕捉呢?
(OS X 10.9.3、Xcode 5.1.1、EV3 [31313])