AttributeError: module 'serial' has no attribute 'reset_input_buffer' - python

Hey guys I have a problem with the command 'rest_input_buffer'.
serial.Serial for example works. I've got the latest version of Thonny 3.3.3, Python 3.7.3 and pyserial version 3.5
The important input:
import serial
ser = serial.Serial('/dev/ttyACM0',baudrate = 9600, timeout = 1)
serial.reset_input_buffer()
This is my output:
AttributeError: module 'serial' has no attribute 'reset_input_buffer'
Any ideas? I already uninstall pyserial and installed it again, same with python. I get a similiar error with the former command 'flushInput()'

You already start your serial session with:
ser = serial.Serial('/dev/ttyACM0',baudrate = 9600, timeout = 1)
Try to flush your buffer like this:
ser.reset_input_buffer()

Related

UnetSocket send() returns a Nonetype object

I am trying out the python code specified in the UnetStack Handbook. While running tx.py and rx.py, UnetSocket object is created successfully as I print it out on the terminal, but the send() function sends Nonetype data at the receiver.
tx.py ===>>
from unetpy import UnetSocket
s = UnetSocket('localhost',1101)
print(s)
s.send('hellooo',31)
s.close()
rx.py =====>>>
from unetpy import UnetSocket
s = UnetSocket('localhost',1102)
rx = s.receive()
print('here ',rx)
print('from node : ',bytearray(rx.data).decode())
s.close()
First I run 2-node-network.groovy on the Simulator.
Then rx.py and next tx.py from the terminal.
Error at rx.py
Traceback (most recent call last):
File "rx.py", line 6, in
print('from node : ',bytearray(rx.data).decode())
AttributeError: 'NoneType' object has no attribute 'data'
O/p at tx.py
<unetpy.UnetSocket object at 0x7fe2909d4550>
I managed to reproduce your problem with the latest versions of unetpy + fjagepy. Seems to be a bug introduced in 1.7.1 release of fjagepy (I am assuming you have version 1.7.1 installed). Try:
pip install fjagepy==1.7.0
and then repeat your test to see if it works.
P.S. I have reported the problem to the maintainer for fjågepy and so hopefully we should have a fix in the next release. Until then you can use 1.7.0 release, if that works for you.

How to get DDE server to work in python 3?

In 2015 I have posted a question on SO how to Create DDE server in python and send data continously. The answer and code posted by JayleoPlayGround to that question back then worked flawlessly in python 2.7 and I have used it until recently.
As Python 2 is no longer actively supported from January 2020, I want to move my code to python 3. I have installed pywin32 (version 227) using pip on python 3.7.6 and tried to use the same code as before:
# coded by JayleoPlayGround
# use Portable Python 2.7.5.1 + pywin32-214
import time
import win32ui, dde
from pywin.mfc import object
class DDETopic(object.Object):
def __init__(self, topicName):
self.topic = dde.CreateTopic(topicName)
object.Object.__init__(self, self.topic)
self.items = {}
def setData(self, itemName, value):
try:
self.items[itemName].SetData( str(value) )
except KeyError:
if itemName not in self.items:
self.items[itemName] = dde.CreateStringItem(itemName)
self.topic.AddItem( self.items[itemName] )
self.items[itemName].SetData( str(value) )
ddeServer = dde.CreateServer()
ddeServer.Create('Orbitron')
ddeTopic = DDETopic('Tracking')
ddeServer.AddTopic(ddeTopic)
while True:
yourData = time.ctime() + ' UP0 DN145000001 UMusb DMfm AZ040 EL005 SNNO SATELLITE'
ddeTopic.setData('Tracking', yourData)
win32ui.PumpWaitingMessages(0, -1)
time.sleep(0.1)
When running the above code in python 3.7.6 and using pywin32 (version 227), the external DDE client application that I interface with is able to connect to the DDE server, but the data string is not received correctly. As described before, if I am using Python 2.7 with pywin32 (version 214) this works fine however.
As there are no error messages shown I am lost what the problem is under python 3. I tried all available pywin32 versions for this python version (222 to 227) without success. Any ideas on how to get this to work would be much appreciated.

'Serial' object has no attribute 'is_open'

