home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2507 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.8 KB  |  111 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from win32file import *
  5. from win32event import *
  6. import win32con
  7. import msvcrt
  8. import threading
  9. import sys
  10.  
  11. def FindModem():
  12.     for i in range(1, 5):
  13.         port = 'COM%d' % (i,)
  14.         
  15.         try:
  16.             handle = CreateFile(port, win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
  17.             if GetCommModemStatus(handle) != 0:
  18.                 return port
  19.         continue
  20.         except error:
  21.             continue
  22.         
  23.  
  24.     
  25.  
  26.  
  27. class SerialTTY:
  28.     
  29.     def __init__(self, port):
  30.         if type(port) == type(0):
  31.             port = 'COM%d' % (port,)
  32.         
  33.         self.handle = CreateFile(port, win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL | win32con.FILE_FLAG_OVERLAPPED, None)
  34.         SetCommMask(self.handle, EV_RXCHAR)
  35.         SetupComm(self.handle, 4096, 4096)
  36.         PurgeComm(self.handle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR)
  37.         timeouts = (0xFFFFFFFFL, 0, 1000, 0, 1000)
  38.         SetCommTimeouts(self.handle, timeouts)
  39.         dcb = GetCommState(self.handle)
  40.         dcb.BaudRate = CBR_115200
  41.         dcb.ByteSize = 8
  42.         dcb.Parity = NOPARITY
  43.         dcb.StopBits = ONESTOPBIT
  44.         SetCommState(self.handle, dcb)
  45.         print 'Connected to %s at %s baud' % (port, dcb.BaudRate)
  46.  
  47.     
  48.     def _UserInputReaderThread(self):
  49.         overlapped = OVERLAPPED()
  50.         overlapped.hEvent = CreateEvent(None, 1, 0, None)
  51.         
  52.         try:
  53.             while None:
  54.                 ch = msvcrt.getch()
  55.                 if ord(ch) == 3:
  56.                     break
  57.                 
  58.                 WaitForSingleObject(overlapped.hEvent, INFINITE)
  59.             SetEvent(self.eventStop)
  60.             return None
  61.  
  62.  
  63.     
  64.     def _ComPortThread(self):
  65.         overlapped = OVERLAPPED()
  66.         overlapped.hEvent = CreateEvent(None, 1, 0, None)
  67.         while None:
  68.             (rc, mask) = WaitCommEvent(self.handle, overlapped)
  69.             if rc == 0:
  70.                 SetEvent(overlapped.hEvent)
  71.             
  72.             rc = WaitForMultipleObjects([
  73.                 overlapped.hEvent,
  74.                 self.eventStop], 0, INFINITE)
  75.             if rc == WAIT_OBJECT_0:
  76.                 (flags, comstat) = ClearCommError(self.handle)
  77.                 (rc, data) = ReadFile(self.handle, comstat.cbInQue, overlapped)
  78.                 WaitForSingleObject(overlapped.hEvent, INFINITE)
  79.                 sys.stdout.write(data)
  80.                 continue
  81.             sys.stdout.close()
  82.             break
  83.             continue
  84.             return None
  85.  
  86.     
  87.     def Run(self):
  88.         self.eventStop = CreateEvent(None, 0, 0, None)
  89.         user_thread = threading.Thread(target = self._UserInputReaderThread)
  90.         user_thread.start()
  91.         com_thread = threading.Thread(target = self._ComPortThread)
  92.         com_thread.start()
  93.         user_thread.join()
  94.         com_thread.join()
  95.  
  96.  
  97. if __name__ == '__main__':
  98.     print 'Serial port terminal demo - press Ctrl+C to exit'
  99.     if len(sys.argv) <= 1:
  100.         port = FindModem()
  101.         if port is None:
  102.             print 'No COM port specified, and no modem could be found'
  103.             print 'Please re-run this script with the name of a COM port (eg COM3)'
  104.             sys.exit(1)
  105.         
  106.     else:
  107.         port = sys.argv[1]
  108.     tty = SerialTTY(port)
  109.     tty.Run()
  110.  
  111.