AttributeError: <class> instance has no attribute <> - python

I am new to python so need your help on the following error message. I have two files first one is "test1.py" which is I am running and has the following code.
import sys, time, re, os, pickle
from ComLib import *
obj = Com()
obj.ComOpen()
obj.ComReset()
obj.ComClose()
and the second file is "ComLib.py" and has the following code
import serial, sys, re, pickle, time
class Com:
def ComOpen(self):
self = serial.Serial()
self.port = "COM1"
self.baudrate = 9600
self.bytesize = serial.EIGHTBITS #number of bits per bytes
self.parity = serial.PARITY_NONE #set parity check: no parity
self.stopbits = serial.STOPBITS_ONE #number of stop bits
self.timeout = 1 #non-block read
self.xonxoff = True #disable software flow control
self.rtscts = False #disable hardware (RTS/CTS) flow control
self.dsrdtr = False #disable hardware (DSR/DTR) flow control
self.writeTimeout = 2 #timeout for write
self.open()
return
def ComClose(self):
self.close()
return
def ComReset(self):
print "Executing ComReset function...!!"
self.write("~~~~~~~~~~\r")
i = 0
while i<10 :
response = self.readline()
print "Inside first while loop...!!"
print "response = "+response
if (response == ':'):
print "-->colon found...ready for next input<---"
break
i=i+1
time.sleep(0.5)
return
While executing the above I am getting the following error
"Traceback (most recent call last):
File "C:\Users\vgupta\Desktop\KeyAT\final\WDEAutomationTestSuite\WDETestSuite\Bootguard\TC#001.py", line 17, in <modul
e>
obj.ComReset()
File "C:\Users\vgupta\Desktop\KeyAT\final\WDEAutomationTestSuite\APILib\ComLib.py", line 52, in ComReset
self.write("~~~~~~~~~~\r")
AttributeError: Com instance has no attribute 'write'"
Can anyone help me out in finding out what is wrong here.
Thanks,
Vipul

Your decleration Should be:
self.sSerial = serial.Serial()
self.sSerial.port = "COM1"
self.sSerial.baudrate = 9600
.........
then you can do self.sSerial.write("~~~~~~~~~~\r")
class Com is missing __init__

Related

Python pySerial - Problem using subclasses

I'm working on a project in Python that uses two or more serial ports to manage some devices from my RPi. When ports are open in the same file and I send commands to different instances of serial.Serial object everything works fine. This is an example:
import serial
device1_port = "/dev/ttyUSB0"
device2_port = "/dev/ttyUSB1"
# Command sent to device 1. No problem
d1 = serial.Serial(device1_port, timeout = 0.5)
d1.write(b'GET MUTE\n')
output1 = d1.readline()
print("Device 1 output: " + str(output1))
# Command sent to device 2. No problem
d2 = serial.Serial(device2_port, timeout = 1)
d2.write(b'00vP\r')
output2 = d2.readline()
print("Device 2 output: " + str(output2))
Output:
Device 1 output: b'DATA MUTE OFF\r\n'
Device 2 output: b'00vP0\r'
The problem comes when I try to separate one device from another using subclasses of serial.Serial. The reason is I want to deal with them like objects with their own methods (each device needs a lot of different commands, status queries...).
class device1(serial.Serial):
def __init__(self, port, timeout):
super().__init__(port, timeout)
serial.Serial(port, timeout)
def command1(self):
self.write(b'SET MUTE OFF\n')
self.write(b'GET MUTE\n')
output = self.readline()
print("Device 1 output: " + str(output))
class device2(serial.Serial):
def __init__(self, port, timeout):
super().__init__(port, timeout)
serial.Serial(port, timeout)
def command2(self):
self.write(b'00vP\r')
output = self.readline()
print("Device 2 output: " + str(output))
device1_port = "/dev/ttyUSB0"
device2_port = "/dev/ttyUSB1"
d1 = device1(device1_port, timeout=0.5)
d2 = device2(device2_port, timeout=1)
d1.command1()
d2.command2()
When I run this code the output is:
Device 1 output: b'DATA MUTE OFF\r\n'
_
and it keeps waiting forever for the second device. I'm forced to Ctrl + C and I get this:
^CTraceback (most recent call last):
File "./ct3.py", line 35, in <module>
d2.command2()
File "./ct3.py", line 23, in command2
output = self.readline()
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 483, in read
ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout.time_left())
KeyboardInterrupt
It's seems like there is some kind of conflict between the two subclasses but, obviously I have no idea what I'm doing wrong.
Can someone help me please?
You shouldn't be calling serial.Serial(port, timeout) from your __init__,
as super().__init__(...) is already doing this. See these answers. You don't even need an __init__ if you are not going to change what the base class does.
Also, there is a difference in your two versions with respect to the use of positional and keyword arguments. serial.Serial()'s first 2 positional arguments are port, baudrate, so you need to explicitly use the keyword argument timeout=:
def __init__(self, port, timeout):
super().__init__(port, timeout=timeout)

