Daitomodachi
Posts: 7
Joined: Sun Oct 28, 2012 5:54 pm

Python Serial Problems

Mon Oct 29, 2012 3:14 pm

Okay. So I'm trying to do serial communications on the raspberry pi.

Code: Select all

import serial
import time
arduino= serial.Serial('/dev/ttyAM0',9600)
test.open()
time.sleep(1)
test.close()
When I run the command I keep getting this error
Traceback (most recent call last):
File "/home/pi/serial.py", line 1, in <module>
import serial
File "/home/pi/serial.py", line 3 in <module>
arduino= serial.Serial('/dev/ttyAM0',9600)
AttributeError: /module' object has no attribute 'Serial'

User avatar
hexelpdkk
Posts: 177
Joined: Fri Feb 24, 2012 4:40 pm

Re: Python Serial Problems

Mon Oct 29, 2012 8:19 pm

Just a quick guess, but as your python file is called "serial.py", you might have just imported your own file rather than the serial library. Try changing your file name to "test-serial.py"

Papa G
Posts: 13
Joined: Thu Oct 18, 2012 5:21 pm
Location: Dallas, TX

Re: Python Serial Problems

Tue Oct 30, 2012 1:23 am

Try:

Code: Select all

arduino= serial.Serial('/dev/ttyAMA0',9600)
Programming languages are so literal!

Daitomodachi
Posts: 7
Joined: Sun Oct 28, 2012 5:54 pm

Re: Python Serial Problems

Thu Nov 01, 2012 3:33 am

Okay. So I'm no longer getting that error. Here's my arduino code

Code: Select all

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  Serial.print('Hello World');
}
and here's my python code

Code: Select all

import serial
import time
arduino=serial.Serial('/dev/ttyAMA0',9600)
while 1:
  x=arduino.readline()
  print x
arduino.close()
When I run the code, it does not displaying Hello World multiple times. I'm pretty sure I have everything connected

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Python Serial Problems

Thu Nov 01, 2012 12:56 pm

Daitomodachi wrote:Okay. So I'm no longer getting that error. Here's my arduino code
Try sending "Hello World\n" from Arduino so the end of line "\n" is in the string, then the Python readline function actually has an end of line to know when it is finished reading a line.

I suspect readline is sitting there waiting for an end of line.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Daitomodachi
Posts: 7
Joined: Sun Oct 28, 2012 5:54 pm

Re: Python Serial Problems

Thu Nov 01, 2012 9:13 pm

techpaul wrote:
Daitomodachi wrote:Okay. So I'm no longer getting that error. Here's my arduino code
Try sending "Hello World\n" from Arduino so the end of line "\n" is in the string, then the Python readline function actually has an end of line to know when it is finished reading a line.

I suspect readline is sitting there waiting for an end of line.
I tried that and it still did not work. I rewrote my python code so it will print only once and I directly connected the Arduino to the USB port of the Raspberry pi.

Code: Select all

import serial
import time
arduino=serial.Serial('/dev/ttyUB0',115200)
x=arduino.readline()
print x
arduino.close()
arduino code

Code: Select all

void setup() {
  // initialize serial communication:
  Serial.begin(115200);
}

void loop() {
  Serial.print("Hello World\n");
}

When I ran the code, I only get this
Attachments
download.jpg
download.jpg (33.19 KiB) Viewed 8250 times

User avatar
hexelpdkk
Posts: 177
Joined: Fri Feb 24, 2012 4:40 pm

Re: Python Serial Problems

Fri Nov 02, 2012 7:39 am

That looks like your arduino is not coming up as USB0. Plug it in, and run the command

Code: Select all

dmesg
from a terminal. The last few lines will tell you what the kernel said when you plugged it in, and you should be able to see what the device name is. I think some versions of arduino come up as "/dev/AMA0".

TillM
Posts: 36
Joined: Wed Oct 17, 2012 7:54 am

Re: Python Serial Problems

Fri Nov 02, 2012 11:41 am

This is wrong
arduino=serial.Serial('/dev/ttyUB0',115200)
It should be:

Code: Select all

arduino=serial.Serial('/dev/ttyUSB0', baudrate = 115200, timeout = 2)
You should use a timeout with serial.Serial.readline()
Also try slowing down the baud rate to 9600 and then bump it up when you feel it's working properly

is this correct ? Serial.begin(9600); shouldn't it be Serial.begin(115200);

Daitomodachi
Posts: 7
Joined: Sun Oct 28, 2012 5:54 pm

Re: Python Serial Problems

Fri Nov 02, 2012 12:22 pm

TillM wrote:This is wrong
arduino=serial.Serial('/dev/ttyUB0',115200)
It should be:

Code: Select all

arduino=serial.Serial('/dev/ttyUSB0', baudrate = 115200, timeout = 2)
You should use a timeout with serial.Serial.readline()
Also try slowing down the baud rate to 9600 and then bump it up when you feel it's working properly

is this correct ? Serial.begin(9600); shouldn't it be Serial.begin(115200);
Although I originally had it as Serial.begin(115200) I changed the serial code to the one you had. I finally got some data to display, but I get a few weird characters
Image

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Python Serial Problems

Fri Nov 02, 2012 12:44 pm

Daitomodachi wrote:Although I originally had it as Serial.begin(115200) I changed the serial code to the one you had. I finally got some data to display, but I get a few weird characters
Image
Welcome to the world of communications and synchronistaion.

Those are garbage characters for many reasons
  • Other end was sending before you set up baud rate
  • Previous attempts with baudrate mismatch still in buffer
  • Other end may have actually sent that data
  • Any other reason including problems in making physical connection
See if serial module has a buffer flush function to call after you have set up baud rate.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Return to “Python”