¼¼Êõ²©¿Í

    06/01/2023 ×÷Õß ÔÆº£ÓÎÏ·

    KVASER CANlib & PythonµÚ3²¿·Ö£ºU100ºÍPython

    ÔÚÕâ¸ö¼ò¶ÌµÄÖ¸ÄÏÖУ¬ÎÒÃǽ«½éÉÜU100½Ó¿ÚÉϵÄLEDµÄ¹¤×÷Ô­ÀíºÍʹÓ÷½Ê½¡£

    ×÷ÕߣºAnton Carlsson£¬Èí¼þ¿ª·¢ÈËÔ±

    ¹²Í¬×÷Õߣº?L-G Fredriksson£¬ÏÖ³¡Ó¦Óù¤³Ìʦ

    °æ±¾£º?2021-08-09A

    ±¾ÎĵÄÄ¿±ê¶ÁÕßÓÐÄÄЩ£¿

    ±¾Ö¸ÄÏÊÊÓÃÓÚÄÇЩϣÍûͨ¹ýCANlibºÍcanlib Python°üÉèÖúÍʹÓÃÔÆº£ÓÎÏ· CAN½Ó¿ÚµÄÓû§¡£

    ÔÚʹÓÃÕâ¸öÖ¸ÄÏǰ£¬Äú±ØÐëÓµÓÐÔÆº£ÓÎÏ· U100½Ó¿Ú¡£

    ÔÚʹÓÃÕâ¸öÖ¸ÄÏ֮ǰ£¬ÎÒÃǽ¨ÒéÄúÔĶÁÒÔÏÂÄÚÈÝ£º

    ¿ìËÙÈëÃÅÓë¹Ø¼üÃüÁî

    Èç¹ûÄúÖжÏÁ˲Ù×÷»òÍü¼ÇÁËʹÓõÄÃüÁî/Á÷³Ì£¬ÇëʹÓÃÒÔÏÂÇ¿´óµÄÃüÁîºÍ˵Ã÷Áбí¡£

    • Æô¶¯Windows Powershell£º
      powershell.exe
    • Çл»µ½Äú´æ·ÅPython½Å±¾µÄĿ¼£ºcd£¨Èç¹ûÄúÒѾ­ÓÐÒ»¸öÐéÄâ»·¾³²¢ÇÒ¾ßÓÐÔËÐÐȨÏÞ£¬ÔòÌø¹ý½ÓÏÂÀ´µÄÁ½¸ö²½Ö裩
    • ´´½¨Ò»¸öÐéÄâ»·¾³£º
      py -3 -m venv .venv ¨Cprompt .
    • Èç¹ûÄúÊÇʹÓÃмÆËã»ú»òûÓÐȨÏÞµÄÐÂÓû§£º
      Set-ExecutionPolicy Unrestricted -Scope CurrentUser
    • ¼¤»îÐéÄâ»·¾³£º
      .\.venv\Scripts\activate
    • Èç¹ûÄúÉÐδÔÚ´ËÐéÄâ»·¾³Öа²×°canlib£¬ÇëÖ´ÐÐÒÔϲÙ×÷£º
      pip install canlib
    • ¡­
    • ÔËÐнű¾£ºpy check_ch.py£¨pyÒ»Ö±¿ÉÒÔÔËÐУ¬ÒÔÈ·±£ËùÐèµÄ½Ó¿ÚÒÑÁ¬½Ó£©
    • Æô¶¯±à¼­Æ÷£º
      python -m idlelib
      python -m idlelib check_ch.py
    • ¡­
    • Í£ÓÃÐéÄâ»·¾³£º
      deactivate

    ÔÆº£ÓÎÏ· U100½Ó¿Ú

    Óëǰ¼¸´úÔÆº£ÓÎÏ·½Ó¿ÚÏà±È£¬U100½øÐÐÁËһЩ¸üС£³ýÁË±ÈÆäËûÉ豸¸ü¼Ó¼á¹ÌÖ®Í⣬×îÃ÷ÏԵı仯ÊǽӿÚÉϵÄLED¡£²»½öLEDÔڳߴçºÍÐÎ×´ÉÏ·¢ÉúÁ˱仯£¬ËüÃǵŤ×÷·½Ê½Ò²×ö³öÁ˵÷Õû¡£ÕâЩ±ä»¯Ê¹Óû§¸üÈÝÒ׿´µ½LED£¬²¢´ÓLED»ñÈ¡ÓйØÉ豸µÄ¸ü¶àÐÅÏ¢¡£

    Ê×ÏÈ£¬Ö®Ç°µü´úµÄCAN½Ó¿ÚÓÐÁ½¸öLEDÓÃÓÚPW£¬Ã¿¸ö¿ÉÓõÄCANͨµÀÉϸ÷ÓÐÁ½¸öLED¡£È»¶ø£¬U100Ö»ÓÐÁ½¸öLEDÌõ£¬ÓÃÓÚÉ豸ÉÏΨһ¿ÉÓõÄCANͨµÀ¡£Ò»¸öLEDÌõÓÃÓÚTX£¬ÁíÒ»¸öLEDÌõÓÃÓÚRX¡£È»¶ø£¬ÕâЩLED¾ßÓиü¶àµÄ¹¦ÄÜ£¬¿ÉÒÔÏÔʾ¶àÖÖÑÕÉ«¡£

    U100 LEDs

    ÓëÒÔǰµÄÉ豸ÉÏÓµÓеÄÈýÖÖÑÕÉ«£¨»ÆÉ«¡¢ºìÉ«ºÍÂÌÉ«£©²»Í¬£¬U100¾ßÓÐÎåÖÖÑÕÉ«¡£ÕâЩÑÕÉ«°üÀ¨£º»ÆÉ«¡¢ÂÌÉ«¡¢ºìÉ«¡¢°×É«ºÍÀ¶É«¡£LEDÌõ¾ßÓÐÁ½¸ö×´Ì¬ÇøÓò£¬ÈçͼËùʾ£¬Ã¿ÌõָʾµÆÓÐÒ»¸ö×´Ì¬ÇøÓò¡£

    µ±Ê×´ÎÁ¬½ÓU100ʱ£¬Á½¸öLEDÌõ»áÏÈ»ºÂýÉÁ˸һ´ÎÂ̹⣬Ȼºó×´Ì¬ÇøÓò»áÁÁÆð°×¹â¡£µ±Í¨µÀÉÏÏßʱ£¬°×É«×´Ì¬ÇøÓò½«±äΪÂÌÉ«¡£µ±U100½ÓÊÕ»ò·¢Ëͱ¨ÎÄʱ£¬ÆäÖÐÒ»¸ö×´Ì¬ÇøÓò½«ÉÁ˸»ÆÉ«¡£Èç¹ûÉ豸½ÓÊÕµ½±¨ÎÄ£¬RX×´Ì¬ÇøÓò½«ÉÁ˸»ÆÉ«¡£Èç¹ûÉ豸·¢Ëͱ¨ÎÄ£¬TX×´Ì¬ÇøÓò½«ÉÁ˸»ÆÉ«¡£Èç¹ûÔÚ´«Êä¹ý³ÌÖгöÏÖ´íÎó£¬LED½«ÔÚÒ»¶¨³Ì¶ÈÉÏÏÔʾÎÒÃǽÓÊÕµ½µÄ´íÎóÀàÐÍ¡£Èç¹ûÎÒÃdz¢ÊÔÔÚ×ÜÏßÉÏûÓÐÆäËûͨµÀµÄÇé¿öÏÂʹÓÃU100·¢Ëͱ¨ÎÄʱ£¬Í¨µÀ½«½øÈë´íÎó±»¶¯×´Ì¬£¬Á½¸ö×´Ì¬ÇøÓò¶¼»áÉÁ˸ºìÉ«¡£´ËÍ⣬ÓÉÓÚͨµÀÕýÔÚ´«Êä´íÎóÖ¡£¬TX LEDÌõ½«±äΪºìÉ«¡£Èç¹ûÎÒÃdz¢ÊÔÒÔ²»Æ¥ÅäµÄ±ÈÌØÂʶÁÈ¡±¨ÎÄ£¬×´Ì¬ÇøÓòÈÔ½«ÉÁ˸ºìÉ«¡£µ«ÊÇÓÉÓÚÎÒÃÇÏÖÔÚÕýÔÚ½ÓÊÕ´íÎóÖ¡£¬RX LEDÌõ½«±äΪºìÉ«£¬¶ø²»ÊÇTXµÆÌõ¡£

    Èç¹ûÎÒÃÇÔö¼Ó×ÜÏ߸ºÔØ£¨Ôö¼Ó·¢Ë͵ı¨ÎÄÊýÁ¿£©£¬LEDָʾµÆÒ²»áËæÖ®ÁÁÆð¡£¸ù¾Ý×ÜÏ߸ºÔصIJ»Í¬£¬´ÓTX»òRX LEDµÄ×´Ì¬ÇøÓò¿ªÊ¼£¬¸ü¶àµÄLED½«ÁÁÆð»Æ¹â£¬ÕâÈ¡¾öÓÚÎÒÃÇÊÇÔÚ·¢ËÍ»¹ÊǽÓÊÕ¡£Òª²âÊÔLED£¬ÎÒÃÇ¿ÉÒÔ±àдÒÔϽű¾²¢½«ÆäÃüÃûΪu100.py¡£

    > .\.venv\Scripts\activate

    (pyproj)> py .\u100.py

    import time
    from canlib import canlib, Frame
    
    # First create two frames with different id:s.
    frame_a = Frame(id_=1, data=[72, 69, 76, 76, 79, 33])
    frame_b = Frame(id_=2, data=[72, 69, 76, 76, 79, 33])
    
    # Second, open two channels, one for the U100 and one for another interface.
    # In this example the U100 is connected to channel 3 and a USBcan is
    # connected to channel 2. And set their bitrate.
    ch_a = canlib.openChannel(channel=2, bitrate=canlib.Bitrate.BITRATE_100K)
    ch_b = canlib.openChannel(channel=3, bitrate=canlib.Bitrate.BITRATE_100K)
    
    #set both channels to normal and go on bus.
    ch_a.setBusOutputControl(canlib.Driver.NORMAL)
    ch_b.setBusOutputControl(canlib.Driver.NORMAL)
    ch_a.busOn()
    ch_b.busOn()
    
    # To get a clearer sense of what happens, you will need to press enter between
    # each test.
    input("Press enter to continue")
    # In the first example we will transmit messages with the U100. In the
    # second example we will receive messages with the U100. And in the third
    # and last example we will both send and receive messages with the U100.
    # In all examples we will send 50 messages, you can test to increase this
    # number to see its effect.
    for i in range(50):
    # We will make use of the try function during these tests. The program will
    # try to execute the commands within the try-block (the indented region
    # underneath). If the program encounters an CanGeneralError (indicated by
    # the except command) the program will instead execute the commands within
    # the except block. In this example the messages might be sent to quickly
    # and result in a CanGeneralError which is why we will wait for 0.1 seconds
    # with time.sleep if this happens.
        try:
            # This is the try-block.
            ch_b.write(frame_b)
            ch_a.read(1000)
        except canlib.exceptions.CanGeneralError:
            # This is the except block.
            time.sleep(0.1)
    
    input("Press enter to continue")
    
    for i in range(50):
        try:
            ch_a.write(frame_a)
            ch_b.read(1000)
        except canlib.exceptions.CanGeneralError:
            time.sleep(0.1)
    
    input("Press enter to continue")
    
    for i in range(50):
        try:
            ch_b.write(frame_b)
            ch_a.read(1000)
            ch_a.write(frame_a)
            ch_b.read(1000)
        except canlib.exceptions.CanGeneralError:
            time.sleep(0.1)
    
    ch_a.close()
    ch_b.close()

    ÔÚ²âÊÔ¹ý³ÌÖУ¬TXºÍRXµÄLED½«ÁÁÆð¡£Èç¹ûÎÒÃÇÔö¼Ó±¨ÎĵÄÊýÁ¿£¨ÔÚÖ§³Ö·¶Î§ÄÚµÄÊýÁ¿£©£¬×ÜÏ߸ºÔؾͻáÔö¼Ó£¬¸ü¶àµÄLEDµÆ½«±ä³É»ÆÉ«¡£

    Author Image

    Logan Via

    ¡¾ÍøÕ¾µØÍ¼¡¿¡¾sitemap¡¿