Problem reading a message from a QCanBusDevice.
-
Hello i'm working on a qt application using CAN Bus Communication. I communicate with a sensor and have to display in my QML side like the temperature, voltage... Value of those data change very often. I send a message to the systec can device and it respond with data containing informations i need. My problem is that i get nothing even when i connect my reading slot to the QCanBusDevice::frameReceived slot.
I wrote a function deviceProfile process to send a request to read my device profile.
void DevicesManagement::deviceProfileProcess() { char data[8]={0x43,0x00,0x10,0x00,0x00,0x00,0x00,0x00}; QByteArray frame(data,8); QCanBusFrame myframe; myframe.setPayload(frame); myframe.setFrameId(0x617); m_device->writeFrame(myframe); emit deviceProfileProcessed(); }
The function deviceProfile() read the frame send in return to my previous message.
void DevicesManagement::deviceProfile() { QCanBusFrame frame=m_device->readFrame(); QString str=frame.payload().toHex().right(8).toUpper(); set_deviceProfile(str); }
I connect the fonction deviceProfile() to the slot QCanBusFrame::framesReceived and i call deviceProfileProcess on my main() in order to see if the device Profile has been update.
The problem is that i have nothing in return while i can verify that a message has been received.
The connect:connect(m_device,&QCanBusDevice::framesReceived,this,&DevicesManagement::deviceProfile,Qt::UniqueConnection);
Anyone can help?
-
Hi @Babs,
First you should change your receive slot to a loop, to make sure all received messages are read. Because it may be two messages arrived, and you only read the first one as reaction to the QCanBusDevice::framesReceived signal.
void DevicesManagement::deviceProfile() { while (m_device->framesAvailable()) { QCanBusFrame frame=m_device->readFrame(); qDebug() << frame.toString(); } }
Does that give you the correct message as debug output?
Regards
-
@aha_1980 It works when it comes to debug output in the while loop. I save that data on variable and try to debug it in my main window. The data is empty.
while(m_device->framesAvailable()){ QCanBusFrame frame=m_device->readFrame(); QString str=frame.payload().toHex().right(8).toUpper(); set_deviceProfile(str); }
In my main
QGuiApplication app (argc, argv); QQmlApplicationEngine engine (&app); registerQtQmlTricksUiElements (&engine); registerQtQmlTricksSmartDataModel (&engine); DevicesManagement *manager=new DevicesManagement(&engine); manager->initializeDevices("125000"); manager->deviceProfileProcess(); manager->addDevicetoList(23); engine.rootContext()->setContextProperty("mManager",manager); qDebug()<<manager->get_deviceProfile();
-
@aha_1980 Also i have to read a lot of data like the temperature ,voltage and current from the Can device. If i intend to read each time in while loop, isn't it possible i get the wrong data sometimes. There are many other datas that transit on my Network.
-
@Babs said in Problem reading a message from a QCanBusDevice.:
I save that data on variable
Do you mean in set_deviceProfile(str)? Can you show its content?
Also, you should append to the string inside while loop. -
@Babs said in Problem reading a message from a QCanBusDevice.:
There are many other datas that transit on my Network.
Yes, that is typical on such networks. You need to find out the interesting CAN frames (by checking the frameId and the data) and only react on these frames.
All other frames should be ignored.
-
@jsulm For set_deviceProfile i wrote this macro
QML_WRITABLE_VAR_PROPERTY(QString, deviceProfile)
it defines a Q_PROPERTY to expose variable to QML. And define getters and setters for this variable;
Set_deviceProfile(QString deviceProfile) contains:m_deviceProfile=deviceProfile; emit deviceProfileChanged
-
@Babs said in Problem reading a message from a QCanBusDevice.:
@aha_1980 I plan using a circular buffer for that issue.
A circular buffer will not help to filter out messages you are not interested in.
For know i still can't get my variable filled with the data.
But why?
-
@aha_1980 said in Problem reading a message from a QCanBusDevice.:
A circular buffer will not help to filter out messages you are not interested in.
I want to use it to store the messages in order to avoid two device attempting to access to the network at the same time