An Unusual Use For The Python Programming Language.




Hi. there again folks,  it's your resident nutter... :)

Well I thought for a change I would write about something different.

So I thought how about my NEW project involving the Python Programming Language and using it to develope a Bench Multimeter for the AMIGA.

Huh!?!,  you might say, but,  yep,  Python does NOT have to be centred around the Internet and the like but can be used to access the AMIGA parallel port for reading.

Hmm,  how?,  you might say;  well in the days of the birth of probably the greatest computer of all time the software/hardware engineers created a device called PAR:  which could write to or read from the parallel port without serious knowledge of the port at all.  To use the parallel.device though required some knowledge of the device offsets for software control.  However I discovered a back door to PAR:  WITHOUT ANY knowledge of the port AT ALL.

This requires a small EXTERNAL modification for the system to work which can be searched for on AMINET as   PAR_READ.lha   and does NOT require any AMIGA internal alterations at all.

There are two levels of difficulty in this archive from  ~childishly simple~,  10 years old,  to  ~childishly complex~,  14 years old.  Both are ludicrously simple and one requires ONLY a single wire link to test with.

I'm not going to describe any hardware here just the Python approach to my idea.  Download PAR_READ.lha  for hardware description.

Well I found an archive of Python Version 1.4 for a stock A1200 and decided to install it to this old tub of an A1200.

The minimum requirements for Python 1.4 is a standard A1200 and that clinched it for me to learn Python for a change.  Although NOT up to current specifications is more than adequate for a complete novice.

Well it took me about 2 hours to get to grips with the syntax and as many hours again to grasp what it could do.

OK, down to the nitty gritty... :)

I wanted to learn Python and the easiest way for me was to jump in at the deep end try to access the AMIGA parallel port for most of the projects on AMINET that require the ADC,  (Analogue to Digital Converter).

This was not as easy as the code below shows as I have always had access to all of the hardware registers and or device offsets.  Accessing hardware using Assembler,  C(++),  BASIC or ARexx is so easy but Python is not,  especially as I was using Version 1.4.

Now PAR: is being used as a VOLUME in READ mode NOT as a DEVICE.  A simple analogy is,  assume the floppy disk drive as DF0:,  this is the DEVICE and is controlled by trackdisk.device.  Now insert a floppy disk with a VOLUME name DF0 then this is the VOLUME.  it can ONLY be written to OR read from just like PAR: above.  You don't need to know anything AT ALL about the VOLUME to read from or write to it.  This is just how PAR:  is being used.

Now down to the code.  The nub of the code are the few lines below.  I've numbered the lines for easy reference and should NOT appear in any real code:-
1)     pointer = open('PAR:', 'rb', 1)
2)     mybyte = str(pointer.read(1))
3)     pointer.close()


This is not a complete coded section just the part that accesses the parallel port.

Below is the COMPLETE test code and WILL run in Python:-
# Start of parallel port access.
# Use Ctrl-C to stop.
def main():
	while 1:
		pointer = open('PAR:', 'rb', 1)
		mybyte = str(pointer.read(1))
		pointer.close()
		print 'Decimal value at the port is:-',ord(mybyte),'.    '
main()
# End of basic running code.


Back to the numbered three lines:-

After the program has started running...

Line 1):-  Opens up a channel,  ~pointer~,  to PAR: to accept 8 bit binary data into it,  because the parallel port is only 1 byte in size then the number 1 fixes the read to 1 byte.
Line 2):-  This value once grabbed is allocated to the variable  ~mybyte~  and is again fixed to 1 byte in size.
Line 3):-  Once grabbed and stored,  IMMEDIATELY close the channel and give back the parallel port to the system as a whole.

~mybyte~  can now be manipulated by any means available under Python.

Looking at the code proper;  is this simple or is this simple!!! :)

Now that I have established that the parallel port is easily accessible  I will now turn to the Demo code for the bench multimeter.  Note that this is UNFINISHED as I haven't fully decided on the multimeter ranges as yet.

This is working code for 1-2-5 ratio,  DC Voltage ranges only.

THE HARDWARE FROM PAR_READ.lha IS REQUIRED FOR THIS TO WORK!!!!

All lines starting with ~#~ are purely comments and help in understanding where I am coming from... :)

To try out this Python script just cut and save as Demo.py and place into the PYTHON:Lib drawer. PYTHON: is assumed to be the VOLUME name where the Python program is located.  Use ASSIGN to point to it.
IMPORTANT NOTE, there are errors and I can't write it down here as IBrowse 2.3 generates these errors!!!
Internet Explorer 6+ displays it OK however...

These are the corrections in words:-
1) ~if mybyte [is less than or equal to] 254:~
(and two cases of)
2) ~while n [is less than or equal to] analogue:~
I hope this is easily understood... :)

Once Python is running from the command prompt ~>>>~ type as follows:-
>>> execfile('PYTHON:Lib/Demo.py')[RETURN]


