cdump / radiacode

Library for RadiaCode-101
MIT License
99 stars 18 forks source link

How to Connect Via USB and Linux #31

Open adamjvr opened 2 months ago

adamjvr commented 2 months ago

There isn't much documentation regarding the examples on how to connect via USB how do I go about doing it ? The documentation shows use for Bluetooth passing a MAC address as a command line argument to get it to connect tot he script. I want to use USB.

Also does this library work under Linux ? I was able to install it via pip no problems but given that the radiacode products don't work under Linux and there isn't a Phyton USB Driver for Linux as far as I know I'm not sure if it will work. Can anyone add anything ?

ckuethe commented 2 months ago

This library works fabulously well under linux, no weird vendor drivers required.

from radiacode import RadiaCode

rc = RadiaCode()
# or rc = RadiaCode(serial_number="RC-102-123456") if you want to connect to a specific device

s = rc.spectrum()
# ... do something with the spectrum
cdump commented 2 months ago

You can try to run it with sudo (see readme.md) or install udev rules (google how to do it)

adamjvr commented 2 months ago

This library works fabulously well under linux, no weird vendor drivers required.

from radiacode import RadiaCode

rc = RadiaCode()
# or rc = RadiaCode(serial_number="RC-102-123456") if you want to connect to a specific device

s = rc.spectrum()
# ... do something with the spectrum

Thanks for your reply and example this is very helpful !! I tried querying the USB ports on my system while the Radiacode 101 is connected and from what I can see it doesn't show up ? Are you able to see it when querying your USB ports from the terminal ? This is what I get when I query with it connected to my system:

image

You can try to run it with sudo (see readme.md) or install udev rules (google how to do it)

Thanks I will try this and report back! I appreciate the support a lot !!

ckuethe commented 2 months ago

Radiacode devices do not show up as a tty, they're connected as a generic USB device. In this library all the "business logic" necessary to interact with a radiacode device is in userspace.

Are you trying to communicate with your device using a VM? If so, you might also need to check your USB pass-through settings in your hypervisor.

dmesg

usb 3-1: new full-speed USB device number 40 using xhci_hcd
usb 3-1: New USB device found, idVendor=0483, idProduct=f123, bcdDevice= 2.00
usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 3-1: Product: RadiaCode-102
usb 3-1: Manufacturer: RadiaCode
usb 3-1: SerialNumber: RC-102-001272

lsusb

$ sudo lsusb -d 0483:f123
Bus 003 Device 040: ID 0483:f123 STMicroelectronics RadiaCode-102

lsusb -v

$ sudo lsusb -d 0483:f123 -v

Bus 003 Device 040: ID 0483:f123 STMicroelectronics RadiaCode-102
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.01
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x0483 STMicroelectronics
  idProduct          0xf123 
  bcdDevice            2.00
  iManufacturer           1 RadiaCode
  iProduct                2 RadiaCode-102
  iSerial                 3 RC-102-001272
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x0020
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          4 Custom Config
    bmAttributes         0xc0
      Self Powered
    MaxPower              400mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass    255 Vendor Specific Subclass
      bInterfaceProtocol    255 Vendor Specific Protocol
      iInterface              5 Custom Interface
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
Binary Object Store Descriptor:
  bLength                 5
  bDescriptorType        15
  wTotalLength       0x000c
  bNumDeviceCaps          1
  USB 2.0 Extension Device Capability:
    bLength                 7
    bDescriptorType        16
    bDevCapabilityType      2
    bmAttributes   0x00000002
      HIRD Link Power Management (LPM) Supported
Device Status:     0x0001
  Self Powered
ckuethe commented 2 months ago

Gonna toot my own horn a little bit and point you at some other things I've written to interact with radiacode on linux, using this library: https://github.com/ckuethe/radiacode-tools

This code works for me on both x86_64, armhf, and aarch64. I don't have any riscv or i686 boards (I guess I could run using qemu) but really this library should work just about anywhere that libusb works.

