i tried to use the following code:
import serial
ser = serial.Serial()
ser.baudrate = 56700
ser.port = 'COM6'
ser.open() # HERE IS THE ERROR
When i do it from Python shell, typing line by line, it works and i can recieve data from a bluetooth device. But when i run it from cmd (C:\>python serial.py) it raises the error "could not open port COM6: Element not found". I can see in the bluetooth device that it connects for a second and then disconnects.
Anyone know what this is?
BTW, Im using Windows 7 64-Bits and Python 2.7. Thanks!
Perhaps you forgot to run the Command prompt as Administrator?
I had the same problem. I fixed it by adding time.sleep(5) around ser.open(). So it would look like this:
import serial
import time
ser = serial.Serial()
ser.baudrate = 56700
ser.port = 'COM6'
time.sleep(5)
ser.open()
time.sleep(5)
I didn't experiment much with the code, but you probably don't have to sleep for 5 seconds, you can probably just sleep for 0.1 seconds and it would still work. This is probably not the best way to fix it, but it works.
Related
I want to communicate between Raspberry pi 3 B+ and GSM GPRS A6. I tried and I am unable to send data to GPRS Module from Raspberry pi.
Now, I know that GPIO serial port is disabled by default in newer Operating Systems (in my case Raspbian Stretch), so I have enabled it by adding following line in config.txt file,
enable_uart=1
Here's my Code:
import serial
import time
port = "/dev/ttyS0"
COMM = serial.Serial(port, baudrate=115200)
while(1):
COMM.write("AT\r")
print (COMM.read(5))
This command is supposed to return "OK", but it does not and nothing is printed. I am using python 2.7.
Some people suggested me to send data using this method,
COMM.write('AT' + '\r')
I tried but it didn't help.
There is no problem with my GPRS module. It works file with arduino.
So, what am I doing wrong here?
Thanks in advance!
,
First , be sure to enable the Serial.
sudo raspi-config -> Interfacing Option -> Serial
Second , sudo nano /boot/cmdline.txt
Delete "console=serial,115200"
And Then
sudo nano /boot/config.txt
Add the end
dtoverlay=pi3-disable-bt
core_freq=250
While you use : Serial(/dev/ttyAMA0,9600)
try sending:
import serial
port = "/dev/ttyS0"
comm = serial.Serial(port, baudrate=115200)
while True:
comm.write('AT' + '\n\r')
msg = comm.readline()
print(msg)
I'm trying to open a port COM on my computer using Python.
Here is my code
import serial
ser = serial.Serial()
ser.port = 4
ser.baudrate = 9600
ser.open()
When I execute my code line by line on the Python shell, the ser.open() generates the following error :
I SerialException: could not open port 'COM5': WindowsError(2, 'Le fichier sp\xe9cifi\xe9 est introuvable.')
it means : sp\xe9cifi\xe9 not found !!
I don't know what's happening with this file :/ and how I can fix this issue.
If need any further information please don't hesitate.
thanks for the help.
The solution was to put plug something in the Port COM5 to open it !!
You can't open the port if there is nothing physically plugged in it
I am using Python 2.7 and serial v 2.6 to listen to a serial port. I can listen to the port just fine, but I cannot write to the port.
import serial
cp = 5
br = 9600
ser = serial.Serial(5,br)
a = ser.readline()
Using this, I can listen to the outcoming data stream. However, if I want to change the status of the instrument (e.g. set GPS to off) I would write a command:
ser.write('gps=off')
When I do this, I get "6L" returned and the gps stay on. However, if I connect via TeraTerm I can see the data stream in in real time. While the data streams in I can type gps=off followed by a return and suddenly my GPS is off. Why is my command in Python not working like TeraTerm?
UPDATE
If I instead do
a = ser.write('gps=on')
"a" is assigned value of 6. I also tried sending a "junk" command via
a = ser.write('lkjdflksdjflksdjf')
with "a" assigned a value of 17, so it seems to be assigning the length of the string to a, which does not make sense.
I think the problem was the ser.write commands were getting stuck in buffer (I am not sure of that, but that is my suspicion). When I checked the input buffer I found it to be full. After flushing it out I was able to write to the instrument.
import serial
ser = serial.Serial(5, 9600)
# the buffer immediately receives data, so ensure it is empty before writing command
while ser.inWaiting()>0:
ser.read(1)
# now issue command
ser.write('gps=off\r')
That works.
I Have Connected a Telepresence Device serially to my PC and it is connected successfully but i am not able to read or write the Data from the Device,Can Any Buddy Help me..
Here is my Python Script.I Need my script to Execute the command whatever i give in its command prompt.
import serial
ser = serial.Serial()
ser.braudrate = 115200
ser.port = "/dev/ttyUSB0"
ser.open()
print ser.name
if ser.isOpen():
print True
elif ser.isOpen():
print False
ser.readall()
print ser
ser.write("hello\n")
print ser
I'm having a similar problem right now, but, in your case, instead of using ser.write() I'd try using ser.writelines. If you are still looking for documentation try here.
In the blocking way I can do this:
from scapy.all import *
sniff(filter"tcp and port 80", count=10, prn = labmda x:x.summary())
# Below code will be executed only after 10 packets have been received
do_stuff()
do_stuff2()
do_stuff3()
I want to be able to sniff packets with scapy in a non blocking way, something like this:
def packet_recevied_event(p):
print "Packet received event!"
print p.summary()
# The "event_handler" parameter is my wishful thinking
sniff(filter"tcp and port 80", count=10, prn=labmda x:x.summary(),
event_handler=packet_received_event)
#I want this to be executed immediately
do_stuff()
do_stuff2()
do_stuff3()
To sum-up: My question is pretty clear, I want to be able to continue executing code without the sniff function blocking it.
One option is to open a separate thread for this, but I would like to avoid it and use scapy native tools if possible.
Environment details:
python: 2.7
scapy: 2.1.0
os: ubuntu 12.04 64bit
This functionality was added in https://github.com/secdev/scapy/pull/1999.
I'll be available with Scapy 2.4.3+ (or the github branch). Have a look at the doc over: https://scapy.readthedocs.io/en/latest/usage.html#asynchronous-sniffing
>>> t = AsyncSniffer(prn=lambda x: x.summary(), store=False, filter="tcp")
>>> t.start()
>>> time.sleep(20)
>>> t.stop()
Scapy doesn't have an async version of the sniff function. You're going to have to fire threads.
There may be other issues with this, mostly having to do with resource locking.