<commit_before>"""
Python module presenting an API to an ELM327 serial interface
(C) 2015 <NAME> <<EMAIL>>
http://github.com/fwaggle/pyELM327

Please see License.txt and Readme.md.
"""

import serial, time, pprint, re
import pids

pidlist = pids.__pids


class ELM327(object):
	"""
	ELM327 Class

	Meta-class for abstracting ELM327 device.
	"""
	__ser = None # pySerial object.
	__readBuffer = ''
	__debug = 0

	id = None


	def __init__(self, port, debug=0, baud=38400, rtscts=0, xonxoff=0):
		self.__debug = debug
		self.__ser = serial.Serial(port, baud, timeout=5, rtscts=rtscts, xonxoff=xonxoff)
		self.reset()

	def reset(self):
		"""
		Try to put the ELM327 device into a known state, by resetting
		it then turning off echos.
		"""

		# 'ATZ' will, depending on the status of the device's echo setting
		# return either just the device ID, or 'ATZ\r' followed by the ID.
		self.write('ATZ', nowait=1)
		self.id = self.expect('^ELM327') # Expecting 'ELM327 v1.5'

		# turn off echos
		self.write('ATE0')
		self.expect('^(>)?OK') # should be 'OK'
		self.expect('>')

		# Set protocol == AUTO for a sensible default
		# My holden is then AUTO, SAE J1850 VPW
		self.write('ATSP 0')
		self.expect('^(>)?OK')

	def tryBaudrate(self, rate=38400):
		"""
		Try a faster baud rate for the PC->ELM327 connection. Depending on your hardware,
		this may not work - for example Bluetooth adaptors are notoriously shit.<commit_msg>Add support for warm resets.<commit_after>"""
Python module presenting an API to an ELM327 serial interface
(C) 2015 <NAME> <<EMAIL>>
http://github.com/fwaggle/pyELM327

Please see License.txt and Readme.md.
"""

import serial, time, pprint, re
import pids

pidlist = pids.__pids


class ELM327(object):
	"""
	ELM327 Class

	Meta-class for abstracting ELM327 device.
	"""
	__ser = None # pySerial object.
	__readBuffer = ''
	__debug = 0

	id = None


	def __init__(self, port, debug=0, baud=38400, rtscts=0, xonxoff=0):
		self.__debug = debug
		self.__ser = serial.Serial(port, baud, timeout=5, rtscts=rtscts, xonxoff=xonxoff)
		self.reset()

	def reset(self, warm=0):
		"""
		Try to put the ELM327 device into a known state, by resetting it then 
		turning off echos.

		If the "warm" parameter is non-zero, don't do a full reset - useful for
		keeping custom baud rates and so on.
		"""

		# 'ATZ' will, depending on the status of the device's echo setting
		# return either just the device ID, or 'ATZ\r' followed by the ID.
		# 'ATWS' is preferred if we've changed the baud rate because it doesn't
		# reset all the things.
		if warm:
			self.write('ATWS', nowait=1)
		else:
			self.write('ATZ', nowait=1)
		self.id = self.expect('^ELM327') # Expecting 'ELM327 v1.5'

		# turn off echos
		self.write('ATE0')
		self.expect('^(>)?OK') # should be 'OK'
		self.expect('>')

		# Set protocol == AUTO for a sensible default
		# My holden is then AUTO, SAE J1850 VPW
		self.write('ATSP 0')
		self.expect('^(>)?OK')

	def tryBaudrate(self, rate=38400):
		"""
		Try a faster baud rate for the PC->ELM327 connection. Depending on your hardware,
		this may not work - for example Bluetooth adaptors are notoriously shit.
{'fraction_non_alphanumeric': 0.12244897959183673, 'fraction_numerical': 0.03571428571428571, 'mean_word_length': 3.7674772036474162, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4070712', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1110, 'n_words': 379}