Menu

Error for the last byte

Help
2016-03-09
2016-03-13
  • FRIKHA Walid

    FRIKHA Walid - 2016-03-09

    Hi,

    I'am using can4linux r518 on a bananapi (I'm using raspbian, bananian and also bananian on a banana pro).

    My problem is very simple, I'm sending 8 bytes with this command:

    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    

    The first message is fine, but for example if the last byte (madonnee.bytes.byte7) is equal to 0x3C, all the message after that will always end with 0x3C even if I send a message like that

    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,0x00,0x01,0x02,0x03' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3)))
    

    So for that exemple, the last 4 bytes will be 0x00,0x01,0x02 and 0x3C

    The problem only appears when I send 8 bytes.

    I don't know how this is possible because if I copy/paste that exact same line in a file where I only send that message it's working fine. The bug only appears when I try to send multiple messages.

    I've attached a draft of my code in python if you want to give it a shot and maybe help me to fin the mistake.

    PS: sorry for my poor english.

    the code:

    # simple pyCan Example for can4linux
    import pyCan
    import time
    import ctypes
    print '....... swig-python wrapper loaded'
    
    # setting the device number
    device = 0
    defaultBaudrate = '250'
    
    # open the can interface /dev/can1
    # but before, set the baud rate if other than default
    try:
        fileBaud = open('/proc/sys/dev/Can/Baud','r+')
        baudRates = fileBaud.read().split()
        baudRates[device] = defaultBaudrate
        fileBaud.write(' '.join(baudRates))
        fileBaud.close()
    except IOError:
        print 'Could not set the new bitrate '
        exit()
    except IndexError:
        print 'No proper entry for bitrate of can'+ str(device) + ' found.'
        exit()
    
    print '....... bit rate changed'
    
    print 'open can'+ str(device) + ' none blocking'
    can_fd = pyCan.open(device)
    
    if can_fd == -1:
         print 'error opening CAN device /dev/can'+ str(device)
         exit()
    
    class byteStruct(ctypes.Structure):
        _fields_ = [("byte0", ctypes.c_ubyte), ("byte1", ctypes.c_ubyte), ("byte2", ctypes.c_ubyte), ("byte3", ctypes.c_ubyte), ("byte4", ctypes.c_ubyte), ("byte5", ctypes.c_ubyte), ("byte6", ctypes.c_ubyte), ("byte7", ctypes.c_ubyte)]
    
    class intStruct(ctypes.Structure):
        _fields_ = [("int0", ctypes.c_short), ("int1", ctypes.c_short), ("int2", ctypes.c_short), ("int3", ctypes.c_short)]
    
    class floatStruct(ctypes.Structure):
        _fields_ = [("float0", ctypes.c_float), ("float1", ctypes.c_float)]
    
    class canData(ctypes.Union):
        _fields_ = [("bytes", byteStruct), ("ints", intStruct), ("floats", floatStruct)]
    
    madonnee=canData()
    
    madonnee.ints.int0=1024
    madonnee.ints.int1=2000
    madonnee.bytes.byte4=0b00000100
    madonnee.bytes.byte5=0b00000001
    ID=hex(0b00000000010)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,0x00,0x00' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5)))
    
    time.sleep(0.001)
    #print(1)
    
    madonnee.floats.float0=0.0015
    madonnee.floats.float1=0.01
    ID=hex(0b00000010010)
    
    #pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    
    time.sleep(0.001)
    #time.sleep(1)
    #print(2)
    
    madonnee.floats.float0=0.1
    madonnee.floats.float1=0.01
    ID=hex(0b00000100010)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    
    print '%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7))
    time.sleep(0.001)
    #time.sleep(1)
    #print(3)
    
    madonnee.floats.float0=15
    madonnee.floats.float1=0
    ID=hex(0b00000110010)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    time.sleep(0.001)
    #time.sleep(1)
    #print(4)
    
    madonnee.floats.float0=30
    madonnee.floats.float1=0
    ID=hex(0b00001000010)
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    #time.sleep(1)
    #print(5)
    
    time.sleep(0.001)
    madonnee.floats.float0=0.00005
    madonnee.floats.float1=0.01
    ID=hex(0b00001010010)
    
    time.sleep(0.001)
    #pyCan.send(can_fd,8,'%d:%d,%d,%d,%d,%d,%d,%d,%d' % (ID,madonnee.bytes.byte0,madonnee.bytes.byte1,madonnee.bytes.byte2,madonnee.bytes.byte3,madonnee.bytes.byte4,madonnee.bytes.byte5,madonnee.bytes.byte6,madonnee.bytes.byte7))
    
    #pyCan.send(can_fd,8,"0x52:0x17,0xB7,0x51,0x38,0x0A,0xD7,0x23,0x3C")
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    
    myvalue = '%s:%s,%s,%s,%s,%s,%s,%s,0x55' % (ID,hex(madonnee.bytes.byte0),madonnee.bytes.byte1,madonnee.bytes.byte2,madonnee.bytes.byte3,madonnee.bytes.byte4,madonnee.bytes.byte5,madonnee.bytes.byte6)
    #print ('%s:%d,%s,%s,%s,%s,%s,%s,%s' % (ID,madonnee.bytes.byte0,madonnee.bytes.byte1,madonnee.bytes.byte2,madonnee.bytes.byte3,madonnee.bytes.byte4,madonnee.bytes.byte5,madonnee.bytes.byte6,madonnee.bytes.byte7))
    #time.sleep(1)
    
    #pyCan.send(can_fd,8,"0x52:0x17,0xB7,0x51,0x38,0x0A,0xD7,0x23,0x3C")
    
    #while 1:
    #    time.sleep(1)
    
    #print(6)
    
    madonnee.floats.float0=0
    ID=hex(0b00001100010)
    
    time.sleep(0.001)
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,0x00,0x01,0x02,0x03' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3)))
    
    #time.sleep(1)
    #print(7)
    
    madonnee.floats.float0=87.92
    madonnee.floats.float1=72
    ID=hex(0b00010000010)#8
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,0x32,0x42' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5)))
    #time.sleep(1)
    #print(8)
    
    print '%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7))
    while 1:
            time.sleep(1)
    
    madonnee.floats.float0=1
    madonnee.floats.float1=0#100
    ID=hex(0b00010010010)#9
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    #time.sleep(1)
    #print(9)
    
    time.sleep(0.001)
    madonnee.ints.int0=0b10000000001
    madonnee.ints.int1=0b10000010001
    ID=hex(0b00010100010)#10
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    #time.sleep(1)
    #print(10)
    
    madonnee.floats.float0=1
    madonnee.floats.float1=1
    ID=hex(0b00011100010)
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    #time.sleep(1)
    #print(11)
    
    madonnee.floats.float0=50
    madonnee.floats.float1=0
    ID=hex(0b00011110010)
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    #time.sleep(1)
    #print(12)
    
    madonnee.floats.float0=800
    madonnee.floats.float1=2
    ID=hex(0b00100000010)
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    #time.sleep(1)
    #print(13)
    
    madonnee.floats.float0=500
    madonnee.floats.float1=4
    ID=hex(0b00100010010)
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    #time.sleep(1)
    #print(14)
    
    madonnee.floats.float0=0.1
    madonnee.floats.float1=0.003
    ID=hex(0b00011010010)
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    #time.sleep(1)
    #print(15)
    
    madonnee.floats.float0=0.000
    madonnee.floats.float1=0.000
    ID=hex(0b000100010010)
    
    time.sleep(0.001)
    
    pyCan.send(can_fd,8,'%s:%s,%s,%s,%s,%s,%s,%s,%s' % (ID,hex(madonnee.bytes.byte0),hex(madonnee.bytes.byte1),hex(madonnee.bytes.byte2),hex(madonnee.bytes.byte3),hex(madonnee.bytes.byte4),hex(madonnee.bytes.byte5),hex(madonnee.bytes.byte6),hex(madonnee.bytes.byte7)))
    
    pyCan.close(can_fd)
    
    exit()
    
     
    • Heinz-Jürgen Oertel

      can you please check:
      - if the can4linux is compiled with the correct settings and the correct settings are used
      - if the python swig library is compiled using the same settings as used for the can4linux driver.

      we have just tested the current code, but not on BananaPI, but on a desktop PC, and could not reproduce your problem.
      here the results of your test:

      Received with ret=1: 1457692844.446517 id=2/0x00000002
      len=8 flags=0x00 : bD ( 8): 00 04 d0 07 04 01 00 00
      Received with ret=1: 1457692844.448726 id=34/0x00000022
      len=8 flags=0x00 : bD ( 8): cd cc cc 3d 0a d7 23 3c
      Received with ret=1: 1457692844.449857 id=50/0x00000032
      len=8 flags=0x00 : bD ( 8): 00 00 70 41 00 00 00 00
      Received with ret=1: 1457692844.452057 id=66/0x00000042
      len=8 flags=0x00 : bD ( 8): 00 00 f0 41 00 00 00 00
      Received with ret=1: 1457692844.454238 id=82/0x00000052
      len=8 flags=0x00 : bD ( 8): 17 b7 51 38 0a d7 23 3c
      Received with ret=1: 1457692844.455344 id=98/0x00000062
      len=8 flags=0x00 : bD ( 8): 00 00 00 00 00 01 02 03
      Received with ret=1: 1457692844.456420 id=130/0x00000082
      len=8 flags=0x00 : bD ( 8): 0a d7 af 42 00 00 32 3c

       
  • FRIKHA Walid

    FRIKHA Walid - 2016-03-12

    I'm sorry but I think there is a mistake

    madonnee.floats.float0=87.92
    madonnee.floats.float1=72
    
    print hex(madonnee.bytes.byte7)
    

    The structure split 87.92 into 4 bytes (from 0 to 3), same goes with 72.
    So this code send me back 0x42, the last bytes is supposed to be 42 but in your code it's 0x3C and that is the problem.

    Maybe I made a mistake in my code (I don't have a lot experience in python) but it seems that you did reproduce the mistake.

    If I print the 8 bytes, 87.92 and 72 I have (0x0A,0xD7,0xAF,0x42,0x00,0X00,0x90,0x42) and you got

    len=8 flags=0x00 : bD ( 8): 0a d7 af 42 00 00 32 3c

    So there is 2 wrong bytes.

    Is it possible for you to print madonne.bytes.byteX to what you got?

     

    Last edit: FRIKHA Walid 2016-03-12
  • Heinz-Jürgen Oertel

    Walid,

    I just tested the Python code on my Bananpi. All works well.
    I now used the can4linux-examples/pyCan-example.py extended with some more pyCan.send() statements.

    typical BananaPi can4linux is compiled without long frame support for CANFD,
    without setting -DCANFD in the Makefile

    /proc/sys/dev/Can/framelength:8

    The same should be valid when compiling the examples
    and of course the Python wrapper.

    you have to install:

    sudo apt-get install swig
    sudo apt-get install python-dev
    

    (for me it is Python 2.7.6-8ubuntu0.2)

    and compile with
    make pyCan USECANFD=

    To test the problem with CAN messages with 8 byte data frame length and last byte 0x3c
    I added some test frames to send, but all in the simple format like
    pyCan.send(can_fd, 8,'0x100:0xaa, 1, 2, 3, 4, 0x11, 0x22, 0x33, 0x3c')

    The result on the bus is:

    1457894182.699766 256/0x00000100 : bD ( 8): 01 02 03 04 11 22 33 00
    1457894182.700709 256/0x00000100 : bD ( 8): 02 02 03 04 11 22 33 3c
    1457894182.701661 256/0x00000100 : bD ( 8): 03 02 03 04 11 22 33 01
    1457894182.702612 256/0x00000100 : bD ( 8): 04 02 03 04 11 22 33 3c
    1457894182.703557 256/0x00000100 : bD ( 8): 05 02 03 04 11 22 33 03
    1457894182.704508 256/0x00000100 : bD ( 8): 06 02 03 04 11 22 33 3c
    1457894182.705462 256/0x00000100 : bD ( 8): 07 02 03 04 11 22 33 04
    1457894182.706404 256/0x00000100 : bD ( 8): 08 02 03 04 11 22 33 3c
    1457894182.707347 256/0x00000100 : bD ( 8): 09 02 03 04 11 22 33 55
    1457894182.708293 256/0x00000100 : bD ( 8): 0a 02 03 04 11 22 33 aa

    As expected from the source code:

    print '... sent message new 8 byte messages'
    pyCan.send(can_fd, 8,'0x100: 1, 2, 3, 4, 0x11, 0x22, 0x33, 0x00')
    pyCan.send(can_fd, 8,'0x100: 2, 2, 3, 4, 0x11, 0x22, 0x33, 0x3c')
    pyCan.send(can_fd, 8,'0x100: 3, 2, 3, 4, 0x11, 0x22, 0x33, 0x01')
    pyCan.send(can_fd, 8,'0x100: 4, 2, 3, 4, 0x11, 0x22, 0x33, 0x3c')
    pyCan.send(can_fd, 8,'0x100: 5, 2, 3, 4, 0x11, 0x22, 0x33, 0x03')
    pyCan.send(can_fd, 8,'0x100: 6, 2, 3, 4, 0x11, 0x22, 0x33, 0x3c')
    pyCan.send(can_fd, 8,'0x100: 7, 2, 3, 4, 0x11, 0x22, 0x33, 0x04')
    pyCan.send(can_fd, 8,'0x100: 8, 2, 3, 4, 0x11, 0x22, 0x33, 0x3c')
    pyCan.send(can_fd, 8,'0x100: 9, 2, 3, 4, 0x11, 0x22, 0x33, 0x55')
    pyCan.send(can_fd, 8,'0x100: 10, 2, 3, 4, 0x11, 0x22, 0x33, 0xaa')
    

    So it seems that
    - you did not compile correctly
    - or your Python code is not correct

     
  • FRIKHA Walid

    FRIKHA Walid - 2016-03-13

    Thank you very much for you explanation. I'll try again.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.