I am currently trying to setup a serial connection between my computer (windows 10) and my Raspberry Pi 3 via the Bluetooth connection.
On windows I have managed to attribute COM4 to the Raspberry Bluetooth connection.
On the Raspberry Rfcomm0 is setup and I use '/dev/ttyAMA0' as port Id detected
For the code I use:
On Windows:
import time
import glob
import serial
import sys
def serial_ports():
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
if __name__ == '__main__':
print(serial_ports())
ser = serial.Serial(
port=serial_ports()[0],
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1,
writeTimeout=0
)
counter=0
str(4)
while counter<20:
print'Write counter: %d \n'%(counter)
ser.flush()
ser.write(str(4))
ser.write(b's')
ser.write(('Write counter: %d \n'%(counter))) #('Write counter: %d \n'%(counter))
time.sleep(1)
counter += 1
ser.close()
On the Raspberry Pi 3 I have:
import time
import serial
# '/dev/tty1'
# '/dev/ttyAMAO'
# '/dev/rfcomm1'
ser = serial.Serial(
port='/dev/ttyAMAO',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1,
writeTimeout=0
)
counter=0
while 1:
x=ser.readline()
print x
Others information:
- The Bluetooth connection is ON
- The Windows Python Have the hand over the COM port
- The RPi3 Python have the hand over the com port
The problem is: My windows Python send letters that my Pi Python doesn't see...
(in other word no echo)