// Spludlow Software
// Copyright © Samuel P. Ludlow 2020 All Rights Reserved
// Distributed under the terms of the GNU General Public License version 3
// Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
// https://www.spludlow.co.uk/LICENCE.TXT
// The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission
// v1.14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Spludlow.Io
{
///
/// Wrapper for "InpOut32 and InpOutx64" so you can access the Parallel Port
///
/// http://www.highrez.co.uk/Downloads/InpOut32/default.htm
/// InpOutBinaries_1501.zip
/// Make sure you Run Win32\InstallDriver.exe as admininstrator (even on x64)
/// "inpoutx64.dll" needs to be in the bin directory (set to copy to output directory) it is not a .net DLL so you can't reference it normally
/// Look up the Parallel Port (LPT) address in device manager. My PCI card is 0xD050
///
/// Not tested Inp32() yet. Not sure about char ? Just cast to byte?
///
public class ParallelPort
{
[DllImport("inpoutx64.dll", EntryPoint = "IsInpOutDriverOpen")]
private static extern UInt32 IsInpOutDriverOpen();
[DllImport("inpoutx64.dll", EntryPoint = "Out32")]
private static extern void Out32(int PortAddress, byte Data);
[DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
private static extern char Inp32(int PortAddress);
private int[] _GrayCodes = { 0x00, 0x01, 0x03, 0x02, 0x06, 0x07, 0x05, 0x04 };
///
/// Write 12 bits (8 data & 4 control)
///
public static void Write(int address, int data)
{
int dataPart = data & 0x0FF;
int controlPart = (data & 0xF00) >> 8;
controlPart ^= 0xFB;
Out32(address, (byte)dataPart);
Out32(address + 2, (byte)controlPart);
}
///
/// Write 12 bits as 3 nibbles
/// Makes sense when you have a 3 X 7 Segment Hex displays hooked up
///
public static void Write(int address, int nib0, int nib1, int nib2)
{
int data = (nib0 & 0xF) | ((nib1 & 0xF) << 4) | ((nib2 & 0xF) << 8);
Write(address, data);
}
///
/// Write 8 bits to the data lines
///
public static void WriteData(int address, int data)
{
int dataPart = data & 0x0FF;
Out32(address, (byte)dataPart);
}
///
/// Write 4 bits to the control lines
/// bits are inverted except "nInitialize" so the 4 bits behave just like the data bits
///
public static void WriteControl(int address, int data)
{
int controlPart = data;
controlPart ^= 0xFB;
Out32(address + 2, (byte)controlPart);
}
///
/// Simple test Method
///
/// Add DLL to "Applications.txt" with:
/// Spludlow.Io.ParallelPort C:\Program Files\SpludlowV1\Spludlow.Io.ParallelPort Lib
///
/// configure "Spludlow-Service.txt" with:
/// Method ShowTime Spludlow.Io.ParallelPort Spludlow.Io.ParallelPort ShowTime D050 500
///
public static void ShowTime(string hexAddress, int miliseconds)
{
AskStop = false;
int address = Int32.Parse(hexAddress, System.Globalization.NumberStyles.AllowHexSpecifier);
int[] nibs = new int[3];
while (AskStop == false)
{
string message = DateTime.Now.ToString("0000yyyyMMdd0000HHmmss");
for (int index = 0; index < message.Length; ++index)
{
for (int n = 0; n < 3; ++n)
{
int offset = index + n;
if (offset >= message.Length)
offset = n;
nibs[n] = Int32.Parse(message.Substring(offset, 1));
}
Spludlow.Io.ParallelPort.Write(address, nibs[0], nibs[1], nibs[2]);
System.Threading.Thread.Sleep(miliseconds);
}
}
}
private static bool AskStop;
public static void Stop()
{
AskStop = true;
}
}
}