thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

python uart

Thu Mar 28, 2013 9:29 am

here this my coding..

Code: Select all

import serial
usbport = '/dev/ttyAMA0'
ser = serial.Serial(usbport, 9600, timeout=1)
ser.write("Raspberry pi\r\n")
ser.timeout = 2
print ("write data = Raspberry pi")
time.sleep(2)

while True:
           response=ser.read()
           time.sleep=2
           print (" read data: python \n")
           print (response)
  
ser.close()
i can't read data from my PC.Please help me..

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: python uart

Thu Mar 28, 2013 9:21 pm

What is the output when you run the python file?

ser.read() will only return 1 byte of data.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: python uart

Fri Mar 29, 2013 1:20 am

the output is

Code: Select all

write data:Rasperry pi
read data: 
read data:
its continue looping.I can write something in my PC but can't receive data or input from PC.please help me.Don't know how to do.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: python uart

Fri Mar 29, 2013 3:36 pm

Can you explain how you have connected the two devices together?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: python uart

Sun Mar 31, 2013 6:43 am

i connect RPi to the PC using UART cable.That cable connect with the USB port.Then i use YAT terminal in my PC to send data from RPi by running the python code.But the problem is i can't receive data from PC.Please help me ..

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: python uart

Sun Mar 31, 2013 3:38 pm

Does your PC receive the string "Raspberry pi" (output from RPi with the ser.write(), if I understand the setup correctly)?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: python uart

Sun Mar 31, 2013 4:12 pm

yes my PC can receive Raspberry pi but couldn't send from PC.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: python uart

Sun Mar 31, 2013 5:22 pm

Ok, that's promising - at least the connection seems ok ...of course there may be an issue with the RX line on the RPI side...

There's at least one clear issue in your code:

Code: Select all

time.sleep=2
inside the while loop should most likely read:

Code: Select all

time.sleep(2)
The sleep before the while loop seems unnecessary. Also not sure if the line ser.timeout should be there (as the timeout is already set in the ser init).
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: python uart

Mon Apr 01, 2013 8:11 am

Still i can't receive data from PC.Is there any other coding to receive data from PC.help me.How python can communicate with UART.

DMcK
Posts: 20
Joined: Sun Jul 01, 2012 4:51 am

Re: python uart

Tue Apr 02, 2013 1:05 am

Using the code below, I can send and receive from the Pi in Raspbian and xbian. I can't get it to receive using raspbmc:

Code: Select all

import time
import serial
ser = serial.Serial(port='/dev/ttyAMA0', baudrate=38400, timeout=1)
ser.write("\x02**INP\x03")
time.sleep(0.05)
result=ser.read(ser.inWaiting())
if "INPS04" in result:
 ser.write("\x02**INPS05\x03")
else:
 ser.write("\x02**INPS04\x03")
ser.close()
So, it may be the distribution you are using causing issues (like raspbmc for me), or you could try reading the data on the port with "result=ser.read(ser.inWaiting())"

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: python uart

Tue Apr 02, 2013 3:03 am

same problem can't receive from PC.I send data from RPi to PC(windows) can receive but reverse can't. RPi can't receive data from PC(windows)

thivtulz
Posts: 56
Joined: Tue Dec 18, 2012 10:05 am

Re: python uart

Tue Apr 02, 2013 7:26 am

After i run this code the YAT terminal(PC) ask me to login..
Lyk tis..

<LF>raspberry login:
<LF>password:

if i login also i can't run the code.Help me please..

thefatmoop
Posts: 6
Joined: Sat Jan 17, 2015 8:11 pm

Re: python uart

Sat Jan 17, 2015 8:14 pm

Bit late to the party but for those stuck on this:
in a terminal:
$sudo raspi-config

go to advanced options -> enable/disable shell and kernel messages on serial connection -> disable it.

$sudo reboot

Now nothing is hogging up /dev/ttyAMA0.

Return to “Python”