! Programmierung der seriellen Schnittstelle

Angeregt durch die gestrigen ersten Gehversuche von Thomas mit Franz' 
frisch erworbenen \\ Mitsubishi-Industrieroboter per 
[Minicom|http://en.wikipedia.org/wiki/Minicom] zu kommunizieren,
fallen mir spontan zwei Möglichkeiten ein, \\automatisiert Kontakt mit dem Gerät aufzunehmen:

# __Java und [javax.com|http://javax.com/]__, siehe [Javakurs|http://hestia.hs-niederrhein.de/~gkorsch/javakurs/wn8/wn8.htm] der FHN Krefeld zum Thema serielle Schnittstelle \\ Dort wird auch [Hyperterminal|http://de.wikipedia.org/wiki/HyperTerminal] als minicomm-Alternative angesprochen

# __Python & pyserial: siehe [Sourceforge.net|http://sourceforge.net/project/showfiles.php?group_id=46487] für Linux und Co.

Short introduction, ebenfalls von [Sourceforge|http://pyserial.sourceforge.net/]

Open port 0 at "9600,8,N,1", no timeout

 >>> import serial
 >>> ser = serial.Serial(0)  #open first serial port
 >>> print ser.portstr       #check which port was realy used
 >>> ser.write("hello")      #write a string
 >>> ser.close()             #close port

Open named port at "19200,8,N,1", 1s timeout

 >>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
 >>> x = ser.read()          #read one byte
 >>> s = ser.read(10)        #read up to ten bytes (timeout)
 >>> line = ser.readline()   #read a '\n' terminated line
 >>> ser.close()

Open second port at "38400,8,E,1", non blocking HW handshaking

 >>> ser = serial.Serial(1, 38400, timeout=0,
 ...                     parity=serial.PARITY_EVEN, rtscts=1)
 >>> s = ser.read(100)       #read up to one hunded bytes
 ...                         #or as much is in the buffer

Get a Serial instance and configure/open it later

 >>> ser = serial.Serial()
 >>> ser.baudrate = 19200
 >>> ser.port = 0
 >>> ser Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
 >>> ser.open()
 >>> ser.isOpen()
 True
 >>> ser.close()
 >>> ser.isOpen()
 False

Be carefully when using "readline". Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that "readlines" only works with a timeout. "readlines" depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.

Do also have a look at the example files in the examples directory in the source distribution or online in CVS repository .

__für Win32: Serial-Package__ von Roger Burnham, http://www.marshallsoft.com

 >>> from Serial import Serial
 >>> #create a 'configuration dictionary' with the port settings
 >>> myconfig = Serial.PortDict()
 >>> myconfig['port'] = Serial.COM4
 >>> # create and open a port with these settings
 >>> myport = Serial.Port(myconfig)
 >>> myport.open()
 >>> myport.send('ATI\015') #ask modem to identify itself
 >>> print myport.read() #get back an y response

--MarkusMonderkamp, 12.02.2008