QCanBusDevice writeFrame fails with error "Cannot write invalid QCanBusFrame"
-
Hi all,
I am using Linux socket APIs to read and write data over can bus.
Here is my code for that
QStringList cmdlist; cmdlist.append( QString( "ip link set %1 type can bitrate %2" ).arg( "can0", QString::number( 500000)) ); cmdlist.append( QString( "ip link set %1 up" ).arg( "can0" ) ); if( system( cmdlist.first().toLocal8Bit() ) < 0 ) { qDebug() << "Failed to set CAN bus name and Bitrate"; return -1; } if( system( cmdlist.at( 1 ).toLocal8Bit() ) < 0 ) { qDebug() << "Failed to set up CAN bus" << m_can0Attribute.busName; return -1; } /** set the socket attribute and bind the socket */ m_can0Attribute.fd = socket( PF_CAN, SOCK_RAW, CAN_RAW ); strncpy( m_can0Attribute.ifr.ifr_name, m_can0Attribute.busName, 4 ); ioctl( m_can0Attribute.fd, SIOCGIFINDEX, &m_can0Attribute.ifr ); m_can0Attribute.addr.can_family = AF_CAN; m_can0Attribute.addr.can_ifindex = m_can0Attribute.ifr.ifr_ifindex; errorCode = bind( m_can0Attribute.fd, (struct sockaddr *)&m_can0Attribute.addr, sizeof(m_can0Attribute.addr) ); if( errorCode < 0 ) { qDebug() << "Failed to bind CAN bus socket with error = " << errorCode; return errorCode ; } /** Set CAM loopback state = 0 */ errorCode = setsockopt( m_can0Attribute.fd, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &m_can0Attribute.loopback, sizeof( m_can0Attribute.loopback ) ); if( errorCode < 0 ) { qDebug() << "Failed to Set Socket Option for CAN bus socket with error = " << errorCode; return errorCode ; }
But now, I want to use QCanBusDevice to send and receive can data. I have used the CAN Bus example code to test the functionality.
I want to send the below frame over the can busID=18EFF221, LEN=8, Data=0x11, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF
After setting up the code, I am stuck at the writeFrame part. Here is my frame which I want to send over can bus.
QCanBusFrame frame = QCanBusFrame{ ( 0x18EFF221U | 0x80000000U ), QByteArray::fromHex("11030000000000FF" ) };
Here is what I received on the console
Connected, state is: QCanBusDevice::CanBusDeviceState(ConnectedState) SendFrame Status = false Error = "Cannot write invalid QCanBusFrame"
Can someone please help me to figure out what is wrong here?
-
@Sachin_Sanghani said in QCanBusDevice writeFrame fails with error "Cannot write invalid QCanBusFrame":
Can someone please help me to figure out what is wrong here?
You may want to check Qt source code where that message appears.
In particular the definition of isValid() method.
-
@Pablo-J-Rogina , Thanks for the replay and the link. I found what was the wrong with code.
-
@Sachin_Sanghani said in QCanBusDevice writeFrame fails with error "Cannot write invalid QCanBusFrame":
I found what was the wrong with code.
Great. So could it be possible you share with the community some explanation about the wrong code (or a code snippet perhaps). Thanks.