Serial Communication Test on Raspberry-pi
- First we have to disable OS from using serial port
- Just for the testing; Connect TxRx pin 8 and 10 on pin header with a jumper(cable).
- Then write to Serial port with following python code.
#!/usr/bin/env python
import time
import serial
ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
counter=0
while 1:
print counter
ser.write('Write counter: %d \n'%(counter))
time.sleep(1)
counter += 1
- Use following code to read it
#!/usr/bin/env python
import time
import serial
ser = serial.Serial(
port='/dev/ttyAMA0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
counter=0
while 1:
x=ser.readline()
print x
Google Docs: Create and edit documents online. |
0 comments:
Post a Comment