View Full Version : Python serial port
nipunreddevil
August 9th, 2009, 02:28 AM
How can i use usb-serial converter to perform serial operations using python.pySerial does not mention it ,i guess?And how can we view our ports in linux as we do using device manager in Windows?
jpkotta
August 9th, 2009, 03:41 AM
Install the python-serial package. Then in your program:
import serial
ser = serial.Serial()
ser.port = "/dev/ttyUSB0" # may be called something different
ser.baudrate = 115200 # may be different
ser.open()
if ser.isOpen():
ser.write("hello")
response = ser.read(ser.inWaiting())
In Linux, serial ports are named /dev/ttySx or /dev/ttyUSBx, where "x" is a number.
nipunreddevil
August 9th, 2009, 04:08 AM
Thanks,so i just plug in my usb-serial cable and assume i am working with serial port.How do i know which serial/usb port i am using
jpkotta
August 9th, 2009, 04:21 AM
Thanks,so i just plug in my usb-serial cable and assume i am working with serial port.How do i know which serial/usb port i am using
For a USB serial port adapter, they are numbered in the order that they enumerate. I think there is a way to give a given device the same number every time, but I've never bothered to learn how. Just after plugging it in, you will see some messages in /var/log/messages (or use dmesg) that tell you what number it got.
nipunreddevil
August 9th, 2009, 04:25 AM
Thanks again.Now what i intend to do is write data serially and receive data serially continuously using pyGTK.How shall python programming for this be done.if i use serialin waiting ,i guess it will keep on receving but will not write
nipunreddevil
August 9th, 2009, 05:46 AM
I tried what you told and it worked in idle but when i use it within Geany or Gedit,i get following errors
Traceback most recent call last
File "serial.py",line 3,in module
import serial
File"/home/nipun/serial.py",line 5,in module
ser=serial.Serial()
Attribute error:module object has no attribute Serial
how can i make it work?
jpkotta
August 9th, 2009, 07:35 AM
Did you name your file serial.py? You can't use that name*. Python tries to import files in the current directory (i.e. serial.py) before other locations, so it's not importing the serial.py from python-serial.
* You could use that name, but it requires a workaround, and isn't recommended.
nipunreddevil
August 9th, 2009, 10:27 AM
changing name has not helped,get same errors for prog.py now
Here is prog.py
import serial
ser=serial.Serial()
ser.port="/dev/tyUSB0"
ser.baudrate=9600
ser.open()
ser.write("hi")
nipunreddevil
August 9th, 2009, 10:46 AM
Thanks,got it working
Had forgotten to delete previous file -serial.py
topdoguk
November 17th, 2009, 11:57 PM
As a newbie i have had exactly the same problem, been tearing my hair out for days.
I had also created a file called serial.py once I deleted this file my program run perfectly.
Thanks for this posting. I bet there's plenty more people out there caught out by this one!
nipunreddevil
November 20th, 2009, 03:33 PM
Yes such things should be mentioned in PySerial website
jpkotta
November 21st, 2009, 03:37 AM
Yes such things should be mentioned in PySerial website
They really shouldn't, if you're referring to the problems caused by having a source file named serial.py. This has absolutely nothing to do with PySerial; it has everything to do with how Python works. It would be like library authors documenting the fact that you need to use syntactic indentation (http://en.wikipedia.org/wiki/Python_(programming_language)#Indentation) when you use their libraries.
LanceRooke
January 15th, 2012, 02:37 AM
Ok, how do I use this script to get my touchscreen working?
http://pastebin.com/yLQ1hbwt
It's from trendystephen article on getting touchscreen to work on Ubuntu on the Fujitsu B3020D.
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.