delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/02/22/18:06:38

From: "Alexei A. Frounze" <dummy_addressee AT hotmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: rs 232 stuff
Date: Thu, 22 Feb 2001 17:58:21 -0500
Lines: 317
Message-ID: <9745fs$nl5pp$1@ID-57378.news.dfncis.de>
References: <7wMZ3ulbJVB AT 0256597511 DOT t-online DOT de>
NNTP-Posting-Host: ip25.rochester6.ny.pub-ip.psi.net (38.26.84.25)
X-Trace: fu-berlin.de 982882623 24811321 38.26.84.25 (16 [57378])
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Try this one insted (because it complies with both :):
--------8<-------
///////////////////////////////////////////////////////
// Serial Chat program by Alexei A. Frounze (c) 2000 //
//                                                   //
// Compiler: Borland C or DJGPP                      //
//                                                   //
// E-Mail  : alexfru AT chat DOT ru                         //
// Homepage: http://alexfru.chat.ru                  //
// Mirror  : http://members.xoom.com/alexfru         //
///////////////////////////////////////////////////////

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>

#ifdef DJGPP
#include <sys/movedata.h>
#endif

// Baud rates //

#define B_110   1040
#define B_150    768
#define B_300    384
#define B_600    192
#define B_1200    96
#define B_2400    48
#define B_4800    24
#define B_9600    12
#define B_19200    6
#define B_38400    3
#define B_57600    2
#define B_115200   1

// Size of character //

#define BITS_5          0
#define BITS_6          1
#define BITS_7          2
#define BITS_8          3

// Number of stop bits //

#define STOPS_1         0
#define STOPS_2         4

// Parity type //

#define PARITY_NO       0
#define PARITY_EVEN     0x18
#define PARITY_ODD      8

// Registers //

#define IO_REG          0
#define LO_DIV          0
#define HI_DIV          1
#define INT_REG         1
#define INT_ID          2
#define CONTROL         3
#define MODEM           4
#define STATUS          5
#define MSTATUS         6

// some extra constants

#define ESC             27
#define CR              13
#define LF              10

// Base addresses of ports //

int base[4]={0,0,0,0};

// Function that finds base addresses of all serial ports //

int find_base_addresses() {
  int res=0, i;
  short addr;

  for (i=0;i<4;i++) {
#ifndef DJGPP
    addr = (short) peek (0x40, i*2);
#else
    _dosmemgetw (0x400+i*2, 1, &addr);
#endif
    base[i] = addr;
    if (addr) res++;
  }

  return res;
}

int OpenCOM (int ComNo, int BaudRate, char Config) {
  int b;

  if ((ComNo<1) || (ComNo>4)) return 0;
  b = base[ComNo-1];
  if (!b) return 0;

  // Waiting while the previous data is being sent...
  while ((inportb(b+STATUS) & 0x60) != 0x60) {};

  // Programming the port
  outportb (b+CONTROL, 0x80);
  outportb (b+HI_DIV, BaudRate >> 8);
  outportb (b+LO_DIV, BaudRate & 0xFF);
  outportb (b+CONTROL, Config);
  outportb (b+MODEM, 0);
  outportb (b+INT_REG, 0);

  return 1;
}

int CloseCOM (int ComNo) {
  int b;

  if ((ComNo<1) || (ComNo>4)) return 0;
  b = base[ComNo-1];
  if (!b) return 0;

  outportb (b+INT_REG, 0);
  outportb (b+MODEM, 0);
  return 1;
}

int SendChar (int ComNo, char value) {
  int b;

  if ((ComNo<1) || (ComNo>4)) return 0;
  b = base[ComNo-1];
  if (!b) return 0;

  // Waiting while previous character is being sent
  while ((inportb(b+STATUS) & 0x20) == 0) {};

  // Sending the character
  outportb (b, value);

  return 1;
}

int ReceiveChar (int ComNo, char *value) {
  int b;

  if ((ComNo<1) || (ComNo>4)) return 0;
  b = base[ComNo-1];
  if (!b) return 0;

  // If there is no any available character, quit with result of 0
  if ((inportb(b+STATUS) & 1) == 0) return 0;

  // otherwise we get the character form the port and return result of 1
  *value = inportb (b);
  return 1;
}

