GithubHelp home page GithubHelp logo

jxltom / gs_usb Goto Github PK

View Code? Open in Web Editor NEW
21.0 4.0 12.0 66 KB

Python Windows/Linux CAN driver based on WCID for Geschwister Schneider USB/CAN devices and candleLight USB CAN interfaces

Python 100.00%
can gs-usb candlelight wcid

gs_usb's Introduction

gs_usb

Python Windows/Linux/Mac CAN driver based on usbfs or WinUSB WCID for Geschwister Schneider USB/CAN devices, candleLight USB CAN interfaces, CAN Debugger devices and other interfaces utilising the gs_usb driver.

Getting Started

Install by pip install gs_usb

Below is a basic demo for CAN message sending and receiving.

import time

from gs_usb.gs_usb import GsUsb
from gs_usb.gs_usb_frame import GsUsbFrame
from gs_usb.constants import (
    CAN_EFF_FLAG,
    CAN_ERR_FLAG,
    CAN_RTR_FLAG,
)


def main():
    # Find our device
    devs = GsUsb.scan()
    if len(devs) == 0:
        print("Can not find gs_usb device")
        return
    dev = devs[0]

    # Configuration
    if not dev.set_bitrate(250000):
        print("Can not set bitrate for gs_usb")
        return

    # Start device
    dev.start()

    # Prepare frames
    data = b"\x12\x34\x56\x78\x9A\xBC\xDE\xF0"
    sff_frame = GsUsbFrame(can_id=0x7FF, data=data)
    sff_none_data_frame = GsUsbFrame(can_id=0x7FF)
    err_frame = GsUsbFrame(can_id=0x7FF | CAN_ERR_FLAG, data=data)
    eff_frame = GsUsbFrame(can_id=0x12345678 | CAN_EFF_FLAG, data=data)
    eff_none_data_frame = GsUsbFrame(can_id=0x12345678 | CAN_EFF_FLAG)
    rtr_frame = GsUsbFrame(can_id=0x7FF | CAN_RTR_FLAG)
    rtr_with_eid_frame = GsUsbFrame(can_id=0x12345678 | CAN_RTR_FLAG | CAN_EFF_FLAG)
    rtr_with_data_frame = GsUsbFrame(can_id=0x7FF | CAN_RTR_FLAG, data=data)
    frames = [
        sff_frame,
        sff_none_data_frame,
        err_frame,
        eff_frame,
        eff_none_data_frame,
        rtr_frame,
        rtr_with_eid_frame,
        rtr_with_data_frame,
    ]

    # Read all the time and send message in each second
    end_time, n = time.time() + 1, -1
    while True:
        iframe = GsUsbFrame()
        if dev.read(iframe, 1):
            print("RX  {}".format(iframe))

        if time.time() - end_time >= 0:
            end_time = time.time() + 1
            n += 1
            n %= len(frames)

            if dev.send(frames[n]):
                print("TX  {}".format(frames[n]))


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass

gs_usb's People

Contributors

bennyevans avatar imanolbarberia avatar jxltom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gs_usb's Issues

'GS_USB_MODE_NO_ECHO_BACK' flags seems not work

New discovery:
candleLight_fw compatible devices only support this OPEN Flags:
`
https://github.com/candle-usb/candleLight_fw/blob/master/include/gs_usb.h

#define GS_CAN_MODE_NORMAL 0
#define GS_CAN_MODE_LISTEN_ONLY (1<<0)
#define GS_CAN_MODE_LOOP_BACK (1<<1)
#define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2)
#define GS_CAN_MODE_ONE_SHOT (1<<3)
#define GS_CAN_MODE_HW_TIMESTAMP (1<<4)

#define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE (1<<7)
and in Linux Kernel 5.12 master,
https://github.com/torvalds/linux/blob/master/drivers/net/can/usb/gs_usb.c

#define GS_CAN_MODE_NORMAL 0
#define GS_CAN_MODE_LISTEN_ONLY BIT(0)
#define GS_CAN_MODE_LOOP_BACK BIT(1)
#define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
#define GS_CAN_MODE_ONE_SHOT BIT(3)
`

##########################################################
##########################################################
Sorry for my poor english. Thank for you great work, this module is useful !

I had some issues using this module with candlelight_fw clone:
I use your test code to send & recv can frame, dev.read() always read back frame send by dev.send(). After read module's code, I add 'GS_USB_MODE_NO_ECHO_BACK' flags like this:

dev.start(mode=GS_USB_MODE_NORMAL | GS_USB_MODE_NO_ECHO_BACK)

but it's seems not work, dev.read() read back frame send by dev.send() again, so i check flags like this, it's seems ok:

iframe = GsUsbFrame()
if dev.read(iframe, 50) and iframe.echo_id != GS_USB_ECHO_ID:
    print("RX  {}".format(iframe))

I‘m not familiar with python and don't know how, any suggestions?

Timeout error when sending single frame

I have noticed that the following timeout error ocurrs only when transmitting a frame without constantly reading frames?

idual Project\RDSMSN_module\test.py", line 95, in main
if dev.send(frames[0]):
File "C:\Python\Python38\lib\site-packages\gs_usb\gs_usb.py", line 160, in send
self.gs_usb.write(0x02, frame.pack(hw_timestamps))
File "C:\Python\Python38\lib\site-packages\usb\core.py", line 989, in write
return fn(
File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 837, in bulk_write
return self.__write(self.lib.libusb_bulk_transfer,
File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 938, in __write
_check(retval)
File "C:\Python\Python38\lib\site-packages\usb\backend\libusb1.py", line 602, in _check
raise USBTimeoutError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBTimeoutError: [Errno 10060] Operation timed out

Is this by design of the gs-usb module?

Thanks,

Oliver

Setup.py broken

First of all, thank you for this useful module!

I had some issues installing the module in a virtual environment using pip.
I believe this is due to the directory structure.
The source files are not inside a separate directory inside the repository, so find_packages() does not find anything and installs an empty package as a result.

Steps to reproduce:

$ python3 -m venv venv
$ source venv/bin/activate
$ pip install gs_usb

$ python -c "import gs_usb"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'gs_usb'

$ find venv -name "gs_usb*"
venv/lib/python3.8/site-packages/gs_usb-0.2.1.dist-info

The same issue occurs when the package is installed directly from git:

$ pip install git+https://github.com/jxltom/gs_usb.git
$ find venv -name "gs_usb*"
venv/lib/python3.8/site-packages/gs_usb-0.2.1.dist-info

Proposed solution

  • Move __init__.py, __version__.py, constants.py, gs_usb.py, gs_usb_frame.py to directory gs_usb inside the repository.

  • Update setup.py:

    with open(os.path.join(here, "gs_usb", "__version__.py")) as f:
  • (Optional) Update __init__.py so that the main classes can be imported directly:

    from .__version__ import __version__
    from .gs_usb import GsUsb
    from .gs_usb_frame import GsUsbFrame

Libusb error with canable gs_usb device

When run exmple with canable device it fail with error:

Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> dev.start() File "C:\Users\100034799\AppData\Local\Programs\Python\Python310-32\lib\site-packages\gs_usb\gs_usb.py", line 66, in start self.gs_usb.ctrl_transfer(0x41, _GS_USB_BREQ_MODE, 0, 0, mode.pack()) File "C:\Users\100034799\AppData\Local\Programs\Python\Python310-32\lib\site-packages\usb\core.py", line 1071, in ctrl_transfer self._ctx.managed_open() File "C:\Users\100034799\AppData\Local\Programs\Python\Python310-32\lib\site-packages\usb\core.py", line 113, in wrapper return f(self, *args, **kwargs) File "C:\Users\100034799\AppData\Local\Programs\Python\Python310-32\lib\site-packages\usb\core.py", line 131, in managed_open self.handle = self.backend.open_device(self.dev) File "C:\Users\100034799\AppData\Local\Programs\Python\Python310-32\lib\site-packages\usb\backend\libusb0.py", line 501, in open_device return _check(_lib.usb_open(dev)) File "C:\Users\100034799\AppData\Local\Programs\Python\Python310-32\lib\site-packages\usb\backend\libusb0.py", line 447, in _check raise USBError(errmsg, ret) usb.core.USBError: [Errno None] b'libusb0-dll:err [os_open] failed to open \\\\.\\libusb0-0003--0x1d50-0x606f: win error: Le fichier sp\xe9cifi\xe9 est introuvable.\r\n'

configuration detail:
Python 3.10
Windows 10
usb driver: libusb-win32 (v1.2.7.3)
canable FW : candleLight_gsusb

No backends error

Hi everyone,
when I run the example code I have this error "usb.core.NoBackendError: No backend available".

Someone can help me?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.