Note: This is Python 2.7 and PyUSB 0.4.3
I am trying to send serial data from an Arduino Yun to a USB dongle plugged into the Yun's USB host port using a Python script. The data is just a sequence of characters (currently just one to simplify debugging). Here is the script, the data to be written is the character 'W':
import usb
busses = usb.busses()
for bus in busses:
devs = bus.devices
for dev in devs:
if dev.idVendor == 9304 and dev.idProduct == 1:
d = dev
conf = d.configurations[0]
intf = conf.interfaces[0][0]
endpoints = []
for endpoint in intf.endpoints:
endpoints.append(endpoint)
endpoint = endpoints[0]
handle = d.open()
handle.interruptWrite(0, 'W')
This is the error:
Traceback (most recent call last):
File "ser_test.py", line 21, in <module>
handle.interruptWrite(0, 'W')
usb.USBError: error submitting URB: No such file or directory
I have tried 0-1000 for the first argument with no luck. What is the proper way to write this to send a 'W' character from the host to the dongle? I've read in other posts that PyUSB is only a set of wrappers for usblib, but haven't been able to find an answer in the usblib documentation.
Here is the output of lsusb (dongle is 2458:0001):
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 058f:6254 Alcor Micro Corp. USB Hub
Bus 001 Device 003: ID 2458:0001
Bus 001 Device 004: ID 058f:6366 Alcor Micro Corp. Multi Flash Reader
Thanks.
Related
I used pycharm for writing my code and plcsim for simulatio. When I run my code and run plcsim I get an error.
I used Tia portal V16 and s7-1200 and I want to communicate between S7-1200 and python-snap7
This is the error:
PS C:\Users\Badro\PycharmProjects\pythonProject1\venv> py test.py
b' TCP : Unreachable peer'
Traceback (most recent call last):
- File "C:\Users\Badro\PycharmProjects\pythonProject1\venv\test.py", line 8, in <module>
plc.connect(IP, RACK, SLOT)
- File "C:\Users\Badro\AppData\Local\Programs\Python\Python310\lib\site-
packages\snap7\client.py",
line 24, in f
check_error(code, context="client")
- File "C:\Users\Badro\AppData\Local\Programs\Python\Python310\lib\site-
packages\snap7\common.py",
line 89, in check_error
raise RuntimeError(error)
RuntimeError: b' TCP : Unreachable peer'enter code here
and this is my code:
import snap7
IP = '192.168.100.100'
RACK = 0
SLOT = 1
plc = snap7.client.Client()
plc.connect(IP, RACK, SLOT)
print(plc.get_cpu_state())
I watched this videohttps://www.youtube.com/watch?v=BKnK4AT_WKs
Where is the problem or is the problem that plcsim must be the real plc for simulation not plcsim?
PLCSIM cannot simulate a network interface on simulated PLCs.
You have to install PLCSIM Advanced V3.0, which also simulates the network interface for each simulated PLC.
I have PLCSIM V13 SP1 and have tested your program and it works.
I think you need to install NetToPLCSim which allows you to connect to the Plc to test your application using the network interface of the PC running the simulation.
It is recommended that you run NetToPLCSim as an administrator.
I'm trying to learn to communicate with a Bluetooth device using Python but my first steps fail.
I can successfully connect to a given device using bluetoothctl:
[bluetooth]# connect F5:EE:1C:40:21:44
Attempting to connect to F5:EE:1C:40:21:44
[CHG] Device F5:EE:1C:40:21:44 Connected: yes
Connection successful
[NEW] Primary Service (Handle 0xa9bd)
/org/bluez/hci0/dev_F5_EE_1C_40_21_44/service000a
00001801-0000-1000-8000-00805f9b34fb
Generic Attribute Profile
[NEW] Primary Service (Handle 0xa9bd)
/org/bluez/hci0/dev_F5_EE_1C_40_21_44/service000b
53300001-0023-4bd4-bbd5-a6920e4c5653
Vendor specific
[NEW] Characteristic (Handle 0x6f54)
/org/bluez/hci0/dev_F5_EE_1C_40_21_44/service000b/char000c
53300002-0023-4bd4-bbd5-a6920e4c5653
Vendor specific
[NEW] Characteristic (Handle 0x6604)
/org/bluez/hci0/dev_F5_EE_1C_40_21_44/service000b/char000e
53300003-0023-4bd4-bbd5-a6920e4c5653
Vendor specific
[NEW] Descriptor (Handle 0x0164)
/org/bluez/hci0/dev_F5_EE_1C_40_21_44/service000b/char000e/desc0010
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
but trying to connect via Python3's pybluez module results in an exception being raised:
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect(("F5:EE:1C:40:21:44", 1))
...
BluetoothError Traceback (most recent call last)
<ipython-input-17-2af54455681d> in <module>
----> 1 sock.connect(("F5:EE:1C:40:21:44", 1))
~/.local/lib/python3.9/site-packages/bluetooth/bluez.py in connect(self, *args, **kwargs)
BluetoothError: [Errno 112] Host is down
What am I doing wrong here? Very likely I'm just missing the very basics of Bluetooth development - maybe you can give me a direction..
Looking at the log information you shared from when you connected with bluetoothctl, it looks like you are connecting to a Bluetooth Low Energy (BLE) device.
The commands you are you issuing with PyBlueZ is to connect to a Bluetooth Classic (BR/EDR) device and I suspect that is why the device is not responding.
PyBlueZ uses a deprecated API for BlueZ. The currently supported APIs are documented at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc
It is the Device API that is used for connecting to a device. Below is an example of how to use it with the generic D-Bus library pydbus
from time import sleep
import pydbus
DEVICE_ADDR = 'F5:EE:1C:40:21:44' # device address
# DBus object paths
BLUEZ_SERVICE = 'org.bluez'
device_path = f"/org/bluez/hci0/dev_{DEVICE_ADDR.replace(':', '_')}"
# setup dbus
bus = pydbus.SystemBus()
device = bus.get(BLUEZ_SERVICE, device_path)
# Connect to device
device.Connect()
sleep(10)
device.Disconnect()
Beaglebone Black Wireless: Ubuntu 16.04,
Computer: Ubuntu 16.04,
Python 3.5.2
I am trying to communicate with a device via Modbus using a Beaglebone Black. I have read of people doing this same thing with a USB/RS485 dongle using minimalmodbus. I've tried changing up the settings; every possible baud rate, shorter cable, etc. There must be something within the Beagle that needs to be configured/is mis-configured for this type of work.
Code
#!/usr/bin/env python
# -*- coding; utf-8 -*-
import minimalmodbus
import serial
import time
i = minimalmodbus.Instrument(port='/dev/ttyUSB0', slaveaddress=1, mode='rtu')
i.serial.baudrate = 115200
i.serial.bytesize = 8
i.serial.parity = serial.PARITY_EVEN
i.serial.stopbits = 1
i.serial.timeout = 1
i.handle_local_echo = None
while True:
try:
print(i.read_registers(2008, 2, 3))
except serial.serialutil.SerialException as error:
print(error)
except FileNotFoundError as error2:
print(error2)
except IOError as error3:
print(error3)
time.sleep(1)
...using a USB/RS485 dongle. Using this dongle/code combination yields perfect results on my computer (for the past 4 months) but not the Beagle today.
Computer: Message received:
[0, 0]
Beagle: Error received:
IOError: No communication with instrument (no answer)
I am looking for possible avenues of troubleshooting. I have verified that the cable is not too long, and the USB dongle is OK. I have also ruled out any code issues.
Edit 1:
Forgot to include lsusb and ls /dev/ttyUSB*:
Bus 002 Device 004: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 002 Device 003: ID 04d9:0024 Holtek Semiconductor, Inc.
Bus 002 Device 002: ID 0409:0059 NEC Corp. HighSpeed Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
and
/dev/ttyUSB0
Edit 2:
I am able to achieve a loopback setup using pyserial and shorting the Rxd and Txd + and - terminals.
Edit 3:
I have tried all possible wiring configurations. Only one configuration works on the computer (Tx+ -> A and Tx- -> B). While using the minimalmodbus debug feature, I can see that the same message is being sent to the device on the Beagle and the computer ('\x01\x03\x07Ø\x00\x02ED' (01 03 07 D8 00 02 45 44)
). The computer receives a response while the Beagle does not.
The Beagle has no trouble mounting the adapter to /dev/ttyUSB0.
Edit 4:
#Carlo Zanocco requested the output of ls /dev/ | grep tty
tty
tty0
tty1
tty10
tty11
tty12
tty13
tty14
tty15
tty16
tty17
tty18
tty19
tty2
tty20
tty21
tty22
tty23
tty24
tty25
tty26
tty27
tty28
tty29
tty3
tty30
tty31
tty32
tty33
tty34
tty35
tty36
tty37
tty38
tty39
tty4
tty40
tty41
tty42
tty43
tty44
tty45
tty46
tty47
tty48
tty49
tty5
tty50
tty51
tty52
tty53
tty54
tty55
tty56
tty57
tty58
tty59
tty6
tty60
tty61
tty62
tty63
tty7
tty8
tty9
ttyGS0
ttyS0
ttyS1
ttyS2
ttyS3
ttyS4
ttyS5
ttyUSB0
Edit 5:
Output of stty -F /dev/ttyUSB0 -a:
speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>;
swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
discard = ^O; min = 0; time = 0;
parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany
-imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl
-echoke -flusho -extproc
I experienced the same issues and it turned out it was a problem of termination and flow control.
I tried different USB dongles, all working with pcs and macs, and many different configurations on the BeagleBone, but nothing worked.
Low-cost USB dongles work well with PCs, but with microcontrollers, such as the BeagleBone, you need something that better handles the bus specifications.
I found out that some simple and low-cost TTL RS232 to RS485 boards work perfectly without any special configuration on the BeagleBone side since they automatically manage flow control.
You can search for RS485 automatic flow control on Ebay or similar websites.
Since the default serial port on your BeagleBone is already coupled to the system console, you need to enable another UART by properly editing the file /boot/uboot/uEnv.txt.
Then you need to connect 5V power supply and GND (available on P9_5 and P9_1 pins) and the correct serial pins (i.e. P9_26 and P9_24 if you enabled UART1) to the 5V, GND, RX and TX connectors of the adapter (do not bother with RTS and CTS since the adapter will manage those for you).
In your code change the port name to the new serial port (i.e. /dev/ttyO1 if you enabled UART1).
To easily debug your code, you can connect the USB dongle on a PC and the adapter on the BeagleBone through the RS485 wires and open two serial terminals.
How to get the USB information like Manufacture Name, Size etc?
I am using Ubuntu 16.
and Python 2.7.12
Have you tried something like this :
busses = usb.busses()
for bus in busses:
devices = bus.devices
for dev in devices:
print (repr(dev))
print("manufacturer", dev.iManufacturer)
print("Size", dev.iSize)
NOTE : I think it will work only when you are connected one usb at a time
with this you can print all USB String Descriptors in pyusb : Manufacturer String Descriptor, Product String Descriptor, Serial Number String Descriptor ( for all USB devices, including the host controllers ) cf https://www-user.tu-chemnitz.de/~heha/viewchm.php/hs/usb.chm/usb5.htm
import usb
busses = usb.busses()
for bus in busses:
devices = bus.devices
for dev in devices:
manufacturer = usb.util.get_string(dev.dev, dev.dev.iManufacturer)
print str(manufacturer)
product = usb.util.get_string(dev.dev, dev.dev.iProduct)
print str(product)
serialnumber = usb.util.get_string(dev.dev, dev.dev.iSerialNumber)
print str(serialnumber)
print('\n')
you have to call this as root / with sudo else you get ValueError: The device has no langid error
other USB attributes you can print with their names :
import usb
busses = usb.busses()
for bus in busses:
devices = bus.devices
for dev in devices:
print " Device class:",dev.dev.bDeviceClass
print " Device sub class:",dev.dev.bDeviceSubClass
print " Device protocol:",dev.dev.bDeviceProtocol
I have a problem. So, i connected 3 huawei e303 modems to my computer. I can see their interfaces and IPs that were given them by system. Here is an example
`modems = []
modem_temp = modem_sys()
for temp in interfaces():
addr = ifaddresses(temp)
if addr.get(AF_INET, None):
if '58:2c:80' in addr[AF_INET][0]['addr']: # 58:2c:80 is huawei
modem_temp.set_ip_adress(addr[AF_INET][0]['addr'])
print(modem_temp.ip_adress)
modem_temp.set_modem_interface(temp)
print(modem_temp.modem_interface)
modems.append(modem_temp)`
Where modem_sys is my class that contains only info about modem.
Output is:
169.254.169.233 {E549A313-E2CF-489A-A08B-A263CA7CBDB0}
192.168.1.100 {F3477F61-FC2D-4752-9FD9-0CF76529EA60}
169.254.170.23 {6F59AD83-A773-4D24-BF01-24FE7D9B6723}
But I can use to execute xml commands only 1 modem, because i can do them only on 192.168.1.1 address. So how can simultaniously use all modems to execute xml-commands? For example, after detecting modems, print device info by response = requests.get('http://192.168.1.1/api/device/information'). I'm using python3. I will be very appriciate for any help.