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

UART

Fri Feb 01, 2013 7:38 am

how to make serial communication between raspberry pi and my PC using python?.Help me.

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: UART

Fri Feb 01, 2013 8:12 am

first, do the mods to the config.txt and such to disable the serial terminal, then just use the serial port just like you would a usb-serial converter chip, here's an example:

Code: Select all

import serial
usbport = '/dev/ttyAMA0'
ser = serial.Serial(usbport, 9600, timeout=1)

        ser.write(dataout)
        ser.read(datain)
and here's a link to show how to disable the serial terminal so you can use it as a normal serial port.

http://www.hobbytronics.co.uk/raspberry-pi-serial-port

Just be careful because the raspberry pi can only handle 3v logic, so you need a level converter to switch the voltages like a max3232 IC or a logic level shifter.
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

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

Re: UART

Mon Mar 18, 2013 2:04 am

How to send data from PC to RPi using python code through the UART?Please help me on this.Thanks on advance. :)

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART

Tue Mar 19, 2013 9:58 am

It is exactly the same as on the Pi, except that on a Windows PCc the names of the serial ports are "COM1", "COM2", etc. instead of "/dev/ttyAMA0"

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

Re: UART

Wed Mar 20, 2013 2:04 am

here the coding..

Code: Select all

import serial
usbport = 'COM47'
ser = serial.Serial(usbport, 9600, timeout=1)
i cant read the data from my pc.I want read the data from pc and display on raspbery pi.please help. :( :( :(

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: UART

Wed Mar 20, 2013 8:37 am

it is just as I had posted above, use /dev/ttyAMA0
just make sure you use some type of level converter chip or resistor voltage divider so you dont burn up your raspberry pi.

or if your using a usb serial port device instead of hooking it directly to the rpi's pins, use /dev/ttyUSB0 or /dev/ttyUSB1

com47 or any comx ports are a windows specific thing, each system has thier own way of naming serial ports. like windows stuff is com1, linux is /dev/ttyUSB1, and macintosh, it uses the serial numbers of the usb device with tty naming like linux, so it's like /dev/ttyUSB-932141414
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART

Wed Mar 20, 2013 9:12 am

@ thivtulz
We need longer posts please. You are giving us very little information on which to base our answers.

You need:
  • A program on your PC.
  • A serial port on your PC
  • A serial cable
  • A TTL-RS232 voltage converter board
  • To disable the console process on the Pi serial port
  • A program on the Pi
Do you have all these things? Which of them do you need help with?

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: UART

Wed Mar 20, 2013 9:44 am

after thinking about it, it sounds like they're trying to use the rpi as a serial terminal.
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

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

Re: UART

Wed Mar 20, 2013 9:59 am

i want to read data from the YAT terminal and display it on my raspberry pi board.I can send data to the terminal from my raspberry pi board but i can't read data from the terminal.so i need the code to read data from the YAt terminal.i need python code to read data.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART

Wed Mar 20, 2013 10:14 am

Do you have the TTL-RS232 converter board, and have you done the work disabling the serial console tty task in /etc/init.d ?

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: UART

Wed Mar 20, 2013 11:43 am

Also, from the looks of it, you havnt done much in the way of programming in python, I would check out some tutorials in python first bc your code itself. On youtube, look up thenewboston and watch his python tutorials, they're pretty good and easy to catch onto. Even if set to the right location for serial port, wouldnt do much other than just read the data, you'd need a print statement to output the data onto the screen also, like this:

Code: Select all

print serialdata
also, if your just needing to use it as a serial terminal, you should check out minicom program that comes with pyserial library instead, or try using a program like PuTTY.
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

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

Re: UART

Thu Mar 21, 2013 1:57 am

here the coding...

Code: Select all

import serial
usbport = '/dev/ttyAMA0'
ser = serial.Serial(usbport, 9600, timeout=1)
ser.write("Raspberry pi\n")
while 1:
ser.read("Welcome\n")
print ser.read()
ser.close()
when i type welcome in the terminal should display it on the raspberry pi board.

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: UART

Thu Mar 21, 2013 8:54 am

try this instead:

Code: Select all

import serial
usbport = '/dev/ttyAMA0'
ser = serial.Serial(usbport, 9600, timeout=1)
ser.writeline("Raspberry pi")
while 1:
    x = ser.readline()
    print x
    
ser.close()
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

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

Re: UART

Thu Mar 21, 2013 9:27 am

giving error....AttributeError:'Serial' object has no attrtibute 'writeline'

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

Re: UART

Thu Mar 21, 2013 10:10 am

It's keep on loops. Duno how to solve it.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART

Thu Mar 21, 2013 10:32 am

Serial does not provide writeline, but you don't need it; just use "write" instead of "writeline". To end the loop you have to either press Ctrl-C on the Pi, or you need to put in an exit condition. Here's an example that fixes the writeline and allows the loop to end if the serial line receives a ctrl-C character.

Code: Select all

    import serial
    usbport = '/dev/ttyAMA0'
    ser = serial.Serial(usbport, 9600, timeout=1)
    ser.write("Raspberry pi\r\n")
    x = ""
    while 3 not in x:
        x = ser.readline()
        print x
       
    ser.close()
I think that will work, if not change the 3 to chr(3).

You can change the exit condition by changing the character it is looking for. For example, to only receive a single line of text, change the 3 (Ctrl-C) to 13 (Return).

cary
Posts: 6
Joined: Thu Mar 21, 2013 2:37 pm

Re: UART

Thu Mar 21, 2013 2:53 pm

I also meet the same problem and was blocked there a few days.
I can send data from raspberry pi to my pc through UART, but can not do that from pc to raspberry.

This is code on raspberry pi
>>> import serial
>>> ser=serial.Serial('/dev/ttyAMA0', 115200)
>>> ser.isOpen()
True
>>> ser.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 453, in read
buf = os.read(self.fd, size-len(read))
OSError: [Errno 11] Resource temporarily unavailable

Does anyone know the reason, thanks

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART

Thu Mar 21, 2013 3:24 pm

Our old friend EAGAIN (errno=11).

See: http://www.raspberrypi.org/phpBB3/viewt ... le#p264238

All it means is that there was no data immediately available to read.

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

Re: UART

Fri Mar 22, 2013 2:03 am

Code: Select all

import serial
    usbport = '/dev/ttyAMA0'
    ser = serial.Serial(usbport, 9600, timeout=1)
    ser.write("Raspberry pi\r\n")
    x = ""
    while 3 not in x:
        x = ser.readline()
        print x
       
    ser.close()
after i run this code i got this error..TypeError: 'in <string>' requires string as left operand, not int...

i wanna print x on my raspberry pi...how to do so?

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART

Fri Mar 22, 2013 7:33 am

Like I said, if you have problems with that, make the "3" into "chr(3)".

Things changed in Python 2.6, and my best guess was that ser.read returned an array of bytes. It seems it is still a string though, or at least behaves like one.

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

Re: UART

Fri Mar 22, 2013 7:51 am

already try to change and still same.no changes cannot read aso..please help me..don't know what to do.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART

Fri Mar 22, 2013 8:46 am

What is the error message with the chr(3) in place?

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

Re: UART

Fri Mar 22, 2013 8:53 am

NameError:name'char' is not defined...there s any other way to solve the problem.??

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART

Fri Mar 22, 2013 8:56 am

There are no "char"s in the code I wrote. Did you insert "char(3)" instead of "chr(3)"?

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

Re: UART

Fri Mar 22, 2013 8:59 am

sorry wrongly send..the error is TypeError: argument of type 'buitin_function_or_method' is not iterable

Return to “Python”