<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=Python_-_Serial_Comms_Example</id>
	<title>Python - Serial Comms Example - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=Python_-_Serial_Comms_Example"/>
	<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Python_-_Serial_Comms_Example&amp;action=history"/>
	<updated>2026-05-18T11:33:59Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://performiq.com/kb/index.php?title=Python_-_Serial_Comms_Example&amp;diff=4601&amp;oldid=prev</id>
		<title>PeterHarding: /* Script */</title>
		<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Python_-_Serial_Comms_Example&amp;diff=4601&amp;oldid=prev"/>
		<updated>2015-08-01T00:30:26Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Script&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 10:30, 1 August 2015&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l148&quot;&gt;Line 148:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 148:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Python]][[Category:RS232]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Python]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:RS232&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Serial&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>PeterHarding</name></author>
	</entry>
	<entry>
		<id>https://performiq.com/kb/index.php?title=Python_-_Serial_Comms_Example&amp;diff=1484&amp;oldid=prev</id>
		<title>PeterHarding: New page: =Overview=  This script uses the win32file module to implement RS232 communications via a COMM port.  ==Script==  &lt;pre&gt; #!/usr/bin/env pyhthon  &#039;&#039;&#039; This code was inspired by Willy Heineman...</title>
		<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Python_-_Serial_Comms_Example&amp;diff=1484&amp;oldid=prev"/>
		<updated>2008-02-25T04:43:19Z</updated>

		<summary type="html">&lt;p&gt;New page: =Overview=  This script uses the win32file module to implement RS232 communications via a COMM port.  ==Script==  &amp;lt;pre&amp;gt; #!/usr/bin/env pyhthon  &amp;#039;&amp;#039;&amp;#039; This code was inspired by Willy Heineman...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Overview=&lt;br /&gt;
&lt;br /&gt;
This script uses the win32file module to implement RS232 communications via a COMM port.&lt;br /&gt;
&lt;br /&gt;
==Script==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env pyhthon&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
This code was inspired by Willy Heineman&amp;#039;s post to&lt;br /&gt;
comp.lang.python on 7/31/2001 in which he advised&lt;br /&gt;
anyone needing such a module to write their own. I had&lt;br /&gt;
tried several serial packages and either could not get&lt;br /&gt;
them to work or they didn&amp;#039;t have all the features I&lt;br /&gt;
needed. So following Willy&amp;#039;s advice, I wrote something&lt;br /&gt;
tailored to my particular needs. Additional features&lt;br /&gt;
could be added but you will need to consult the&lt;br /&gt;
documentation for the underlying Windows functions&lt;br /&gt;
(CreateFile, WriteFile, ReadFile, etc.) as you will&lt;br /&gt;
not find a great deal of help in the documentation for&lt;br /&gt;
win32file. This can be found in the MSVS Help or&lt;br /&gt;
perhaps the Microsoft web site.&lt;br /&gt;
&lt;br /&gt;
You will also need to install Mark Hammond&amp;#039;s Windows&lt;br /&gt;
Extensions to Python, which you can find at&lt;br /&gt;
www.activestate.com. &lt;br /&gt;
&lt;br /&gt;
Thanks Willy.&lt;br /&gt;
&lt;br /&gt;
Gary Richardson&lt;br /&gt;
&lt;br /&gt;
*Comment inserted by Rob.*&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
import win32file as wf&lt;br /&gt;
import string&lt;br /&gt;
&lt;br /&gt;
class Comm: &lt;br /&gt;
   def __init__(self, port=&amp;#039;COM1&amp;#039;, baudRate=wf.CBR_9600, stopBits=wf.ONESTOPBIT, parity=wf.NOPARITY ):&lt;br /&gt;
      self.handle = wf.CreateFile(port,&lt;br /&gt;
                                 wf.GENERIC_READ |  wf.GENERIC_WRITE,&lt;br /&gt;
                                 0,                                                   # Exclusive access&lt;br /&gt;
                                 None,                                             # No security attributes&lt;br /&gt;
                                  wf.OPEN_EXISTING,&lt;br /&gt;
                                 0,                                                   # Not overlapped&lt;br /&gt;
                                 0)                                                   # Zero required for comm devices&lt;br /&gt;
      wf.SetupComm(self.handle, 1000, 1000)   #Set the size of the input and output buffers&lt;br /&gt;
      wf.PurgeComm(self.handle, wf.PURGE_TXCLEAR | wf.PURGE_RXCLEAR )&lt;br /&gt;
      dcb = wf.GetCommState(self.handle)&lt;br /&gt;
      dcb.BaudRate = baudRate&lt;br /&gt;
      dcb.StopBits = stopBits&lt;br /&gt;
      dcb.Parity = parity&lt;br /&gt;
      dcb.fDtrControl = wf.DTR_CONTROL_DISABLE&lt;br /&gt;
      dcb.ByteSize = 8&lt;br /&gt;
      wf.SetCommState(self.handle, dcb)&lt;br /&gt;
      ct = 0xFFFFFFFF, 0xFFFFFFFF, 1000, 10, 10      # 1 sec ReadFile timeout&lt;br /&gt;
      wf.SetCommTimeouts(self.handle, ct)&lt;br /&gt;
      self.termChar = &amp;#039;\r&amp;#039;      # See readPort&lt;br /&gt;
&lt;br /&gt;
   def close(self):&lt;br /&gt;
      wf.CloseHandle(self.handle)&lt;br /&gt;
&lt;br /&gt;
   def controlDTR(self, state):&lt;br /&gt;
      if state:&lt;br /&gt;
         cmd = wf.SETDTR&lt;br /&gt;
      else:&lt;br /&gt;
         cmd = wf.CLRDTR&lt;br /&gt;
      wf.EscapeCommFunction(self.handle, cmd)&lt;br /&gt;
      &lt;br /&gt;
   def  sendCmd(self, command, maxReplyLength=40):&lt;br /&gt;
      # Send a command and wait for a reply.&lt;br /&gt;
      # The reply string must end with the termination character or be maxReplyLength long or longer.&lt;br /&gt;
      wf.PurgeComm(self.handle, wf.PURGE_TXCLEAR | wf.PURGE_RXCLEAR )&lt;br /&gt;
      ec, nbytes = wf.WriteFile(self.handle, command, None)&lt;br /&gt;
      if nbytes != len(command):&lt;br /&gt;
         print &amp;#039;sendCmd - write error&amp;#039;, nbytes, len(command)&lt;br /&gt;
         return None&lt;br /&gt;
      data = self.readPort(maxReplyLength)&lt;br /&gt;
      return data&lt;br /&gt;
&lt;br /&gt;
   def readPort(self, maxChars):&lt;br /&gt;
      data = &amp;quot;&amp;quot;&lt;br /&gt;
      while 1:&lt;br /&gt;
         rc, chars = wf.ReadFile(self.handle, maxChars, None)&lt;br /&gt;
         data += chars&lt;br /&gt;
         if string.count(chars, self.termChar):&lt;br /&gt;
            break&lt;br /&gt;
         if len(data) &amp;gt;= maxChars:&lt;br /&gt;
            break&lt;br /&gt;
         if len(chars) == 0:&lt;br /&gt;
            return None               # Timeout&lt;br /&gt;
      return data&lt;br /&gt;
         &lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
   import time&lt;br /&gt;
&lt;br /&gt;
   def testDTR():&lt;br /&gt;
      # This code toggles the DTR line 4 times at 3 second intervals.&lt;br /&gt;
      dtr = 1&lt;br /&gt;
      com = Comm()&lt;br /&gt;
      for x in range(4):&lt;br /&gt;
         print &amp;#039;dtr=&amp;#039;, dtr&lt;br /&gt;
         print&lt;br /&gt;
         com.controlDTR(dtr)&lt;br /&gt;
         dtr = dtr ^ 1&lt;br /&gt;
         print&lt;br /&gt;
         time.sleep(3.0)&lt;br /&gt;
      com.close()&lt;br /&gt;
      print &amp;#039;done&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   def loopbackTest():&lt;br /&gt;
      &amp;quot;&amp;quot;&amp;quot; This code performs a loopback test. A long string of characters is transmitted and the received&lt;br /&gt;
          data is compared with what was sent.&lt;br /&gt;
          The receive timeout can be tested by unplugging the loopback connector.&lt;br /&gt;
      &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
      com = Comm()&lt;br /&gt;
      td = []&lt;br /&gt;
      total = 0.0&lt;br /&gt;
      totalChars = 0&lt;br /&gt;
      for x in range(5):&lt;br /&gt;
         cmd = &amp;quot;Now is the time for all good men. The quick brown fox jumped over the lazy dog&amp;#039;s back.&amp;quot;*3 + &amp;quot;\r&amp;quot;&lt;br /&gt;
         totalChars += len(cmd)&lt;br /&gt;
         ts = time.clock()&lt;br /&gt;
         data = com.sendCmd(cmd, maxReplyLength=len(cmd))&lt;br /&gt;
         total += time.clock() - ts&lt;br /&gt;
         notok = -1&lt;br /&gt;
         if data:&lt;br /&gt;
            for k in range(len(cmd)):&lt;br /&gt;
               if cmd[k] != data[k]:&lt;br /&gt;
                  notok = k&lt;br /&gt;
                  break&lt;br /&gt;
            if notok &amp;gt;= 0:&lt;br /&gt;
               print &amp;#039;Error at %d&amp;#039; % (k)&lt;br /&gt;
         else:&lt;br /&gt;
            print &amp;#039;Timeout&amp;#039;&lt;br /&gt;
         td.append(data)&lt;br /&gt;
      com.close()&lt;br /&gt;
      for n in td: print n&lt;br /&gt;
      a, b = total, total/totalChars&lt;br /&gt;
      print &amp;quot;Total elapsed time: %3.1f\nAverage time per character: %7.5f\nCharacters per sec: %5.1f&amp;quot; % (a, b, 1/b)&lt;br /&gt;
&lt;br /&gt;
   print&lt;br /&gt;
   print&lt;br /&gt;
   loopbackTest()                     &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Python]][[Category:RS232]]&lt;/div&gt;</summary>
		<author><name>PeterHarding</name></author>
	</entry>
</feed>