Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created April 15, 2012 00:48
Show Gist options
  • Save kurtbrose/2389072 to your computer and use it in GitHub Desktop.
Save kurtbrose/2389072 to your computer and use it in GitHub Desktop.
open LPT1, read from it, save changes to a file
import ctypes
import time
import datetime
#a bunch of win32 constants; unfortunately must be hardcoded here
GENERIC_READ = 0x80000000
SHARE_NONE = 0
OPEN_EXISTING = 3
FILE_ATTRIBUTE_NONE = 0
handle = ctypes.windll.kernel32.CreateFileA("\\.\LPT1", GENERIC_READ,
SHARE_NONE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NONE, None)
if handle == -1:
print "ERROR: unable to open LPT1 port"
print ctypes.FormatError() #win32 library description of problem
def read(handle):
buffer = ctypes.create_string_buffer(32)
amount_read = ctypes.create_string_buffer(2)
ctypes.windll.kernel32.ReadFile(handle, buffer, 32, amount_read, None)
return [bin(ord(a)) for s in buffer[:ord(amount_read[0])]]
lastval = None
val = None
file = open("parallel_values.txt", "a")
while True: #terminate program with Ctrl-C
lastval = val
val = read(handle)
if val != lastval:
file.write(str(val)+" "+str(datetime.datetime.now())+"\n")
time.sleep(0.01) #sleep for 10ms to free CPU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment