Python-Serial : 2 connections to make it work ?


5 posts
by momo194 » Wed Jul 11, 2012 5:26 am
Hello,

I am Maurice from Belgium. I try to interface my Raspberry Pi with a Picaxe board through a FTDI USB cable.

The test program is simple : "Light up LED on C.6 (picaxe port) when letter "O" received"

So I made the following script in Python :

Code: Select all
import serial
test=serial.Serial("/dev/ttyUSB0",9600)
test.open()
test.write("O")
test.close()


Not working !

But this works perfectly :

Code: Select all
import serial
test=serial.Serial("/dev/ttyUSB0",19200)
test=serial.Serial("/dev/ttyUSB0",9600)
test.open()
test.write("O")
test.close()


I double checked all the RS-232 params (baudrate, bitesize, parity,stopbit) to match them to the one of Picaxe.

So now I have a "solution" but I want a proper one with only one line to connect with 9600 baudrate (First code).

So could someone help ? I don't find any forum or help (except FAQ) on python-serial website.

Thanks in advance,

Maurice
Posts: 2
Joined: Wed Jul 11, 2012 5:18 am
by greypower » Wed Jul 11, 2012 12:07 pm
Maurice,

I had a go with Python 2.7, which seems OK, but Python 3.2 gave problems (NOT like yours) - this problem could be my fault - I MUST read more of the manual on pyserial
(my versions as follow - pyserial 2.6, op.sys wheezy beta)

tests as follows:


Code: Select all
PICAXE 08M2 Code:

main:
disconnect
serrxd b1 ;wait forever
pause 500
sertxd (b1) ;buffered on RPi?
pause 500
reconnect
goto main

PYTHON 2.7

pi@raspberrypi ~ $ python2.7
Python 2.7.3rc2 (default, Apr 23 2012, 04:52:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> test=serial.Serial("/dev/ttyUSB0",4800)
>>> test.open()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 271, in open
    raise SerialException("Port is already open.")
serial.serialutil.SerialException: Port is already open.
>>> test.write("R")
1
>>> c="c"
>>> c=test.read(1)
>>> print(c)
R
>>>

PYTHON 3.2

pi@raspberrypi ~ $ python3.2
Python 3.2.3 (default, Jun 26 2012, 05:19:37)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> test=serial.Serial("/dev/ttyUSB0",4800)
>>> test.open()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.2/dist-packages/serial/serialposix.py", line 271, in open
    raise SerialException("Port is already open.")
serial.serialutil.SerialException: Port is already open.
>>> test.write("R")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.2/dist-packages/serial/serialposix.py", line 475, in write
    n = os.write(self.fd, d)
TypeError: 'str' does not support the buffer interface
>>>


Cheers

Richard
Posts: 91
Joined: Sat Jan 07, 2012 10:00 am
by speculatrix » Wed Jul 11, 2012 12:47 pm
I don't know about the Picaxe, but I have successfully made serial connections between a BeagleBone and both an Arduino and RPi. With the Arduino, when you first make a serial connection to it, it can cause the Arduino board to reset. Have you tried inserting a short pause after first opening the connection?
User avatar
Posts: 48
Joined: Thu Apr 05, 2012 8:29 am
Location: France
by momo194 » Wed Jul 11, 2012 4:35 pm
hello all,

Thanks for replying. I found the solution. The chip is a 28X2 running a default 16Mhz.
I tough it was 8Mhz because 28X2-5V default frequency is 8Mhz.
Check that on : http://www.picaxe.com/docs/picaxex2.pdf

Everything is fine using setfreq m16 and 19200 as baudrate on python-serial.

Maurice
Posts: 2
Joined: Wed Jul 11, 2012 5:18 am
by scotty101 » Fri Jul 13, 2012 6:33 pm
Try a time.sleep(small_time) after you open the port.

I had issues with an arduino where by opening the serial port resets the arduino which takes a few moments to start the main program.
Building a Raspberry Pi Home. https://plus.google.com/100752687760844230040/posts/RtZzoVqDGfy
Posts: 236
Joined: Fri Jun 08, 2012 6:03 pm