#include
#include
#include
#include
#include
#define TRUE 1
#define FALSE 0
#define COM 0x3f8 /* COM1=0x3f8, COM2=0x2f8 */
#define RBR 0 /* Receive Buffer Register */
#define THR 0 /* Transmit Holding Reg. */
#define DLL 0 /* Divisor Latch LSB */
#define DLM 1 /* Divisor Latch MSB */
#define IER 1 /* Interrupt Enable Reg. */
#define IIR 2 /* Interrupt ID Register */
#define FCR 2
#define AFR 2
#define LCR 3 /* Line Control Register */
#define MCR 4 /* Modem Control Register */
#define LSR 5 /* Line Status Register */
#define MSR 6 /* Modem Status Register */
#define SCR 7 /* The Scratch Register */
#define I8259M 0x21
#define I8259 0x20 /* The address of the 8259 */
#define EOI 0x20 /* The end of int command */
#define IRQ 4
#define SIZE 512 /* Buffer size */
char buffer[SIZE]; /* Receiving buffer */
char *i_get, *i_put; /* Pointer to buffer */
unsigned int baud; /* Baud Rate */
int Size_Temp = 2;
/* Foward declaration of functions used in this program */
void main_menu(void);
void interrupt com_handler(void);
void trans(int c);
int readchr (char *c);
void test_TX(void);
void test_RX(void);
unsigned short CRC16(unsigned char *message,unsigned short data_length);
int wait_chr(char *wc);
void manual_RTU(void);
void rand_NOISE(void);
void manual(void);
void baud_rate(void);
void INPUT(unsigned short int *temp);
void LoopBack_Test(void);
int looptest1(void);
int looptest2(void);
int loootest3(void);
void about(void);
void main (void)
{
int line,mask; /* Comm parameters */
void interrupt(*old_handler)();
old_handler=getvect(IRQ+8); /* Old ISR adress */
i_put=i_get=buffer; /* Pointer initialisation */
memset(buffer,0,SIZE); /* Buffer intialisation */
baud=0x0c; /* 0x01 = 115200 bps
0x02 = 57600 bps
0x03 = 38400 bps
0x06 = 19200 bps
0x0c = 9600 bps */
line=0x03; /* 8 bits, non parity, 1 stop */
/* initialisation */
outportb(COM+IER,0);
inportb(COM+LSR);
inportb(COM+MSR);
inportb(COM+IIR);
inportb(COM+RBR);
/* set speed */
outportb(COM+LCR, inportb(COM+LCR) | 0x80);
outportb(COM+DLM,baud>>8);
outportb(COM+DLL,baud&0xff);
outportb(COM+LCR,inportb(COM+LCR) & 0x7f);
/* set data format */
outportb(COM+LCR,line);
outportb(COM+MCR,0x0b);
outportb(COM+IER,0x01);
/* set ISR address */
disable();
setvect (IRQ+8,com_handler);
enable();
mask=inportb(I8259M);
mask &=~(1<>1) ^0xA001;
else CRC>>=1;
}
}
temp = CRC & 0xFF; // Temp storage for swap
CRC = ((CRC>>8)&0xFF) + (temp<<8); // Swap high amd low byte
return (CRC); // Return calulated 16-bit CRC
}
int wait_chr(char *wc)
{
Delay: Baud = 9600, 8-bits & Start & stop = 10-bits
The entire message frame must be transmitted
as a continuous stream. IF silent for more
than 1.5 character time, the receving device
flushes incompete message.
1.5 X 0.96 = 1.44 ms about 1.5ms
1.5ms (Max Gap between Characters) */
char timeout;
delay(2); // Delay 1.5ms - MAX time between character's
if(readchr(wc)!=0)
{
sound(70); // Beep on
delay(100); // Delay 100ms
nosound(); // Beep off
printf("\n\n!!!!!!!!!! Error Timeout !!!!!!!!!!");
timeout = TRUE;
}
else timeout=FALSE;
return timeout;
}
void manual_RTU(void)
{
unsigned char RTU_frame[SIZE], // RTU Frame
CRC_HI, // High byte of CRC
CRC_LO; // Low byte of CRC
unsigned short int count=0, // Used for loop control
frame_length=0, // The total length of RTU frame
temp, // 16-bit temp
CRC; // 16-bit CRC
clrscr();
printf("\n\nManual RTU frame with added CRC");
printf("\n---------------------------------");
printf("\nEnter RTU_frame in byte's enter 'DF' when finished");
count=0;
printf("\nRTU_frame[%d] (e.g 01) = ",count);
while (temp != 0xDF)
{
Size_Temp = 2;
INPUT(&temp);
RTU_frame[count] = temp;
count++;
printf("RTU_frame[%d] (e.g 01) = ",count);
}
count--;
/* Calulate CRC for test message */
CRC = CRC16(RTU_frame,count);
printf("\nCalulated CRC (Hex): %X",CRC);
/* Split 16-bit CRC into high and low bytes */
CRC_LO = CRC;
CRC_HI = CRC >> 8;
/* Add CRC to end of RTU */
RTU_frame[count++] = CRC_HI;
RTU_frame[count] = CRC_LO;
frame_length = count;
count=0;
printf("\nTotal Frame Length (Dec): %d",frame_length+1);
/* Construncting of RTU frame is now complete */
/* Print & send RTU frame through RS232 COM 1 */
printf("\n\nSending RTU Frame (HEX): -\n");
while (count <= frame_length)
{
printf("%X ",RTU_frame[count]);
trans(RTU_frame[count++]);
}
printf("\n\nFinished Sending RTU Frame");
i_get=i_put;
test_RX(); // Wait for Responce
}
void rand_NOISE(void)
{
unsigned short int temp; // 16-bit temp
clrscr();
printf("\n\nSimulate Random Noise");
printf("\n-----------------------");
printf("\nNo delay between bytes -- Hit 1");
printf("\nRandom delay (0 to 100ms) betweem bytes -- hit 2");
printf("\nUser sets delay in milliseconds -- hit 3");
printf("\nCancel - Return to Main Menu -- hit 4");
switch (getch())
{
case '1': printf("\nSending Random noise - delay = 0 (anykey to stop)");
while(!kbhit()) trans(rand() % 0xff);
break;
case '2': printf("\nSending Random noise - delay = RAND (anykey to stop)");
while(!kbhit())
{
delay(rand() % 100);
trans(rand() % 0xff);
}
break;
case '3': printf("\nDelay (ms) ? ");
scanf("%d",&temp);
printf("Sending Random noise - delay = %dms (anykey to stop)",temp);
while(!kbhit())
{
delay(temp);
trans(rand() % 0xff);
}
break;
}
i_get = i_put; /* Clear Buffer */
}
void manual(void)
{
unsigned char RTU_frame[SIZE], // RTU Frame
CRC_HI, // High byte of CRC
CRC_LO; // Low byte of CRC
unsigned short int count=0, // Used for loop control
frame_length=0, // The total length of RTU frame
temp; // 16-bit temp
clrscr();
printf("\n\nManual RTU frame no CRC");
printf("\n-------------------------");
printf("\nEnter RTU_frame in byte's enter 'DF' when finished");
count=0;
printf("\nRTU_frame[%d] (e.g 01) = ",count);
while (temp != 0xDF)
{
Size_Temp = 2;
INPUT(&temp);
RTU_frame[count] = temp;
count++;
printf("RTU_frame[%d] (e.g 01) = ",count);
}
count= count-2;
frame_length = count;
count=0;
printf("\nTotal Frame Length (Dec): %d",frame_length+1);
/* Construncting of RTU frame is now complete */
/* Print & send RTU frame through RS232 COM 1 */
printf("\n\nSending RTU Frame (HEX): -\n");
while (count <= frame_length)
{
printf("%X ",RTU_frame[count]);
trans(RTU_frame[count++]);
}
printf("\n\nFinished Sending RTU Frame");
i_get=i_put;
test_RX(); // Wait for Responce
}
void baud_rate(void)
{
int rate; /* Baud Rate */
/* 0x01 = 115200 bps
0x02 = 57600 bps
0x03 = 38400 bps
0x06 = 19200 bps
0x08 = 14400 bps
0x0c = 9600 bps */
clrscr();
rate = (115200 / baud);
if(baud != 1)
printf(" Baud Rate (Current = %.6u bps)",rate); else
printf(" Baud Rate (Current = 115200 bps)"); printf("\n------------------------------------");
printf("\n 115200 bps -- bit 1");
printf("\n 57600 bps -- hit 2");
printf("\n 38400 bps -- hit 3");
printf("\n 19200 bps -- hit 4");
printf("\n 14400 bps -- hit 5");
printf("\n 9600 bps -- hit 6");
printf("\n 4800 bps -- hit 7");
printf("\n 2400 bps -- hit 8");
printf("\n 1200 bps -- hit 9");
printf("\n No Change -- hit 0");
switch(getch())
{
case '1':
baud = 0x01;
break;
case '2':
baud = 0x02;
break;
case '3':
baud = 0x03;
break;
case '4':
baud = 0x06;
break;
case '5':
baud = 0x08;
break;
case '6':
baud = 0x0c;
break;
case '7':
baud = 0x18;
break;
case '8':
baud = 0x30;
break;
case '9':
baud = 0x60;
break;
default:
clrscr();
return;
}
/* set speed */
outportb(COM+LCR, inportb(COM+LCR) | 0x80);
outportb(COM+DLM,baud>>8);
outportb(COM+DLL,baud&0xff);
outportb(COM+LCR,inportb(COM+LCR) & 0x7f);
rate = 115200 / baud;
if(baud != 1) printf("\n\nBaud Rate Changed to %u bps",rate);
else printf("\n\nBaud Rate Changed to 115200 bps",rate);
clrscr();
}
void INPUT(unsigned short int *temp)
{
int status;
if (Size_Temp == 4)
{
while ((status = scanf("%4X",temp)) != EOF)
if (status !=1 || ! isspace(getchar()))
{
sound(70); // Beep on
delay(100); // Delay 100ms
nosound(); // Beep off
printf("Error, try again (4 HEX digits eg 1234) : ");
fflush(stdin);
}
else break;
}
else
{
while ((status = scanf("%2X",temp)) != EOF)
if (status !=1 || ! isspace(getchar()))
{
sound(70); // Beep on
delay(100); // Delay 100ms
nosound(); // Beep off
printf("Error, try again (2 HEX digits eg 01) : ");
fflush(stdin);
}
else break;
}
}
void LoopBack_Test(void)
{
struct time t;
int res1=0, res2=0, res3=0;
clrscr();
gettime(&t);
printf("\n\n\n Test Started: %2d:%02d:%02d",t.ti_hour,t.ti_min,t.ti_sec);
printf("\n -----------------------------------");
printf("\n Testing .... 00%%");
res1 = looptest1();
res2 = looptest2();
res3 = looptest3();
if (res1 == 1 || res2 == 1 || res3 == 1)
printf(" - FAIL");
else
printf(" - PASS");
printf("\n * Test 1");
if (res1 == 1)
printf(" - FAIL");
else
printf(" - PASS");
printf("\n * Test 2");
if (res2 == 1)
printf(" - FAIL");
else
printf(" - PASS");
printf("\n * Test 3");
if (res3 == 1)
printf(" - FAIL");
else
printf(" - PASS");
gettime(&t);
printf("\n -----------------------------------");
printf("\n Test Finished: %2d:%02d:%02d",t.ti_hour,t.ti_min,t.ti_sec);
printf("\n\n\n\nHit any [Key] to return to main menu");
getch();
}
int looptest1(void)
{
unsigned int data = 0,
status = 0,
i = 0;
unsigned short CRC,
CRC_RX,
timer;
unsigned char TX[800]; /* Transmit temp storage */
unsigned char RX[800]; /* Receive temp storage */
unsigned char er = FALSE,
ch;
/* Test data 0 - 255 - 0 */
while (511 > i)
{
TX[i++] = data;
if (data < 255 && status == 0) data++;
if (data > 0 && status == 1) data--;
if (data == 255) status = 1;
if (data == 0) status = 0;
}
CRC = CRC16(TX, 512);
TX[512] = CRC >>8;
TX[513] = CRC;
i = 0;
/* Clear Buffer - before transmiting */
i_get = i_put;
/* TX */
while (513 >= i)
{
trans(TX[i++]);
}
printf("%c%c%c17%%",0x08,0x08,0x08);
/* RX */
timer = 0;
i=0;
while (1)
{
/* Timeout after 0.25 second */
if (timer >= 250)
{
er = TRUE;
break;
}
if ((readchr (&ch)) != 0)
{
delay(1); /* 1 ms delay */
timer++;
continue;
}
timer = 0;
RX[i++] = ch;
if (i >= 514) break;
}
/* Compare RX with TX */
i = 0;
while (i <= 513)
{
if (TX[i] != RX[i]) er = TRUE;
i++;
}
/* Check CRC */
CRC = CRC16(RX, 512);
CRC_RX = (RX[512] <<8 | RX[513]);
if (CRC != CRC_RX) er = TRUE;
printf("%c%c%c33%%",0x08,0x08,0x08);
if (er == TRUE) return 1;
else return 0;
}
int looptest2(void)
{
unsigned int i = 0;
unsigned short CRC,
CRC_RX,
timer;
unsigned char TX[800]; /* Transmit temp storage */
unsigned char RX[800]; /* Receive temp storage */
unsigned char er = FALSE,
ch;
/* Test data - Random Numbers 0 to 255 */
while (511 > i) TX[i++] = rand() % 255;
CRC = CRC16(TX, 512);
TX[512] = CRC >>8;
TX[513] = CRC;
i = 0;
/* Clear Buffer - before transmiting */
i_get = i_put;
/* TX */
while (513 >= i)
{
trans(TX[i++]);
}
printf("%c%c%c50%%",0x08,0x08,0x08);
/* RX */
timer = 0;
i=0;
while (1)
{
/* Timeout after .25 seconds */
if (timer >= 250)
{
er = TRUE;
break;
}
if ((readchr (&ch)) != 0)
{
delay(1); /* 1 ms delay */
timer++;
continue;
}
timer = 0;
RX[i++] = ch;
if (i >= 514) break;
}
/* Compare RX with TX */
i = 0;
while (i <= 513)
{
if (TX[i] != RX[i]) er = TRUE;
i++;
}
/* Check CRC */
CRC = CRC16(RX, 512);
CRC_RX = (RX[512] <<8 | RX[513]);
if (CRC != CRC_RX) er = TRUE;
printf("%c%c%c67%%",0x08,0x08,0x08);
if (er == TRUE) return 1;
else return 0;
}
int looptest3(void)
{
unsigned int status = 0,
i = 0;
unsigned short CRC,
CRC_RX,
timer;
unsigned char TX[800]; /* Transmit temp storage */
unsigned char RX[800]; /* Receive temp storage */ unsigned char er = FALSE,
ch;
/* Test data - Random Numbers 0 or 0xFF */
while (511 > i)
{
status = rand() % 2;
switch (status)
{
case 0:
TX[i++] = 0;
break;
case 1:
TX[i++] = 0xFF;
break;
}
}
CRC = CRC16(TX, 512);
TX[512] = CRC >>8;
TX[513] = CRC;
i = 0;
/* Clear Buffer - before transmiting */
i_get = i_put;
/* TX */
while (513 >= i)
{
trans(TX[i++]);
}
printf("%c%c%c75%%",0x08,0x08,0x08);
/* RX */
timer = 0;
i=0;
while (1)
{
/* Timeout after .25 seconds */
if (timer >= 250)
{
er = TRUE;
break;
}
if ((readchr (&ch)) != 0)
{
delay(1); /* 1 ms delay */
timer++;
continue;
}
timer = 0;
RX[i++] = ch;
if (i >= 514) break;
}
/* Compare RX with TX */
i = 0;
while (i <= 513)
{
if (TX[i] != RX[i]) er = TRUE;
i++;
}
/* Check CRC */
CRC = CRC16(RX, 512);
CRC_RX = (RX[512] <<8 | RX[513]);
if (CRC != CRC_RX) er = TRUE;
printf("%c%c%c100%%",0x08,0x08,0x08);
if (er == TRUE) return 1;
else return 0;
}
void about(void)
{
clrscr();
printf("\n\n-----------------------------------------");
printf("\n| Final Year Project EEE516J4 |");
printf("\n| Unversity of Ulster |");
printf("\n| BEng (hons) Electronic Systems |");
printf("\n| |");
printf("\n| Date: 22/02/2002 |");
printf("\n| Revision: 1.05 |");
printf("\n| By: Colin K McCord |");
printf("\n-----------------------------------------");
printf("\n\n\n\nHit any [Key] to return to main menu");
getch();
}