Reading and appending to the same text file

I have a program that writes serial data to the text file. I want to check for specific card UIDs in the text file by reading the file at first, if there is already a card UID I want to skip the serial write step (serial write 0), if there isn't that card UID I will go ahead and serial write 1.
In order to check for card UIDs I have employed next command, please have a look at my code.
import threading
import serial
import sys
import io
import codecs
import queue
from pynput import keyboard
with io.open("uid.txt", "w", encoding="utf-8") as b:
b.write("")
q = queue.Queue()
ser = serial.Serial('COM4', baudrate = 9600, timeout = 5)
class SerialReaderThread(threading.Thread):
def run(self):
while True:
output = ser.readline().decode('utf-8')
print(output)
q.put(output)
class FileWriting(threading.Thread):
def run(self):
while True:
output = q.get()
with io.open("uid.txt", "r+", encoding="utf-8") as input:
for line in input:
if line.startswith("Card UID: "):
s = (next(input))
if line.startswith(s): ***
ser.write(b'0\r\n')
else:
ser.write(b'1\r\n')
with io.open("uid.txt", "a+", encoding="utf-8") as f:
f.write(output)
serial_thread = SerialReaderThread()
file_thread=FileWriting()
serial_thread.start()
file_thread.start()
serial_thread.join()
file_thread.join()
FileWriting thread is what I need help with. Again I want to first read the text file (which initially will be empty as it is created) and check for lines with card UID and look if there already is that specific card UID in the file if there is write 0 if there isn't write 1 in serial.
However running this code gives me an error:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Tsotne\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Tsotne\AppData\Local\Programs\Python\Python38-32\project\fff.py", line 36, in run
s = (next(input))
StopIteration
Since you re-create the uid.txt file empty each time you run your program, you don't need a file to hold the information. Just use a set instead:
ser = serial.Serial('COM4', baudrate = 9600, timeout = 5)
class SerialReaderThread(threading.Thread):
def run(self):
uids = set()
while True:
output = ser.readline().decode('utf-8')
print(output)
response = b'0' if output in uids else b'1'
ser.write(response + b'\r\n')
uids.add(output)
serial_thread = SerialReaderThread()
serial_thread.start()
serial_thread.join()

Scapy sniff() function not working for no apparent reason

I'm trying to use the sniff() function that scapy provides but it raises the following error:
Traceback (most recent call last):
File "TestCode.py", line 54, in <module>
packets = getMessege()
File "TestCode.py", line 45, in getMessege
return sniff(count=getLen(), lfilter=filterFrom)
File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\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')
Here is the code (FromGlobal is a tuple that contains the IP and Port of the sender):
def getLen():
while True:
length, LenFrom = sock.recvfrom(1024)
try:
IntLen = int(length)
except:
pass
else:
if LenFrom == FromGlobal:
return IntLen
def filterFrom(pck):
try:
return pck[IP].src == FromGlobal[0] and pck[UDP].sport == FromGlobal[1]
except:
return False
def getMessege(): # TODO This needs to return only the messege and port
return sniff(count=getLen(), lfilter=filterFrom)
packets = getMessege()
print packets.show()
The weird part is that if I try to do it like so:
def func1():
return int('1')
def lfilter(pack):
return TCP in pack and pack[IP].src != '8.8.8.8'
def func():
return sniff(count=func1(), lfilter=lfilter)
var = func()
print var.show()
it works perfectly well. If someone could point out the difference between the two it would help a lot.
I'm use WinPcap 4.1.3 and scapy 2.x.
Well, Resolved it myself. apparently if you do:
from scapy.all import *
from scapy.layers.inet import *
the sniff function won't works so do only
from scapy.all import *

Sending a Raspberry Pi Datastream to xively