I've been using code on my RPi2 to communicate to an RS485 Shield to drive various relays. I recently got a RPi3, and the code that has previously worked on the RPi2 has an error on the RPi3.
To begin with, I know that the uart (/dev/ttyAMA0) is "stolen" on the RPi3 for the bluetooth controller. Using this post, I reassigned the uart to the GPIO header so the RS485 shield should work as before. I give you this history, even though I suspect the problem is not with the hardware per se.
Here's the problem. When I execute the code below on the RPi3, I get an error:
Traceback (most recent call last):
File "serialtest.py", line 15, in <module>
if usart.is_open:
AttributeError: 'Serial' object has no attribute 'is_open'
Obviously, within the pySerial library, the serial object DOES have the 'is_open' attribute. Any suggestions on why this error is thrown? I haven't found any references to this specific error in web searches.
#!/usr/bin/env python
import serial
import time
import binascii
data = "55AA08060100024D5E77"
usart = serial.Serial ("/dev/ttyAMA0",19200)
usart.timeout = 2
message_bytes = data.decode("hex")
try:
usart.write(message_bytes)
#print usart.is_open # True for opened
if usart.is_open:
time.sleep(0.5)
size = usart.inWaiting()
if size:
data = usart.read(size)
print binascii.hexlify(data)
else:
print('no data')
else:
print('usart not open')
except IOError as e :
print("Failed to write to the port. ({})".format(e))
If you have an old version of pyserial on the Raspberry Pi, pyserial might not have the is_open, but isOpen() method instead. The isOpen() method was depricated in version 3.0 according to the documentation. You can check the pyserial version with serial.VERSION.

AttributeError: module 'socket' has no attribute 'AF_PACKET'

I am working on building a packet sniffing program using Python, however I have hit a speed bump. For some reason I think socket has not imported properly, because I am getting the following message when my program is run: AttributeError: module 'socket' has no attribute 'AF_PACKET'
I am using OS X and Pycharm is my IDE and I am running the latest version of Python if that helps.
Anyways here is my complete program so far:
import struct
import textwrap
import socket
def main():
connection = socket.socket(socket.AF_PACKET, socket.SOCKET_RAW, socket.ntohs(3))
while True:
rawData, address = connection.recvfrom(65535)
reciever_mac, sender_mac, ethernetProtocol, data = ethernet_frame(rawData)
print('\nEthernet Frame: ')
print('Destination: {}, Source: {}, Protocol: {}'.format(reciever_mac, sender_mac, ethernetProtocol))
# Unpack ethernet frame
def ethernet_frame(data):
reciever_mac, sender_mac, protocol = struct.unpack('! 6s 6s H', data[:14])
return getMacAddress(reciever_mac), getMacAddress(sender_mac), socket.htons(socket), data[14:]
# Convert the Mac address from the jumbled up form from above into human readable format
def getMacAddress(bytesAddress):
bytesString = map('{:02x}'.format, bytesAddress)
macAddress = ':'.join(bytesString).upper()
return macAddress
main()
Thanks for any help in advance!
Actually, AF_PACKET doesn't work on OS X, it works on Linux.
AF_PACKET equivalent under Mac OS X (Darwin)
I ran into this issue on macOS 10.13.1, using Python 3.6.3 and this cool scapy fork that is compatible with python3.
I was using version 0.22 of that tool and as suggested in this issue downgrading to version 0.21 fixed this issue!
In case scapy is not a viable alternative, you could also try the pcap library as suggested in this post (although using python 2 seems to be necessary here).

Python os.startfile stopped working in ubuntu server

I have a little script that runs every day by cron and has been working for the last 3 months.
On 30th September it stopped working with the following error:
File "NumberChecker.py", line 32, in start_server
os.startfile(startfile[0])
AttributeError: 'module' object has no attribute 'startfile'
here is the code for that bit:
def start_server(xml):
startfile = xml.xpath('/config/files/startfile/text()')
try:
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
driver.get('http://www.google.com')
except:
print('no server')
server_status = 'down'
os.startfile(startfile[0])
while server_status == 'down':
try :
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
driver.get('http://www.google.com')
server_status = 'running'
except :
pass
It just tests to see if the selenium server is up or not by requesting google and if not calls a bash script that starts it.
Why would os.startfile suddenly stop working?
I have tried at the command line this:
import os
os.startfile(home/adam/file/start_server.sh)
and I get
File "< stdin >", line 1, in
AttributeError: 'module' object has no attribute 'startfile'
I just can't work out why it has just stopped working?
It is python 3.3 in an virtual environment and the os is ubuntu server 12.04
os.startfile is a Windows specific method, it won't work on linux.

Categories

Resources