send message with Peak can bus
-
Hello guys,
i am trying to send a canbus message to a motor using the usb peak device but it doesn't work and I can't figure out where I’am wrong.
The operating system is Windows 10 with QT5.9.9 and Peak driver 4.2.
I am attaching the short code that I have implemented:
#include <QCoreApplication>
#include <windows.h>
#include <iostream>
#include <QCanBus>
#include <QCanBusDevice>
#include <QCanBusFrame>
#include <QByteArray>
#include <QDebug>
#include <QString>
#include <QThread>using namespace std;
int canID;
bool frameWritten;
unsigned char ch;
char *msg;
unsigned int size = 8;int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);if (QCanBus::instance()->plugins().contains(QStringLiteral("peakcan"))) { qDebug() << "plugin available"; } QString errorString; QCanBusDevice *device = QCanBus::instance()->createDevice( QStringLiteral("peakcan"), QStringLiteral("usb0"), &errorString); if (!device) { // Error handling goes here qDebug() << errorString;
} else {
device->connectDevice();
device->setConfigurationParameter(device->BitRateKey, 1000000);
}canID = 0x141; QCanBusFrame roxFrame; roxFrame.setFrameId(canID); roxFrame.setFrameType(QCanBusFrame::DataFrame); QByteArray payload(size, ch); payload.data()[0] = 0xA4; payload.data()[1] = 0x00; payload.data()[2] = 0x90; payload.data()[3] = 0x01; payload.data()[4] = 0xFF; payload.data()[5] = 0xFF; payload.data()[6] = 0xFF; payload.data()[7] = 0xFF; roxFrame.setPayload(payload); frameWritten = device->writeFrame(roxFrame); cout << "Hello World!" << endl; cout << "Rox6Aconsole" << endl; cout << "frameWritten = " << frameWritten << endl; return a.exec();
}
the same message sent with Peak-view works. Can you help me?
-
@Rossano said in send message with Peak can bus:
device->connectDevice();
You should check what this returns.
If it returns false check what https://doc.qt.io/qt-5/qcanbusdevice.html#errorString tells you. -
@Rossano I'm not talking about compilation. Also how is frameWritten related to device->connectDevice()?
Please check what device->connectDevice() returns (true or false?). If it returns false check what https://doc.qt.io/qt-5/qcanbusdevice.html#errorString returns. -
does this work?
#QCanBusFrame roxFrame; #roxFrame.setFrameId(canID); #roxFrame.setFrameType(QCanBusFrame::DataFrame); QByteArray payload; payload[0] = 0xA4; payload[1] = 0x00; payload[2] = 0x90; payload[3] = 0x01; payload[4] = 0xFF; payload[5] = 0xFF; payload[6] = 0xFF; payload[7] = 0xFF; frameWritten = device->writeFrame(canID, payload);
-
Hi Pablo, I tried to write the array as you indicated (without round brackets), but it gives me compile error -> C: \ Users \ tre-ros \ Documents \ Rox6Aconsole \ main.cpp: 73: error: invalid types' < unresolved overloaded function type> [int] 'for array subscript
73 | payload.data [0] = 0xA4; -
@Rossano said in send message with Peak can bus:
Hi Pablo, I tried to write the array as you indicated
Just in case, I suggested to run the Qt CAN example...