VectorCAN插件封装了低级API,以便与Vector Informatik CAN适配器一起工作。
这个插件需要Vector CAN设备驱动程序和vxlapi64.dll(64位构建的vxlapi64.dll)。
Creating CAN Bus Devices
首先,有必要检查QCanBus是否提供所需的插件:
if (QCanBus::instance()->plugins().contains(QStringLiteral("vectorcan"))) {
// plugin available
}
其中vectorcan为插件名称。
接下来,可以建立到特定接口的连接:
QString errorString;
QCanBusDevice *device = QCanBus::instance()->createDevice(
QStringLiteral("vectorcan"), QStringLiteral("can0"), &errorString);
if (!device) {
// Error handling goes here
qDebug << errorString;
} else {
device->connectDevice();
}
其中can0为活动CAN通道名。VectorCAN插件提供了从can0到can63的64个通道(由Vector API中的XL_CONFIG_MAX_CHANNELS定义)。其中一些通道可以是虚拟的,因此可以在没有实际can硬件的情况下使用。要找出虚拟通道,可以使用“Vector Hardware Config”(vcanconf.exe)程序,该程序包含在Vector的驱动程序包中。availableDevices()方法返回当前可用设备的列表。
设备现在可以读写CAN帧:
QCanBusFrame frame;
frame.setFrameId(8);
QByteArray payload("A36E");
frame.setPayload(payload);
device->writeFrame(frame);
读取可以使用readFrame()方法完成。当至少有一个新的帧可供读取时,就会发出framesReceived()信号:
QCanBusFrame frame = device->readFrame();
VectorCAN支持以下配置,可以通过setConfigurationParameter()来控制:
Configuration parameter key | Description |
---|---|
QCanBusDevice::BitRateKey | 确定CAN总线连接的比特率。 |
QCanBusDevice::ReceiveOwnKey | 缺省情况下,发送CAN帧的同一设备不能接收CAN帧。当启用此选项时,所有发送到CAN总线的CAN帧立即出现在接收缓冲区中。这可以用来检查发送是否成功。如果启用了这个选项,那么接收到的帧将被标记为QCanBusFrame::hasLocalEcho() |
VectorCAN支持以下附加函数:
QCanBusDevice::busStatus()