Problem reading a message from a QCanBusDevice.
-
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