Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QCanBusDevice writeFrame fails with error "Cannot write invalid QCanBusFrame"
Forum Updated to NodeBB v4.3 + New Features

QCanBusDevice writeFrame fails with error "Cannot write invalid QCanBusFrame"

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 2 Posters 777 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sachin_Sanghani
    wrote on last edited by
    #1

    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 bus

    ID=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?

    Pablo J. RoginaP 1 Reply Last reply
    0
    • S Sachin_Sanghani

      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 bus

      ID=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?

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @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.

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      S 1 Reply Last reply
      2
      • Pablo J. RoginaP Pablo J. Rogina

        @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.

        S Offline
        S Offline
        Sachin_Sanghani
        wrote on last edited by
        #3

        @Pablo-J-Rogina , Thanks for the replay and the link. I found what was the wrong with code.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • S Sachin_Sanghani

          @Pablo-J-Rogina , Thanks for the replay and the link. I found what was the wrong with code.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @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.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved