//  V1.00
// 
//  EEEE  ZZZZ       SSS  EEEE  RRR   III    A    L
//  E        Z      S     E     R  R   I    A A   L 
//  EEE     Z   --   SS   EEE   RRR    I    AAA   L  
//  E      Z           S  E     R R    I   A   A  L    
//  EEEE  ZZZZ      SSS   EEEE  R  R  III  A   A  LLLL 
//
//  For those who cant afford mscomm...
//  ===============================================================
//  
//  What?	EzSerial.dll
//
//  Why?    VB learning edition users cant use MSComm.
//          Other languages can also use the DLL, but why anyone
//          would need anything other than C and VB is beyond me...          
//
//  How?    Put the DLL in a convenient place (c:\win\sys) is
//          good. declare all the functions in VB and youre on 
//          your way.
//          EZSerial talks directly to the UART, so there is no  
//          opening of ports etc, although use inituart to set baud
//          etc.
//    
//  When?   2/10/03
//
//  Who?	Scott Day   ScottDaySA@hotmail.com
//
//==================================================================
//
//  TERMS OF USE:
//  -------------
//  
//  I will accept no responsibility if this program causes any loss
//  or doesnt work as intended. If you type in the wrong port number
//  and blow something up, that's your problem.
//
//  This program is provided free for personal use only. This does
//  include making money off it, as long as you arent doing so for
//  a company of two or more people.
//
//  If you are part of a company, send me $5 if you like this code and
//  want to use it. Contact me at ScottDaySA@Hotmail.com in this case
//  and no other! I dont want to hear about improvements that I should
//  make or any little bugs you find. I am a student and dont have time.
//
//  If you modify my code, fine, but leave this header on the top and
//  comment your modifications clearly and put your name below. Make 
//  sure you update the VB declaration section.
//
//===================================================================
//




#include "stdafx.h"
#include <conio.h>


//void __stdcall esInitUART(int ePortAddr,int eBaud, char eParity, char eBits, char eStop );
//char __stdcall esTxByte (int ePortAddr, char eTxByte );
//char __stdcall esRxByte (int ePortAddr, char ParityChar);
//bool __stdcall esWaitforTxReady (int ePort);
//bool __stdcall esRxReady (int ePort);

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}





 //esInitUART(0x378,9600,0, 8, 1 );

//	esTxByte ( 0x378, i );


void __stdcall esInitUART(int ePortAddr,int eBaud, char eParity, char eBits, char eStop )
{
	char BaudDivisor;
	char lParity;
	char lBits;
	char lStop;
	char lCmd;
	int ErrCode;

	char Result;
	int Cntr;


	BaudDivisor = 115200 / eBaud;

	lCmd = 0;
	switch (eParity)
	{
		case 'N' :
			lParity = 0;
			break;
		case 'O' :
			lParity = 8;
			break;
		case 'E' :
			lParity = 24;
			break;
		case 'M' :
			lParity = 40;
			break;
		case 'S' :
			lParity = 56;
			break;
	}

	switch (eBits)
	{
		case 5:
			lBits = 0;
			break;
		case 6:
			lBits = 1;
			break;
		case 7:
			lBits = 2;
			break;
		case 8:
			lBits = 3;
			break;
	}


	switch (eStop)
	{
		case 1:
			lStop = 0;
			break;
		case 2:
			lStop = 4;
			break;
	}
	

					// turn on baud set
//	ErrCode = _outp(ePortAddr + 3,128 | lParity | lBits | lStop);
					//send to LCR register
	ErrCode = _outp(888 + 3,128 | 0 | 3 | 0);

	Cntr = 0;
	do
	{
	Result = (_inp(ePortAddr + 5)) & 32 ;	 //check the TX Ready bit
	Cntr = Cntr + 1;
	if (Cntr > 32000) Result = 1;
	} while (Result == 0);


	ErrCode = _outp(ePortAddr + 0,BaudDivisor );



	Cntr = 0;
	do
	{
	Result = (_inp(ePortAddr + 5)) & 32 ;	
	Cntr = Cntr + 1;
	if (Cntr > 32000) Result = 1;
	} while (Result == 0);


	lCmd = lParity | lBits | lStop;

	// turn off baud set


	ErrCode = _outp(ePortAddr + 3,lCmd);
					//send to LCR register


	Cntr = 0;
	do
	{
	Result = (_inp(ePortAddr + 5)) & 32 ;	
	Cntr = Cntr + 1;
	if (Cntr > 32000) Result = 1;
	} while (Result == 0);

	ErrCode = _outp(ePortAddr + 2,1); //turn on fifo
	ErrCode = _outp(ePortAddr + 1,0); 
	ErrCode = _outp(ePortAddr + 4,0); //turn off interrupts

	return;
}



char __stdcall esTxByte (int ePortAddr, char eTxByte )
{
	char ErrCode;
	int Cntr;
	char Result;

	
	
	Cntr = 0;
	
	do
	{
	Result = (_inp(ePortAddr + 5)) & 32 ;	
	//Cntr = Cntr + 1;
	//if (Cntr > 32000) Result = 1;
	} while (Result == 0);
	
	ErrCode = _outp(ePortAddr ,eTxByte);

	return eTxByte;
}


char __stdcall esRxByte (int ePortAddr, char ParityChar)
{

	char RxReady;
	char Result;
	char ErrCode;

	RxReady = (_inp(ePortAddr + 5)) & 1 ;   //check RXReady bit
	
	if (RxReady == 1)
	{
		Result = _inp(ePortAddr + 0);

		ErrCode = _inp(ePortAddr + 5) & 4;  //check for parity error

		if ((ErrCode != 0) & (ParityChar != 0)) Result = ParityChar;

	}
	else
	{
		Result = 0;
	}

	

	return Result;
}

bool __stdcall esWaitforTxReady (int ePort)
{

int Cntr;
char Result;
bool Res;
Cntr = 0;
	do
	{
	Result = (_inp(ePort + 5)) & 32 ;	
	Cntr = Cntr + 1;
	if (Cntr > 32000) Result = 1;
	} while (Result == 0);

//wait till Tx buffer is sent

	if (Result > 1) Res = true; else Res = false;

	return Res;
}



bool __stdcall esRxReady (int ePort)
{

int Cntr;
char Result;
bool Res;
Cntr = 0;

	Result = (_inp(ePort + 5)) & 1 ;	

	if (Result == 1) Res = true; else Res = false;

	return Res;
}






//Paste these declarations into your VB code...
//
//Declare Sub esInitUART Lib "EZSerial.dll" (ByVal iPort As Integer, ByVal iBaud As Integer, ByVal iParity As Byte, ByVal iBits As Byte, ByVal iStop As Byte)
//Declare Function esTxByte Lib "EZSerial.dll" (ByVal iPort As Integer, ByVal iByte As Byte) As Byte
//Declare Function esRxByte Lib "EZSerial.dll" (ByVal iPort As Integer, ByVal ParityChar As Byte) As Byte
//Declare Function esWaitforTxReady Lib "EZSerial.dll" (ByVal iPort As Integer) As Boolean
//Declare Function esRxReady Lib "EZSerial.dll" (ByVal iPort As Integer) As Boolean