adamjvr commented 2 months ago

Radiacode devices do not show up as a tty, they're connected as a generic USB device. In this library all the "business logic" necessary to interact with a radiacode device is in userspace.

Are you trying to communicate with your device using a VM? If so, you might also need to check your USB pass-through settings in your hypervisor.

dmesg

usb 3-1: new full-speed USB device number 40 using xhci_hcd
usb 3-1: New USB device found, idVendor=0483, idProduct=f123, bcdDevice= 2.00
usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 3-1: Product: RadiaCode-102
usb 3-1: Manufacturer: RadiaCode
usb 3-1: SerialNumber: RC-102-001272

lsusb

$ sudo lsusb -d 0483:f123
Bus 003 Device 040: ID 0483:f123 STMicroelectronics RadiaCode-102

lsusb -v

$ sudo lsusb -d 0483:f123 -v

Bus 003 Device 040: ID 0483:f123 STMicroelectronics RadiaCode-102
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.01
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x0483 STMicroelectronics
  idProduct          0xf123 
  bcdDevice            2.00
  iManufacturer           1 RadiaCode
  iProduct                2 RadiaCode-102
  iSerial                 3 RC-102-001272
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x0020
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          4 Custom Config
    bmAttributes         0xc0
      Self Powered
    MaxPower              400mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass    255 Vendor Specific Subclass
      bInterfaceProtocol    255 Vendor Specific Protocol
      iInterface              5 Custom Interface
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
Binary Object Store Descriptor:
  bLength                 5
  bDescriptorType        15
  wTotalLength       0x000c
  bNumDeviceCaps          1
  USB 2.0 Extension Device Capability:
    bLength                 7
    bDescriptorType        16
    bDevCapabilityType      2
    bmAttributes   0x00000002
      HIRD Link Power Management (LPM) Supported
Device Status:     0x0001
  Self Powered

I am not using a VM, I am using a Thinkpad P15 Gen2i with Ubuntu Budgie 22.04 LTS latest updates and patches via Ubuntu Pro

Gonna toot my own horn a little bit and point you at some other things I've written to interact with radiacode on linux, using this library: https://github.com/ckuethe/radiacode-tools

This code works for me on both x86_64, armhf, and aarch64. I don't have any riscv or i686 boards (I guess I could run using qemu) but really this library should work just about anywhere that libusb works.

I will check out this project as well !! Thanks for such a quick response overall !! I appreciate it a lot !!

ckuethe commented 2 months ago

I would be remiss if I didn't also say have a look at the included examples, which I used in order to write my tools: https://github.com/cdump/radiacode/tree/master/radiacode-examples

Once you can make the examples work on your machine, you could do a PR to improve the documentation for the next person to find the library. You're approaching this with fresh eyes; what would you have liked to see in the docs to make it easier to start working with RadiaCode devices on linux? :)

adamjvr commented 2 months ago

I would be remiss if I didn't also say have a look at the included examples, which I used in order to write my tools: https://github.com/cdump/radiacode/tree/master/radiacode-examples

Once you can make the examples work on your machine, you could do a PR to improve the documentation for the next person to find the library. You're approaching this with fresh eyes; what would you have liked to see in the docs to make it easier to start working with RadiaCode devices on linux? :)

I just got the basic example working properly and retooled my own code, now my script successfully connects to the hardware and prints back some device information. Now I just need to figure out how to sample data and get it into a CSV file entry. Thank you so much for you help so far !

I was thinking a Markdown page that describes the API for the radiacode class would be very helpful. I'm new to Python development and spent most of my time with C for embedded microcontrollers, I'm learning fast but the more information the easier it can be for me.

adamjvr commented 2 months ago

I should mention that my project is open source and available here ! Still workign through getting the correct data in the correct columns but I've made a lot of progress today and I'm very thankful for all the help everyone has been giving me !! Thank you so much ! https://github.com/adamjvr/radiacode-101-datalogging-script