And off we go... :)

The software detects whether and ADC is connected or not and runs either the ~DEMO MODE.~ or the ~REAL MODE.~ routines.  The -ACK line hardware MUST be connected or the system will hang as though it has locked up,  SO BEWARE!!!

# An analogue and digital multimeter using standard Python, Version 1.4...
# This WILL run on a stock A1200, NO FastRAM is needed, but it helps. :)
# This is (C)2006, B.Walker, G0LCU. PAR_READ.lha from AMINET IS required.
# BEST VIEWED IN 640x200 using standard TOPAZ 8 FONTS.
# Press Ctrl-C to stop the program.

# Import the necessary components for either REAL or DEMO mode.
import whrandom
import time

# Set up a working display that looks OK.
print '\f'
print '          Analogue and digital multimeter readout for TestGear series.'
print
print '                                   +--------+'
print '                                   | 0.00 V |'
print '                                   +--------+'
print
print '  1 Volt.     0   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0'
print '  2 Volts.    0   0.2  0.4  0.6  0.8  1.0  1.2  1.4  1.6  1.8  2.0'
print '  Default.    0   0.5  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0'
print '             *++++++++++++++++++++++++++++++++++++++++++++++++++++*'
print '            (*|                                                   *)'
print '             *+----+----+----+----+----+----+----+----+----+----+-*'

# This is the main working program. It is AMIGA compliant ONLY.
# Wake up the parallel port for use under this program.
pointer = open('PAR:', 'rb', 1)
mychar = str(pointer.read(1))
# Put back to sleep again ready for general use.
pointer.close()

# This is the main coded segment.
def main():
	# Check for any A-D Converter connected to the Parallel Port.
	# This uses PAR: as a VOLUME in READ mode only.
	# PAR_READ.lha IS required for this to work.
	# Open up a channel for PAR: to be read.
	pointer = open('PAR:', 'rb', 1)
	# Grab an 8 bit data byte.
	mychar = str(pointer.read(1))
	# Once grabbed IMMEDIATELY close the channel.
	pointer.close()
	# Convert the data byte to a decimal value.
	mybyte = (ord(mychar))
	# If mybyte equals 255 then A-D Converter NOT connected so run in DEMO mode.
	# If mybyte less than 255 then A-D Converter exists so run in REAL mode.
	if mybyte == 255:
		# DEMO MODE!!!
		print '\n','                                   DEMO MODE.','\v\v'
		while 1:
			# This is the DEMO routine.
			# Generate a random number from 0 to 255.
			mybyte = int(whrandom.random() * 256)
			time.sleep(1)
			# Convert to a digital number look.
			digital = mybyte * 0.02
			# Print the value in the box.
			print '\v\v\v\v\v\v\v\v\v','                                   |',digital,'V '
			# Now convert to analogue.
			analogue = (mybyte/5)
			# Clear the meter reading.
			print '\n\n\n\n\n\n','            (*|                                                   *)'
			print '\v','            (*',
			# Plot the meter movement to 0.1V accuracy.
			n = 0
			# NOTE IBROWSE2.3 ERROR HERE!!!
			while n <= analogue:
				print '\b|',
				n = n + 1
			# Clean up for the next ~GRAB~.
			print
			print
	# NOTE IBROWSE2.3 ERROR HERE!!!
	if mybyte <= 254:
		# REAL MODE!!!
		print '\n','                                   REAL MODE.','\v\v'
		while 1:
			# This is the Parallel Port access routine.
			# Open up a channel for PAR: to be read.
			pointer = open('PAR:', 'rb', 1)
			# Grab an 8 bit data byte.
			mychar = str(pointer.read(1))
			# Once grabbed IMMEDIATELY close the channel.
			pointer.close()
			# Convert the data byte to a decimal value.
			mybyte = (ord(mychar))
			time.sleep(0)
			# Convert to a digital number look.
			digital = mybyte * 0.02
			# Print the value in the box.
			print '\v\v\v\v\v\v\v\v\v','                                   |',digital,'V '
			# Now convert to analogue.
			analogue = (mybyte/5)
			# Clear the meter reading.
			print '\n\n\n\n\n\n','            (*|                                                   *)'
			print '\v','            (*',
			# Plot the meter movement to 0.1V accuracy.
			n = 0
			# NOTE IBROWSE2.3 ERROR HERE!!!
			while n <= analogue:
				print '\b|',
				n = n + 1
			# Clean up for the next ~GRAB~.
			print
			print
main()
# Program End.


Thats all there is to it,  when I have done the final coding you will only be able to get it from here or from my URL:-


http://homepages.tesco.net/wisecracker/G0LCU.HTM



ANY and ALL comments to be sent to:-

wisecracker@tesco.net

AND they WILL be re-published on here, good or bad...(Any extra ideas ALWAYS welcome.)



Bazza...




© RIYAN Productions