delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/17/17:40:47

From: "Alexei A. Frounze" <alex DOT fru AT mtu-net DOT ru>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: com1 programming .... a lot of question
Date: Wed, 17 May 2000 21:47:53 +0400
Organization: MTU-Intel ISP
Lines: 292
Message-ID: <3922DB49.69774632@mtu-net.ru>
References: <8fudhg$jnb$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE>
NNTP-Posting-Host: ppp104-101.dialup.mtu-net.ru
Mime-Version: 1.0
X-Trace: gavrilo.mtu.ru 958589773 62734 212.188.104.101 (17 May 2000 18:56:13 GMT)
X-Complaints-To: usenet-abuse AT mtu DOT ru
NNTP-Posting-Date: 17 May 2000 18:56:13 GMT
X-Mailer: Mozilla 4.72 [en] (Win95; I)
X-Accept-Language: ru,en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

This is a multi-part message in MIME format.
--------------DBD5F7550924A76260A1B9C7
Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 7bit

Try this one.


Good Luck
Alexei A. Frounze
-----------------------------------------
Homepage: http://alexfru.chat.ru
Mirror:   http://members.xoom.com/alexfru

Florent wrote:
> 
> Hi,
> I'm trying to send datas in the com1 to a controller in order to pilot a
> machine.
> 
> I have some questions about the functions _bios_serialcom.
> 
>  I use : state= _bios_serialcom(_COM_STATUS,0,byte);
> 
> then I want to test byte with all the values in order to get the settings of
> the COM1, I do :
> 
> if ((byte&_COM_9600)==_COM_9600) printf ("9600 bauds);
> 
> But I have problems because when I init the com1 at 9600bauds (with
> _COM_INIT) and then I test the status of the com1, the settings are not 9600
> bauds, it's always 110bauds !
> 
> Do I test it in the wrong way or ..... ???
> 
> For _COM_INIT I do :
> 
> state=_bios_serialcom(_COM_INIT,0,_COM_9600 | _COM_CHR8 | _COM_STOP1 |
> _COM_NOPARITY);
> 
> There is something strange also, I test the constante _COM_STOP1, _COM_STOP2
> and _COM_EVENPARITY and there are egal to 0 !!!! Hwo can I know if my
> settings is one stop bit or 2 if both constante egal to 0 !!
> 
> In order to know the value of all constant of _bios_serialcom I have done :
> 
> printf("%#x|%#x|%#x|%#x|%#x|%#x|%#x|%#x|%#x|%#x|%#x|%#x|%#x|%#x|%#x",_COM_CH
> R7,_COM_CHR8,_COM_STOP1,_COM_NOPARITY,
> _COM_ODDPARITY,_COM_EVENPARITY,_COM_110,_COM_150,
> _COM_300,_COM_600,_COM_1200,_COM_2400,_COM_4800,_COM_9600);
> 
> thank you for your help ...
> 
> Florent.
--------------DBD5F7550924A76260A1B9C7
Content-Type: text/plain; charset=koi8-r;
 name="SER_CHAT.TXT"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="SER_CHAT.TXT"

///////////////////////////////////////////////////////
// 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;
}

--------------DBD5F7550924A76260A1B9C7--



- Raw text -


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