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)
Related
I need to interface an old machine (thermostream) to interface with the raspberry pi (model B+)
The thermostream device has a RS232 serial port and I want to connect it to the USB port of the raspberry pi using the RS232 serial to usb cable (where the usb end of the cable is inserted in the pi and the serial end is connected to the device). And I need to write the code in python.
IS this possible? If yes, how should I proceed? Any help is greatly appreciated.
Yes. First you need to install pyserial
Then, in Python, you can use the following function to create a serial object that connects to a port. The usb ports on the pi are dynamically assigned a name and those names can change. This function will enable you to loop through each port for the name. (might not work well with multiple devices attached). Check your connection settings on the device you wish to communicate with (baudrate, parity, stopbits etc) and modify the code to use those settings.
import serial
def serialConnect():
serlocations=['/dev/ttyACM', '/dev/ttyACM0', '/dev/ttyACM1','/dev/ttyACM2', '/dev/ttyACM3','/dev/ttyACM4', '/dev/ttyACM5','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3', '/dev/ttyUSB4', '/dev/ttyUSB5', '/dev/ttyUSB6', '/dev/ttyUSB7', '/dev/ttyUSB8', '/dev/ttyUSB9', '/dev/ttyUSB10','/dev/ttyS0', '/dev/ttyS1', '/dev/ttyS2', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'com10', 'com11', 'com12', 'com13', 'com14', 'com15', 'com16', 'com17', 'com18', 'com19', 'com20', 'com21', 'com1', 'end']
for device in serlocations:
try:
ser = serial.Serial(
port=device,
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)
print device
return ser
except:
x=0
if device == 'end':
print "No Device Found"
ser = serialConnect()
if ser:
ser.write("TEST")
ser.timeout=5
for i in ser.readlines():
print i
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.
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.