Coverage for pyilper/pilrs232.py: 77%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

62 statements  

1#!/usr/bin/python3 

2# -*- coding: utf-8 -*- 

3# pyILPER 1.2.1 for Linux 

4# 

5# An emulator for virtual HP-IL devices for the PIL-Box 

6# derived from ILPER 1.4.5 for Windows 

7# Copyright (c) 2008-2013 Jean-Francois Garnier 

8# C++ version (c) 2013 Christoph Gießelink 

9# Python Version (c) 2015 Joachim Siebold 

10# 

11# This program is free software; you can redistribute it and/or 

12# modify it under the terms of the GNU General Public License 

13# as published by the Free Software Foundation; either version 2 

14# of the License, or (at your option) any later version. 

15# 

16# This program is distributed in the hope that it will be useful, 

17# but WITHOUT ANY WARRANTY; without even the implied warranty of 

18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19# GNU General Public License for more details. 

20# 

21# You should have received a copy of the GNU General Public License 

22# along with this program; if not, write to the Free Software 

23# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 

24# 

25# serial device object class -------------------------------------------- 

26# 

27# Changelog 

28#  

29# 07.06.2014 jsi: 

30# - baudrate support 

31# 06.10.2015 jsi: 

32# - class statement syntax update 

33# 16.05.2016 jsi 

34# - use Windows device naming syntax for serial device (hint by cg) 

35# 03.07.2016 jsi 

36# - introduced setBaudrate and flushInput methods 

37# 11.07.2016 jsi 

38# - use platform function from pilcore.py 

39# 

40import serial,time 

41from .pilcore import isWINDOWS 

42 

43# 

44class Rs232Error(Exception): 

45 def __init__(self,value): 

46 self.value=value 

47 def __str__(self): 

48 return repr(self.value) 

49 

50 

51class cls_rs232: 

52 

53 def __init__(self,parent=None): 

54 self.__device__= None 

55 self.__isOpen__= False 

56 self.__timeout__=0 

57 

58# 

59# Windows needs much time to reconfigure the timeout value of the serial 

60# device, so alter the device settings only if necessary. Give the device 

61# some time to settle. 

62# 

63 def __settimeout__(self,timeout): 

64 if timeout != self.__timeout__: 

65 try: 

66 self.__ser__.timeout= timeout 

67 except: 

68 raise Rs232Error('cannot set timeout value of serial device') 

69 self.__timeout__=timeout 

70 

71 def isOpen(self): 

72 return self.__isOpen__ 

73 

74 

75 def open(self,device,baudrate): 

76# 

77# use Windows device naming (hint by cg) 

78# 

79 if isWINDOWS(): 

80 self.__device__="\\\\.\\"+device 

81 else: 

82 self.__device__= device 

83 

84 self.__device__= device 

85 try: 

86 self.__ser__= serial.Serial(port=device,baudrate=baudrate,timeout=0.10) 

87 self.__isOpen__= True 

88 time.sleep(0.5) 

89 except: 

90 self.__device__="" 

91 raise Rs232Error('cannot open serial device') 

92 def close(self): 

93 try: 

94 self.__ser__.close() 

95 self.__isOpen__=False 

96 except: 

97 raise Rs232Error('cannot close serial device') 

98 self.__device__="" 

99 

100 def snd(self,buf): 

101 try: 

102 self.__ser__.write(buf) 

103 except: 

104 raise Rs232Error('cannot write to serial device') 

105 

106 def rcv(self,timeout): 

107 self.__settimeout__(timeout) 

108 try: 

109 c= self.__ser__.read(1) 

110 except: 

111 raise Rs232Error('cannot read from serial device') 

112 return c 

113 

114 def flushInput(self): 

115 try: 

116 self.__ser__.flushInput() 

117 except: 

118 raise Rs232Error('cannot reset serial device') 

119 

120 def setBaudrate(self,baudrate): 

121 try: 

122 self.__ser__.baudrate= baudrate 

123 except: 

124 raise Rs232Error('cannot set baudrate')