1 |
2 |
nuubik |
#! /usr/bin/env python
|
2 |
17 |
nuubik |
# -*- coding: ISO-8859-1 -*-
|
3 |
2 |
nuubik |
|
4 |
|
|
|
5 |
|
|
##########################################################################
|
6 |
|
|
# USPP Library (Universal Serial Port Python Library)
|
7 |
|
|
#
|
8 |
|
|
# Copyright (C) 2006 Isaac Barona <ibarona@gmail.com>
|
9 |
|
|
#
|
10 |
|
|
# This library is free software; you can redistribute it and/or
|
11 |
|
|
# modify it under the terms of the GNU Lesser General Public
|
12 |
|
|
# License as published by the Free Software Foundation; either
|
13 |
|
|
# version 2.1 of the License, or (at your option) any later version.
|
14 |
|
|
#
|
15 |
|
|
# This library is distributed in the hope that it will be useful,
|
16 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
18 |
|
|
# Lesser General Public License for more details.
|
19 |
|
|
|
20 |
|
|
# You should have received a copy of the GNU Lesser General Public
|
21 |
|
|
# License along with this library; if not, write to the Free Software
|
22 |
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
23 |
|
|
##########################################################################
|
24 |
|
|
|
25 |
|
|
#-------------------------------------------------------------------------
|
26 |
|
|
# Project: USPP Library (Universal Serial Port Python Library)
|
27 |
|
|
# Name: uspp.py
|
28 |
|
|
# Purpose: Main module. Imports the correct module for the platform
|
29 |
|
|
# in which it is running.
|
30 |
|
|
#
|
31 |
|
|
# Author: Isaac Barona Martinez <ibarona@gmail.com>
|
32 |
|
|
# Copyright: (c) 2006 by Isaac Barona Martinez
|
33 |
|
|
# Licence: LGPL
|
34 |
|
|
#
|
35 |
|
|
# Created: 26 June 2001
|
36 |
|
|
# History:
|
37 |
|
|
# 05/08/2001: Release version 0.1.
|
38 |
|
|
# 24/02/2006: Final version 1.0.
|
39 |
|
|
#
|
40 |
|
|
#-------------------------------------------------------------------------
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
"""
|
44 |
|
|
USPP - Universal Serial Port Python Library
|
45 |
|
|
|
46 |
|
|
This module exports a SerialPort class to access serial ports in
|
47 |
|
|
different platforms (currently W9X and Linux) with the same code.
|
48 |
|
|
|
49 |
|
|
When the library user import the uspp module, it automatically
|
50 |
|
|
loads the correct class implementation for the platform in which
|
51 |
|
|
is running.
|
52 |
|
|
|
53 |
|
|
The public classes, exceptions and methods are the following:
|
54 |
|
|
|
55 |
|
|
SerialPortException
|
56 |
|
|
===================
|
57 |
|
|
Exception raised in the SerialPort methods.
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
SerialPort
|
61 |
|
|
==========
|
62 |
|
|
Class that encapsulate methods to access serial ports. It has the
|
63 |
|
|
following public methods:
|
64 |
|
|
|
65 |
|
|
* __init__(self, dev, timeout=None, speed=None, mode='232', params=None)
|
66 |
|
|
|
67 |
|
|
Create an object to access serial port 'dev'
|
68 |
|
|
|
69 |
|
|
Arguments:
|
70 |
|
|
|
71 |
|
|
dev: String that indicate the name of the port. Possible values are:
|
72 |
|
|
In Linux: '/dev/ttyS0', '/dev/ttyS1', ..., '/dev/ttySX' or
|
73 |
|
|
'/dev/cua0', '/dev/cua1', ..., '/dev/cuaX'
|
74 |
|
|
In W9X: 'COM1', 'COM2', ..., 'COMX'
|
75 |
|
|
|
76 |
|
|
timeout: specifies in milliseconds the inter-byte timeout. Possible
|
77 |
|
|
values are:
|
78 |
|
|
* None: For blocking readings. Time-outs are not used for reading
|
79 |
|
|
operations.
|
80 |
|
|
* 0: For non-blocking readings. The reading operation is to return
|
81 |
|
|
inmediately with the characters that have already been received,
|
82 |
|
|
even if no characters have been received.
|
83 |
|
|
* >0: For time-out readings. A character must be read in less than
|
84 |
|
|
this value.
|
85 |
|
|
|
86 |
|
|
speed: integer that specifies the input and output baud rate to
|
87 |
|
|
use. Possible values are: 110, 300, 600, 1200, 2400, 4800, 9600,
|
88 |
|
|
19200, 38400, 57600 and 115200.
|
89 |
|
|
If None a default speed of 9600 bps is selected.
|
90 |
|
|
|
91 |
|
|
mode: string that specifies RS-232 or RS-485 mode. The RS-485 mode
|
92 |
|
|
is half duplex and use the RTS signal to indicate the
|
93 |
|
|
direction of the communication (transmit or recive).
|
94 |
|
|
Posible values are: '232' or '485'. Default to RS232 mode (the
|
95 |
|
|
only implemented just now).
|
96 |
|
|
|
97 |
|
|
params: list that specifies properties of the serial communication.
|
98 |
|
|
If params=None it uses default values for the number of bits
|
99 |
|
|
per byte (8), the parity (NOPARITY) and the number of stop bits (1)
|
100 |
|
|
else:
|
101 |
|
|
* In W9X: params must be a list with three items setting up the
|
102 |
|
|
these values in this order.
|
103 |
|
|
* In Linux: params must is the termios package mode array to use
|
104 |
|
|
for initialization.
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
* __del__(self):
|
109 |
|
|
|
110 |
|
|
Destroy the SerialPort object and close the serial port. It is a good
|
111 |
|
|
idea that when you finish working with the serial port you explicity
|
112 |
|
|
do: del tty (where tty is a SerialPort object).
|
113 |
|
|
|
114 |
|
|
* fileno(self):
|
115 |
|
|
|
116 |
|
|
Returns the file descriptor of the serial port. This information is
|
117 |
|
|
sometimes necessary for example if you want to use the select function.
|
118 |
|
|
|
119 |
|
|
* read(self, num=1):
|
120 |
|
|
|
121 |
|
|
Read num bytes from the serial port. Depending the timeout value used in
|
122 |
|
|
the inicialitation this operation can be:
|
123 |
|
|
|
124 |
|
|
- Blocking (if timeout=None): The operation blocks until num bytes
|
125 |
|
|
arrive the serial port.
|
126 |
|
|
- Non-blocking (if timeout=0): The operation returns inmediatelly
|
127 |
|
|
with as many as num bytes that were waiting in the serial port to be read.
|
128 |
|
|
- Time-out (if timeout>0): A byte must arrive in less milliseconds than
|
129 |
|
|
the specified. If the number of read bytes is less than num a
|
130 |
|
|
SerialPortException is raised because a time-out has happened.
|
131 |
|
|
|
132 |
|
|
* write(self, s):
|
133 |
|
|
|
134 |
|
|
Write the string s to the serial port.
|
135 |
|
|
|
136 |
|
|
* inWaiting(self):
|
137 |
|
|
|
138 |
|
|
Returns the number of bytes waiting to be read.
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
* flush(self):
|
142 |
|
|
|
143 |
|
|
Discards all characters from the output or input buffer.
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
NOTE ON CHARACTERS AND BYTES
|
148 |
|
|
============================
|
149 |
|
|
|
150 |
|
|
The write and read methods of the SerialPort class expect data
|
151 |
|
|
in string buffers. Do not think this library only works for
|
152 |
|
|
ASCII communications. To interpret string elements as bytes (integer number)
|
153 |
|
|
you only have to use the built-in ord() function. To convert a byte
|
154 |
|
|
into a string element, use chr().
|
155 |
|
|
|
156 |
|
|
Example: Suppose you want to transmit the following three bytes:
|
157 |
|
|
0x02, 0x10, 0x30. You only have to do:
|
158 |
|
|
|
159 |
|
|
packet = ''
|
160 |
|
|
packet = packet + chr(0x02) + chr(0x10) + chr(0x30)
|
161 |
|
|
tty.write(packet)
|
162 |
|
|
|
163 |
|
|
So, you can see the bytes you send or receive as integers or as characters
|
164 |
|
|
depending the situation.
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
"""
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
__author__="Isaac Barona Martinez <ibarona@tid.es>"
|
173 |
|
|
|
174 |
|
|
__copyright__="""
|
175 |
|
|
Copyright (C) 2001 Isaac Barona Martinez <ibarona@tid.es>
|
176 |
|
|
|
177 |
|
|
This library is free software; you can redistribute it and/or
|
178 |
|
|
modify it under the terms of the GNU General Public License
|
179 |
|
|
as published by the Free Software Foundation; version 2 dated
|
180 |
|
|
June, 1991.
|
181 |
|
|
"""
|
182 |
|
|
|
183 |
|
|
__version__="0.1"
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
import sys
|
187 |
12 |
nuubik |
drv_ok = 0
|
188 |
2 |
nuubik |
if sys.platform=='win32':
|
189 |
12 |
nuubik |
print "Windows platform detected:"
|
190 |
|
|
#try:
|
191 |
|
|
# import notworking_d2xx
|
192 |
|
|
# from D2xxPort_win import *
|
193 |
|
|
# print "Using D2xx FTDI driver"
|
194 |
|
|
# drv_ok = 1
|
195 |
|
|
#except ImportError:
|
196 |
|
|
# print "pyUSB for fast COM not found (see http://bleyer.org/pyusb/)"
|
197 |
|
|
|
198 |
|
|
if drv_ok == 0:
|
199 |
|
|
try:
|
200 |
|
|
from win32file import *
|
201 |
|
|
from win32event import *
|
202 |
|
|
from SerialPort_win import *
|
203 |
|
|
print "Using VCP FTDI driver"
|
204 |
17 |
nuubik |
except ImportError,SerialPortException:
|
205 |
12 |
nuubik |
print "Python for winiows extensions for COM not found"
|
206 |
|
|
print "(see https://sourceforge.net/projects/pywin32/)"
|
207 |
|
|
print "Could not find any usable support for FTDI chip in python"
|
208 |
|
|
print "Try installing python support from one of the links."
|
209 |
|
|
sys.exit()
|
210 |
|
|
|
211 |
|
|
|
212 |
2 |
nuubik |
elif sys.platform=='linux2':
|
213 |
12 |
nuubik |
print "Linux platform detected:"
|
214 |
2 |
nuubik |
from SerialPort_linux import *
|
215 |
|
|
else:
|
216 |
17 |
nuubik |
sys.exit('Sorry, no implementation for this platform yet')
|