summaryrefslogtreecommitdiffstats
path: root/pyprofibus/phy_serial.py
blob: a10e23d4101b1a8258139e2a08a00866ac00e4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
#
# PROFIBUS DP - Communication Processor PHY access library
#
# Copyright (c) 2016-2021 Michael Buesch <m@bues.ch>
#
# Licensed under the terms of the GNU General Public License version 2,
# or (at your option) any later version.
#

from __future__ import division, absolute_import, print_function, unicode_literals
from pyprofibus.compat import *

from pyprofibus.phy import *
from pyprofibus.fdl import FdlTelegram
from pyprofibus.util import *

import sys

try:
	import serial
except ImportError as e:
	if "PyPy" in sys.version and\
	   sys.version_info[0] == 2:
		# We are on PyPy2.
		# Try to import CPython2's serial.
		import glob
		sys.path.extend(glob.glob("/usr/lib/python2*/*-packages/"))
		import serial
	else:
		raise e
try:
	import serial.rs485
except ImportError:
	pass

__all__ = [
	"CpPhySerial",
]

class CpPhySerial(CpPhy):
	"""pyserial based PROFIBUS CP PHYsical layer
	"""

	__slots__ = (
		"__discardTimeout",
		"__rxBuf",
		"__serial",
	)

	PFX = "PHY-serial: "

	def __init__(self, port, useRS485Class=False, *args, **kwargs):
		"""port => "/dev/ttySx"
		debug => enable/disable debugging.
		useRS485Class => Use serial.rs485.RS485, if True. (might be slower).
		"""
		super(CpPhySerial, self).__init__(*args, **kwargs)
		self.__discardTimeout = None
		self.__rxBuf = bytearray()
		try:
			if useRS485Class:
				if not hasattr(serial, "rs485"):
					raise PhyError("Module serial.rs485 "
						"is not available. "
						"Please use useRS485Class=False.")
				self.__serial = serial.rs485.RS485()
			else:
				self.__serial = serial.Serial()
			self.__serial.port = port
			self.__serial.baudrate = CpPhy.BAUD_9600
			self.__serial.bytesize = 8
			self.__serial.parity = serial.PARITY_EVEN
			self.__serial.stopbits = serial.STOPBITS_ONE
			self.__serial.timeout = 0
			self.__serial.xonxoff = False
			self.__serial.rtscts = False
			self.__serial.dsrdtr = False
			if useRS485Class:
				self.__serial.rs485_mode = serial.rs485.RS485Settings(
					rts_level_for_tx = True,
					rts_level_for_rx = False,
					loopback = False,
					delay_before_tx = 0.0,
					delay_before_rx = 0.0
				)
			self.__serial.open()
		except (serial.SerialException, ValueError) as e:
			raise PhyError("Failed to open "
				"serial port:\n" + str(e))

	def close(self):
		try:
			self.__serial.close()
		except serial.SerialException as e:
			pass
		self.__rxBuf = bytearray()
		super(CpPhySerial, self).close()

	def __discard(self):
		s = self.__serial
		if s:
			s.flushInput()
			s.flushOutput()
		if monotonic_time() >= self.__discardTimeout:
			self.__discardTimeout = None

	def __startDiscard(self):
		self.__discardTimeout = monotonic_time() + 0.01

	# Poll for received packet.
	# timeout => In seconds. 0.0 = none, Negative = unlimited.
	def pollData(self, timeout=0.0):
		if timeout > 0.0:
			timeoutStamp = monotonic_time() + timeout
		ret = None
		rxBuf = self.__rxBuf
		ser = self.__serial
		size = -1
		getSize = FdlTelegram.getSizeFromRaw

		while self.__discardTimeout is not None:
			self.__discard()
			if timeout > 0.0 and monotonic_time() >= timeoutStamp:
				return None

		try:
			rxBufLen = len(rxBuf)
			while True:
				if rxBufLen < 1:
					rxBuf.extend(ser.read(1))
				elif rxBufLen < 3:
					if size < 0:
						size = getSize(rxBuf)
					readLen = (size if size > 0 else 3) - rxBufLen
					if readLen > 0:
						rxBuf.extend(ser.read(readLen))
				elif rxBufLen >= 3:
					if size < 0:
						size = getSize(rxBuf)
						if size < 0:
							if self.debug and rxBuf:
								self._debugMsg("RX (fragment)   %s" % bytesToHex(rxBuf))
							rxBuf = bytearray()
							self.__startDiscard()
							raise PhyError("PHY-serial: "
								"Failed to get received telegram size: "
								"Invalid telegram format.")
					if rxBufLen < size:
						rxBuf.extend(ser.read(size - rxBufLen))

				rxBufLen = len(rxBuf)
				if rxBufLen == size:
					ret = rxBuf
					rxBuf = bytearray()
					rxBufLen = 0
					break

				if (timeout == 0.0 or
				    (timeout > 0.0 and monotonic_time() >= timeoutStamp)):
					break
		except serial.SerialException as e:
			if self.debug and rxBuf:
				self._debugMsg("RX (fragment)   %s" % bytesToHex(rxBuf))
			rxBuf = bytearray()
			self.__startDiscard()
			raise PhyError("PHY-serial: Failed to receive "
				"telegram:\n" + str(e))
		finally:
			self.__rxBuf = rxBuf
		if self.debug and ret:
			self._debugMsg("RX   %s" % bytesToHex(ret))
		return ret

	def sendData(self, telegramData, srd):
		if self.__discardTimeout is not None:
			return
		try:
			if self.debug:
				self._debugMsg("TX   %s" % bytesToHex(telegramData))
			self.__serial.write(telegramData)
		except serial.SerialException as e:
			raise PhyError("PHY-serial: Failed to transmit "
				"telegram:\n" + str(e))

	def setConfig(self, baudrate=CpPhy.BAUD_9600, rtscts=False, dsrdtr=False, *args, **kwargs):
		wellSuppBaud = (9600, 19200)
		if baudrate not in wellSuppBaud and not isMicropython:
			# The hw/driver might silently ignore the baudrate
			# and use the already set value from __init__().
			self._warningMsg("The configured baud rate %d baud "
					 "might not be supported by the hardware. "
					 "Note that some hardware silently falls back "
					 "to 9600 baud for unsupported rates. "
					 "Commonly well supported baud rates by serial "
					 "hardware are: %s." % (
					 baudrate,
					 ", ".join(str(b) for b in wellSuppBaud)))
		try:
			if baudrate != self.__serial.baudrate or rtscts != self.__serial.rtscts or dsrdtr != self.__serial.dsrdtr:
				self.__serial.close()
				self.__serial.baudrate = baudrate
				self.__serial.rtscts = rtscts
				self.__serial.dsrdtr = dsrdtr
				self.__serial.open()
				self.__rxBuf = bytearray()
		except (serial.SerialException, ValueError) as e:
			raise PhyError("Failed to set CP-PHY "
				"configuration:\n" + str(e))
		self.__setConfigPiLC(baudrate)
		super(CpPhySerial, self).setConfig(baudrate=baudrate,
						   rtscts=rtscts,
						   dsrdtr=dsrdtr,
						   *args, **kwargs)

	def __setConfigPiLC(self, baudrate):
		"""Reconfigure the PiLC HAT, if available.
		"""
		try:
			import libpilc.raspi_hat_conf as raspi_hat_conf
		except ImportError as e:
			return
		if not raspi_hat_conf.PilcConf.havePilcHat():
			return
		try:
			conf = raspi_hat_conf.PilcConf()
			conf.setBaudrate(baudrate / 1000.0)
		except raspi_hat_conf.PilcConf.Error as e:
			raise PhyError("Failed to configure PiLC HAT:\n%s" %\
				str(e))
bues.ch cgit interface