
#ifndef __COM_DEF_
#define __COM_DEF_
#include        <dos.h>
#include        <time.h>

#include        <bios.h>
#include        <stdio.h>
#include        <stdlib.h>

/*============== Error Code ==============*/
#define RxOK            1               /* Receive OK */
#define TxOK            2               /* Transmit OK */
#define TxTimeOut       -10             /* Transmit time out */
#define RxTimeOut       -11             /* Receive time out */
#define RxOverFlow      -12             /* queue over flow */

/*============== Serial port IO address ==============*/
#define Com1Base        0x3f8           /* Base address of COM1  */
#define Com2Base        0x2f8           /*                 COM2  */
#define COM1            0
#define COM2            1

#define TxD             ComBase         /* Tx buffer       DLAB=0 */
#define RxD             ComBase         /* Rx buffer       DLAB=0 */
#define DLL             ComBase         /* baudrate LSB    DLAB=1 */
#define DLH             ComBase+1       /* baudrate MSB    DLAB=1 */
#define IER             ComBase+1       /* Int. enable reg DLAB=0 */
#define IIR             ComBase+2       /* Interrupt Ident. reg   */
#define FCR             ComBase+2       /* FIFO control reg       */
#define LCR             ComBase+3       /* Line control reg       */
#define MCR             ComBase+4       /* Modem control reg      */
#define LSR             ComBase+5       /* Line status reg        */
#define MSR             ComBase+6       /* Modem status reg       */
#define SCR             ComBase+7       /* Scratch reg            */

#define DataMask        0x03            /* xxxx-xx00 */
#define Data8bit        0x03
#define Data7bit        0x02
#define Data6bit        0x01
#define Data5bit        0x00

#define ParityMask      0x18            /* xx00-0xxx */
#define NonParity       0x00
#define OddParity       0x08
#define EvenParity      0x18
#define MarkParity      0x28

#define StopMask        0x04            /* xxxx-x0xx */
#define Stop1bit        0x00
#define Stop2bit        0x04

/*============== Max wait time for Serial port receive ==============*/
#define COM_MAX_RX_WAIT_TIME            10

#define qSize   256
#define qNormal 0
#define qFull   1
#define qEmpty  2


int TxCharCOM(int ComNo, char ch);
//int RxCharCOM(int ComNo, char* ch);
int PollRxChar(int ComNo, char* ch);

//int InitISRofCOM(int ComNO);
int InitCOM(int ComNo, unsigned long int BaudRate, int DataFormat);
//int ResetQueue(int ComNo);
void WaitClock(int count);




#define         CHKSUMenable    1
#define         CHKSUMdisable   0

/*============== Error Code ==============*/
#define         RxChksumErr     -13             /* CHKSUM error */

typedef struct {
  int ComNo;
  long BaudRate;
  int DataFormat;
  int CheckSum;
}ComSetting;

int ResetCOM(int ComNo);
int OpenCOM(int ComNo);
int CloseCOM(int ComNo);
int SendCommand(int ComNo, char* str);
int ReceiveCommand(int ComNo, char *str);
char HEXtoCHAR(int ch);
int CHARtoHEX(char ch);


/* ============================================================================
Connect HP3401A via RS-232
===============================================================================
Wire Connection :                       Configure :
 TX (3) ------ (2)RX                     9600bps, even parity, 7 bit data
 RX (2) ------ (3)TX                     Add \n(0x0A) in end of string
 DTR(4) ------ (6)DSR
 DSR(6) ------ (4)DTR
 GND(5) ------ (5)GND
===============================================================================
HP34401A Command Set:
 :SYST:REM       Set remote control
 :SYST:LOC       Set local control
 *CLS            Clear display
 :CONF:VOLT:DC   Set read DC voltage
 :MEAS:VOLT:DC?  Read DC voltage
 :READ?          Read value
============================================================================ */



#define         HP_OK                   3

void Initit(int ComNo);
void Closeit(int ComNo);
void InitHP(int ComNo);
void CloseHP(int ComNo);
int HPSendCommand(int ComNo, char *str);
int HPReceiveCommand(int ComNo, char *str);

#endif

