www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/03/22:12:36

Date: Thu, 3 Nov 1994 17:51:30 GMT
From: gilliard AT lectra DOT fr (Laurent Pierre Gilliard)
To: djgpp AT sun DOT soe DOT clarkson DOT edu

I want to read a serial port using serial interruption.
I've a program in BorlandC which is ok.
I've tried to change it for GCC but.... Searching help! Thanks.

#define GCC                     /* else TURBOC  */
#define COM     1               /* or 2                         */

#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <graphics.h>
#include <string.h>
#include <process.h>
#include <sys\types.h>
#ifdef GCC
	#include <dpmi.h>
	#include <pc.h>
#endif

#define FAUX    0
#define VRAI    1

#define ICR 0x20                /* Int Control reg       */
#define EOI 0x20                /* End of Int            */
#define IMR 0x21                /* Int Mask reg  */

#if COM==1
	#define IER 0x3F9       /* Int Enable     reg */
	#define IIR 0x3FA       /* Int Identif   reg */
	#define LSR 0x3FD       /* Line Status   reg */
	#define TDR 0x3F8       /* Transmit Data reg */
	#define RDR 0x3F8       /* Receive  Data reg */
	#define DLR 0x3FB       /* Divisor Latch reg */
	#define DRL 0x3F8       /* DLR Low               */
	#define DRH 0x3F9       /* DLR High              */
	#define MCR 0x3FC       /* Modem Control reg */
	#define LCR 0x3FB       /* Line Control  reg */
#else
	#define IER 0x2F9       /* Int Enable     reg */
	#define IIR 0x2FA       /* Int Identif   reg */
	#define LSR 0x2FD       /* Line Status   reg */
	#define TDR 0x2F8       /* Transmit Data reg */
	#define RDR 0x2F8       /* Receive  Data reg */
	#define DLR 0x2FB       /* Divisor Latch reg */
	#define DRL 0x2F8       /* DLR Low               */
	#define DRH 0x2F9       /* DLR High              */
	#define MCR 0x2FC       /* Modem Control reg */
	#define LCR 0x2FB       /* Line Control  reg */
#endif

	void                    InitLine(void);
#ifdef GCC
	void                    RSInt();
#else
	void interrupt          RSInt();
#endif
	void                            RestoreLine();

#ifdef GCC
	_go32_dpmi_seginfo      OldRS, new_handler;
#else
	void interrupt (*OldRS)();
#endif

struct {                                /* Save lines status */
	char imr,
		lcr,
		drl,
		drh,
		mcr,
		lsr;
} etat_ligne;

volatile char Caractere;

void InitLine(void) {
	char c;
	
	disable();
	etat_ligne.imr = inportb(IMR);
	etat_ligne.lcr = inportb(LCR);
	etat_ligne.drl = inportb(DRL);
	etat_ligne.drh = inportb(DRH);
	etat_ligne.mcr = inportb(MCR);
	etat_ligne.lsr = inportb(LSR);
	outportb(LCR,0x80);                     /* Init de la vitesse   */
	outportb(DRL,0x0C);                     /* 9600                                 */
	outportb(DRH,0);
	outportb(LCR,3);                        /* 8 bits, 1 de stop, pas de parite */
	outportb(MCR,0x08);

	outportb(IER,1);                        /* Generer int des que car present  */
	c = inportb(IMR);
	c &= COM==1 ? 0xEF : 0xF7;
	outportb(IMR,c);

#ifdef GCC
	_go32_dpmi_get_protected_mode_interrupt_vector(COM==1 ? 0x0C : 0x0B, &OldRS);

	new_handler.pm_offset = (int)RSInt;
	new_handler.pm_selector = _go32_my_cs();
	_go32_dpmi_chain_protected_mode_interrupt_vector(COM==1 ? 0x0C : 0x0B, &new_handler);
#else
	OldRS = getvect(COM==1 ? 0x0C : 0x0B);
	setvect(COM==1 ? 0x0C : 0x0B, RSInt);
#endif

	enable();
}

#ifdef GCC
	void RSInt() {
#else
	void interrupt RSInt() {
#endif
	unsigned etat;

	etat = inportb(LSR);            /* Line state */
	if (etat&1) {
		Caractere = inportb(RDR);
	}
	outportb(ICR,EOI);
}

void RestoreLine(void) {
	while (!(inportb(LSR)&0x20));   /* Wait end of transmission     */
	disable();
	outportb(IMR,etat_ligne.imr);
	outportb(LCR,etat_ligne.lcr);
	outportb(DRL,etat_ligne.drl);
	outportb(DRH,etat_ligne.drh);
	outportb(MCR,etat_ligne.mcr);
	outportb(LSR,etat_ligne.lsr);

	outportb(IER,0);                        /* End of interruptions */
#ifdef GCC
	_go32_dpmi_set_protected_mode_interrupt_vector(COM==1 ? 0x0C : 0x0B, &OldRS);
#else
	setvect(COM==1 ? 0x0C : 0x0B, OldRS);
#endif
	enable();
}

void main() {
	printf("Start\n");
	InitLine();

	while (1) {
		if(kbhit())
			break;
		printf("%c\n",Caractere);
	}

	RestoreLine();
	printf("End\n");
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019