I need some help troubleshooting an Attribute Error in my Python code.
I am working with a serial device connected by a /dev/ttyUSB0 to a Raspberry Pi 2B. My goal for this code is to read live serial data continuously from the serial device and separate out commands delineated by an '#' symbol instead of /n or /r.
This project is still in its early stages but from some quick research I've done the pyserial attribute 'read_until' would work perfectly as its backbone. This would allow me to read in one byte at a time and stop at an '#' symbol, and afterwards process the data before moving on to the next portion of serial data.
For some reason I get an Attribute Error when I try to use 'read_until' claiming that the 'Serial' object does not have this function. Below is the test code I am using and the error it spit back out:
import serial
gc = serial.Serial('/dev/ttyUSB0', baudrate = 230400)
print(gc.name)
def getCommand():
gcData = gc.read_until('#', 1).decode('ascii')
print(gcData)
getCommand()
gc.close()
And here is the output that this code gave me:
>>>
/dev/ttyUSB0
Traceback (most recent call last):
File "/home/pi/Python/GC/serialRead1.py", line 10, in <module>
getCommand()
File "/home/pi/Python/GC/serialRead1.py", line 7, in getCommand
gcData = gc.read_until('#', 1).decode('ascii')
AttributeError: 'Serial' object has no attribute 'read_until'
>>>
The frustration behind this is that 'read_until' should have been imported along with the serial library. Perhaps I am missing something quite simple, but it would be very helpful if I could use this attribute. Most other questions about 'read_until' are referencing the 'telnetlib' which as far as I know is not the same as what PySerial has.
Here is a link to the website where I found the documentation saying that 'read_until' should belong to PySerial: https://pyserial.readthedocs.io/en/latest/pyserial_api.html
Just so you are aware, I am very new to programming in general. Some of the jargon that seasoned programmers like yourselves use might go over my head and so I may have no idea how to perform the solutions you have. I ask for your patience as I learn more about programming and Python in general.
Thank you very much for your time!
It seems you have PySerial version 2.6 installed. This version was released in 2011, while the read_until method was added in 2015.
The easiest way to install the latest version (as you've discovered) is to use pip:
sudo apt install python3-pip
python3 -m pip install --user pyserial
Note that using pip with sudo is bad practice.
It's best to use a virtual environment or to use the --user flag, as demonstrated above.
Related
Hello there,
i am writing a code for auto discovery of IP within the network and then
data transfer using the socket programming in python. I have read the RSA
and want to implement in the code. i go through to the link where i got
the code implementation and the whole code for server and client.
Here is the link where the code is:
<https://riptutorial.com/python/example/27170/client-side-implementation>
<https://riptutorial.com/python/example/27169/server-side-implementation>
There are two links and the setup for PyCrypto is.
*PyCrypto (Download Link: https://pypi.python.org/pypi/pycrypto )
*PyCryptoPlus (Download Link: https://github.com/doegox/python-cryptoplus )
i tried it on raspberry pi and install all the essential modules which i wrote above, and run it using the command line as follows:
python3 server.py
but it gives me some module related errors.
Crypto.Cipher.IDEA isn't available. You're probably using the Debian
pycrypto version. Install the original pycrypto for IDEA.
Traceback (most recent call last):
File "serverRsa.py", line 10, in <module>
from CryptoPlus.Cipher import IDEA
File "/home/pi/.local/lib/python3.5/site-
packages/CryptoPlus/Cipher/IDEA.py", line 4, in <module>
import Crypto.Cipher.IDEA
ImportError: No module named 'Crypto.Cipher.IDEA'
i tried it using the pip install PyCrypto and using the same with pip3.
and then run the same code but same error occurred.
Actually problem statement is to auto discover of all the nearby ip's
using the python programming , where i run the code on Raspberry Pi and
make it as a hotspot and other Pi boards act as client. Now when the
server found the client or discover them then it register them using some
key or encryption method.
i just need or code that passes some message to client using RSA but it seems the code have error.
Anyone please fix this issue.
Crypto.Cipher doesn't have any attribute named: IDEA.
import Crypto.Cipher.IDEA #won't work -_-
Maybe what you are looking for is CryptoPlus:
import CryptoPlus.Cipher.IDEA
If you really need an IDEA cipher in Python 2, and it's OK for you that it's slow (much slower than if it was implemented in C), there is one here: https://pastebin.com/hTn5K3Tx . Use it like this:
cb = IDEA('2bd6459f82c5b300952c49104881ff48'.decode('hex'))
plaintext, ciphertext = 'f129a6601ef62a47'.decode('hex'), 'ea024714ad5c4d84'.decode('hex')
assert cb.encrypt(plaintext) == ciphertext
assert cb.decrypt(ciphertext) == plaintext
Please note that the last patent on IDEA expired in 2012, and now IDEA is free to use for the public.
Standard Crypto Ciphers installed with pip install pycrypto now is version 2.6.1 and because of license restrictions it doesn't include IDEA (Crypto.Cipher.IDEA)
If you want to install Crypto.Cipher.IDEA you must find pycrypto-2.0.1 (freely available for download) which was last used to embedded this crypto cipher see pts comment above. Then follow the standard procedure to install the package pycrypto
python setup.py install
Problem appear if want to install IDEA for pyton3.
pexpect has gone through big changes since the version provided by Ubuntu 15.04 (3.2). When installing newest version of pexpect using pip3, this minimal program that previously successfully gave terminal emulation over serial console does not work any more:
#!/usr/bin/python3
import serial
import pexpect.fdpexpect
ser = serial.Serial("/dev/ttyS0", baudrate=115200)
spawn = pexpect.fdpexpect.fdspawn(ser)
spawn.interact()
The newer API is missing interact() method in class pexpect.fdpexpect.fdspawn which used to be there.
Question: how is the newer version of pexpect (currently 4.2.1) supposed to be used to provide free manual interaction with file object (serial port in this case)?
Alternatively, question/work-around: I recognize I am using a bit heavy machinery for such a simple use case, any suggestions for other python libraries that can do the same as earlier version of pexpect could?
Code reading: Examples use pexpect.spawn(command_str) to get a spawn object which has interact() method; actually this pexpect.spawn() is the same as directly creating a pexpect.pty_spawn.spawn object which has this method. On the other hand, pexpect.fdpexpect.fdspawn() will construct a pexpect.fdpexpect.fdspawn class which is missing interact() method. Both of these spawn classes derive from pexpect.spawnbase.SpawnBase class. Based on my quick reading, this looks like regression that resulted from refactoring on the way to version 4.x.
Browsing the pexpect issues, found #351, #356, and the newly submitted #377. By my quick reading, it seems this is a regression that was brought by uncompleted refactoring along the way towards new major release version 4. There are three possible avenues:
Make sure to install older version:
$ pip3 install "pexpect<3"
(or make sure that the version installed by other system is 3.x).
Fix pexpect by yourself.
Use some other python library.
These avenues were all more or less hinted by github user #takluyver, apparently maintainer of pexpect in comment to issue #351:
#jquast any thoughts on what to do for this (and #356)? I feel bad that we've broken things people were doing with fdspawn, but I really don't want to make it inherit from the pty spawn class, and now that it works on Windows, I think it's important to preserve that too.
Maybe we should just say:
If you have legacy code that broke with Pexpect 4, downgrade to Pexpect 3.x (pip install pexpect<4)
If you're writing new code, use streamexpect instead of pexpect.
I recently installed scapy and was trying to start using it and I'm having trouble using the sniff() function.
I've been able to install Scapy using the steps described in their docs. I'm running Windows 7 x64 and using Python 2.6. I'm able to use the send family of functions fine (confirmed with Wireshark) but sniff() is failing with the following stack trace:
Traceback (most recent call last):
File "sniffingStuff.py", line 11, in <module>
sniff(filter="ip",prn=customAction)
File "C:\Python26\lib\site-packages\scapy\sendrecv.py", line 575, in sniff
sel = select([s],[],[],remain)
select.error: (10038, 'An operation was attempted on something that is not a socket')
The only thing off the top of my head that I thought might be wrong is that I had PCAP installed already because I had Wireshark installed. I looked around and didn't see any useful answers.
Edit: Since I didn't make this clear in my original post, any calls to the sniff function fails, regardless of parameters, filters, etc. For a concrete reference see here.
Thanks
I believe that scapy requires a specific version of WinPCAP as per the instructions for installation. Check the Windows installation guide here for supported version information.
I am trying to follow the tutorial given in:
https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html
While trying the third example (BackgroundSubtractorGMG) I get this error:
AttributeError: 'module' object has no attribute 'createBackgroundSubtractorGMG'
I got the same error for the earlier examples. But I followed the explanation given in this post. some how, the same trick did not work here.
If there is some one who has managed to solve it, please help me out.
Using Python 2.7.3 & opencv 2.4.6.1 on Ubuntu 12.04
In OpenCV 3.0.0-dev, you have to compile with the contrib repos and then it's in the bgsegm sub-module. I.e. just call cv2.bgsegm.createBackgroundSubtractorGMG()
cv2.bgsegm.createBackgroundSubtractorGMG()
cv2.createBackgroundSubtractorMOG2()
cv2.bgsegm.createBackgroundSubtractorMOG(),
**this worked for me **
oh dear, that's another one of those stories ...
with 2.4.6, you only can use BackgroundSubtractorMOG from python. (full stop)
as of 2.4.8, it seems, the BackgroundSubtractorMOG2 problem got fixed, but the BackgroundSubtractorGMG is still missing.
with both versions, you use a plain constructor to create one.
in 3.0 (master), they changed the syntax, you now have to call 'createBackgroundSubtractorGMG', 'createBackgroundSubtractorMOG2' and such (that's what your tutorial might be refering to). but now you can use all 3 versions at least.
so in any way, if you want to use BackgroundSubtractorMOG2 , you'll have to update to 2.4.8, if you need BackgroundSubtractorGMG, you'll need 3.0 (which is 'bleeding edge' in a way, but the new interface has the far better control over the params needed, imho).
bgsegm was in contrib module of opencv, but after the update I am not sure.
But still,
if you have not built contrib module:
pip install opencv-contrib-python
Make sure no console is running that has imported cv2 while you execute your installing process.
Run the cmd as Administration
It worked for me.
First of all I am quite new with both python and Linux.
That said I am trying to communicate to an FTDI UM232H chip using the pylibftdi library.
I am running my scripts on Linux Ubuntu 12.04.
I installed the library that I got here:
http://pylibftdi.readthedocs.org/en/latest/
and apparently everything worked fine.
I was also able to run some of the examples successfully.
I then tried to write some code to communicate with the device: I wired it in a bus powered configuration (in order to get power from the USB), then I short-circuited TX and RX pins so that what I am sending on TX will be read back on RX.
I do not get any error, but I am not able to read back anything on RX.
Here is my very simple code:
import pylibftdi as p
import time
test = p.Driver()
print test.list_devices()
#This is not working
#print test.libftdi_version()
dev1 = p.Device(device_id='FTUBL36A', mode='t')#, chunk_size = 0)
dev1.flush()
dev1.baudrate = 9600
len = dev1.write('Hello World')
time.sleep(1)
res = dev1.read(len)
print 'res: ', res
Also I am not able to get the libftdi_verion information, even though I installed it.
Does anybody have an idea of what I am doing wrong? Has anyone else ever experienced such a problem?
Thanks in advance!
I'm afraid I don't have a full answer, but some observations (I would make this more concise and put as a comment, but I don't have sufficient rep).
Disclaimer: I'm the author of pylibftdi
libftdi version / installation
The Ubuntu libftdi package (even latest 13.10) is 0.20. This is particularly confusing / annoying since the Ubuntu package name is 'libftdi1'. Prior to (real) libftdi 1.0, there isn't a ftdi_get_library_version() function, so libftdi_version() won't work with the Ubuntu default package. The next version of pylibftdi recognises this and gives an appropriate response.
To install 1.0, follow the instructions at http://developer.intra2net.com/mailarchive/html/libftdi/2013/msg00014.html (e.g. the following worked for me - note I'd previously had the Ubuntu libftdi1 package installed, and other dependencies may be required):
$ sudo apt-get install cmake libusb-1.0
$ git clone git://developer.intra2net.com/libftdi
$ cd libftdi
$ git checkout tags/v1.0
$ mkdir build; cd build
$ cmake ..
$ make
$ sudo make install
$ sudo ldconfig # ensure the new library is recognised
Following this, getting the library version should work:
$ python -c 'from pylibftdi import Driver; print Driver().libftdi_version()'
(1, 0, 0, '1.0', 'v1.0')
Data reading errors with UM232H
Note that the latest libftdi (repeat things above but use git checkout master) seems to work slightly better for me, and your program above works perfectly for me with a UM232H (I obviously omitted the device=... parameter, but otherwise left it unchanged). If I replace 'Hello World' with 'Hello World' * 10, writing and reading a longer string, then I get a truncated value returned, typically only 29 bytes. I'm not sure why it's this value; with earlier libftdi versions it seemed to return 17 bytes consistently. Very odd.
With other devices (UB232R, UM232R), this all works perfectly as expected, so in the (admittedly unlikely) event you have a choice of device, you could consider switching... Note the FT232H chip is relatively new and support in libftdi may not be as solid as for the older devices - but equally possible is that I'm incorrectly assuming it should work in a similar way to the older devices in pylibftdi.
Other thoughts
I've tried blacklisting ftdi_sio (add blacklist ftdi_sio to the end of /etc/modprobe.d/blacklist.conf), which stops the Linux kernel doing who-knows-what with the device on plugging it in (the Rx/Tx LED flashes several times when plugging in without this and the ftdi_sio module is loaded. I'm not sure this is necessary or makes a difference though.
Note that there's no guarantee that a read() will return anything previously written to the device (assuming an external Tx->Rx loopback), due to internal buffering in the device, and various driver layers (including USB). These are just streams, and framing should be done in the application layer. Having said that, it 'just works' on a UB232R, and even taking this into account seems the UM232H seems to have issues with pylibftdi for serial mode.
Bitbang mode seems to work fine for UM232H / pylibftdi.
I'll continue investigating and update this answer if I find out anything more.