I have my rpi connected up to a digital scales via RS232, a MAX3232 serial port / ttl module connected into the GPIO. Successfully collected the weight data streaming from the scales using this code (data is in blocks of 20 and the weight is at 11 - 14). This prints it out locally in terminal:
import serial
import time
ser = serial.Serial("/dev/ttyAMA0")
read = ser.read(20)
def get_num(read):
return float(''.join(ele for ele in read if ele.isdigit() or ele == '.'))
while True:
print(get_num(read))
time.sleep(2)
This works great but my python code to send datastream to xively isn't so good.
def get_num(read):
return float(''.join(ele for ele in read if ele.isdigit() or ele == '.'))
# function to return a datastream object. This either creates a new datastream,
# or returns an existing one
def get_datastream(feed):
try:
datastream = feed.datastreams.get("(get_num(read))")
return datastream
except:
datastream = feed.datastreams.create("(get_num(read))", tags = "weight")
return datastream
def run():
feed = api.feeds.get(FEED_ID)
datastream = get_datastream(feed)
datastream.max_value = None
datastream.min_value = None
while True:
weight = get_num(read)
datastream.current_value = weight
datastream.at = datetime.datetime.utcnow()
try:
datastream.update()
except requests.HTTPError as e:
print "HTTPError({0}): {1}".format(e.errno, e.strerror)
time.sleep(5)
run()
It checks out OK in IDLE - but when run it generates an error as follows:
Traceback (most recent call last):
File "xivelycodescales.py", line 51, in <module>
run()
File "xivelycodescales.py", line 36, in run
datastream = get_datastream(feed)
File "xivelycodescales.py", line 32, in get_datastream
datastream = feed.datastreams.create("(get_num(read))")
File "/usr/local/lib/python2.7/dist-packages/xively/managers.py", line 416, in create
response.raise_for_status()
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 773, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 422 Client Error:
I can see what the error type is - and had good help at the RPI forums. But can't figure out where I've gone wrong
Many thanks

I cannot run mincemeat.py - a bunch of unknown stuff is printed to the terminal

I am trying to write a homework about map-reduce. I run in a terminal:
ioannis#ioannis-desktop:~$ python hw3.py
then in another terminal:
python mincemeat.py -p changeme localhost
Immediately in the former terminal, a bunch of stuff is typed:
ioannis#ioannis-desktop:~$ python hw3.py
error: uncaptured python exception, closing channel <mincemeat.ServerChannel connected 127.0.0.1:58061 at 0x7fabef5045a8>
(<type 'exceptions.TypeError'>:'NoneType' object is not iterable
[/usr/lib/python2.7/asyncore.py|read|83]
[/usr/lib/python2.7/asyncore.py|handle_read_event|449]
[/usr/lib/python2.7/asynchat.py|handle_read|158]
[/home/ioannis/mincemeat.py|found_terminator|82]
[/home/ioannis/mincemeat.py|process_command|280]
[/home/ioannis/mincemeat.py|process_command|123]
[/home/ioannis/mincemeat.py|respond_to_challenge|106]
[/home/ioannis/mincemeat.py|post_auth_init|289]
[/home/ioannis/mincemeat.py|start_new_task|258]
[/home/ioannis/mincemeat.py|next_task|304])
^CTraceback (most recent call last):
File "hw3.py", line 54, in <module>
results = s.run_server(password="changeme")
File "/home/ioannis/mincemeat.py", line 220, in run_server
self.close_all()
File "/usr/lib/python2.7/asyncore.py", line 421, in __getattr__
%(self.__class__.__name__, attr))
AttributeError: Server instance has no attribute 'close_all'
ioannis#ioannis-desktop:~$ python hw3.py
the code for hw3.py:
import mincemeat
import glob
from stopwords import allStopWords
text_files = glob.glob('/home/ioannis/Web Intelligence and Big Data/Week 3: Load - I/hw3data/hw3data/*')
def file_contents(file_name):
f = open(file_name)
try:
# print f.read()
return f.read()
except:
print "exception!!!!!!"
finally:
f.close()
source = dict((file_name, file_contents(file_name))
for file_name in text_files)
def mapfn(key, value):
for line in value.splitlines():
print "I have reach that point!"
...........
...........
def reducefn(k, vs):
result = sum(vs)
return result
s = mincemeat.Server()
s.source = source
s.mapfn = mapfn
s.reducefn = reducefn
results = s.run_server(password="changeme")
print results
In the thread Python, Asyncore and forks, the following suggestion was made:
Change your handle_accept() to return immediately when accept() returns None.
In the file mincemeat.py there is a function:
def handle_accept(self):
conn, addr = self.accept()
sc = ServerChannel(conn, self)
sc.password = self.password
Is the solution to my problem to change something in that function?
s.source = source needs to be s.datasource = source.

Categories