int main() {
  int serial_ports, i;
  int ComNo;
  char c, k;

  clrscr();
  printf ("Serial Chat program by Alexei A. Frounze (c) 2000\n\n");
  printf ("E-Mail  : alexfru AT chat DOT ru\n");
  printf ("Homepage: http://alexfru.chat.ru\n");
  printf ("Mirror  : http://members.xoom.com/alexfru\n\n");
  serial_ports = find_base_addresses();

  printf ("There are %d serial ports available.\n", serial_ports);
  if (!serial_ports) return 0;

  printf ("\nBase addresses are:\n");
  for (i=0;i<4;i++)
    if (base[i])
      printf ("  COM%d ... 0x%X\n", 1+i, base[i]);

  printf ("\nPlease choose COM port number: ");
  ComNo = getch();
  printf ("%c\n", ComNo);

  ComNo -= '0';
  if ((ComNo < 1) || (ComNo > 4)) {
    printf ("Ivalid number.\n");
    return 0;
  };
  if (!base[ComNo-1]) {
    printf ("COM%d doesn't exist.\n", ComNo);
    return 0;
  };

  printf ("You've choosen COM%d\n", ComNo);

  if (!OpenCOM (ComNo, B_9600, BITS_8+STOPS_1+PARITY_NO)) {
    printf ("Couldn't open the port.\n");
    return 0;
  };

  printf ("\nCOM%d port is open. Enjoy the chat. Hit ESC for quit.\n\n",
ComNo);
  while (ReceiveChar (ComNo, &c)) {};   // skip data from the previos
session

  do {
    if (ReceiveChar (ComNo, &c)) {      // if there is a character
available...
      switch (c) {
        case ESC:
          printf ("Remote machine disconnected.\n"); // :))
          break;
        case CR:
          printf ("\n");                // move the cursor to the new line
          break;
        default:
          printf ("%c", c);             // print the received chracter
      }
    }
    if (kbhit()) {
      k = getch();
      if (!SendChar (ComNo, k)) {
        printf ("Couldn't send a character.\n");
        return 0;
      };
    } else k=0xFF;
  } while (k!=ESC);                     // quit (disconnect :)

  CloseCOM (ComNo);
  return 0;
}
--------8<-------
Good Luck
--
Alexei A. Frounze
alexfru [AT] chat [DOT] ru
http://alexfru.chat.ru
http://members.xoom.com/alexfru/
http://welcome.to/pmode/


"Dietmar Segbert" <Dietmar DOT Segbert AT T-ONLINE DOT de> wrote in message
news:7wMZ3ulbJVB AT 0256597511 DOT t-online DOT de...
> Hello,
> i have a routine to controll the rs 232 serial Port.
> It is writen in turbo C.
> Compiling with GCC he makes some Errors.
> "oldirq" undefined refernce.
>
> At the Top of the file i found:
> ...
> static void (interrupt far *oldirq)(void);
>
> ...
>
> int init_232(serial_232_t *sr)
> {
>     int com;
>     unsigned cntlword;
>
>         com=sr->comport ; /* -1 ; */
>         uart_adrs=irq_adr_table[com-1];    /* get base address */
>
>         /* set default IRQ */
>         /* set IRQ=4 for COM1/3, 3 for COM 2/4 */
>             sr->irq=(com & 1) == 1 ? 4 : 3;
>     irqnum=sr->irq;
>
>     /* calculate irq mask bit for PIC */
>     irqmask=1<<sr->irq;
>     oldirq=getvect(sr->irq+8);          /* get old IRQ vector */
>     setvect(sr->irq+8,comint);          /* instal serial interupt handler
*/
>     outportb(uart_adrs+LCR,0x83);       /* none/8/1 - DLAB set */
>
>     /* set baud rate */
>     outportb(uart_adrs+DATA_REG,baud_table[sr->baud]&0xFF);
>     outportb(uart_adrs+IER,baud_table[sr->baud]>>8);
>
>     /* calculate control word for LCR */
>     cntlword=(2*sr->parity-sr->parity?1:0)<<4;
>     cntlword|=2*(sr->stops-1);
>     cntlword|=sr->bits-5;
>     outportb(uart_adrs+LCR,cntlword);
>     outportb(uart_adrs+MCR,0xF);           /* enable interrupts */
>     outportb(uart_adrs+IER,0xF);
>     outportb(0x21,inportb(0x21)&~irqmask);
>
>     return 0;
> }
>
>
> /*
>    CALL THIS FUNCTION BEFORE PROGRAM EXIT!
>
>    or before calling init_232() for a second time
> */
> void deinit_232(void)
>    {
>    outportb(uart_adrs+IER,0);             /* clear UART interrupts */
>    outportb(uart_adrs+MCR,0);
>    outportb(0x21,inportb(0x21)|irqmask);  /* clear PIC */
>    setvect(irqnum+8,oldirq);              /* restore interrupt vector */
>    }
>
>
>
> How can i compile this serial stuff with GCC.
> If you want i send it complete.
> It is from Alex Russel written in turbo C.
>
> Thanks
>
> Dietmar
>


- Raw text -


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