delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/12/13:45:56

From: "Michal Strelec" <strelec AT adam DOT osu DOT cz>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Setting baud rate of serial port
Date: Mon, 12 Jul 1999 08:21:24 +0200
Organization: Czech Technical University
Lines: 106
Message-ID: <7mc1l3$iqq$1@ns.felk.cvut.cz>
References: <37861656 DOT 79C0AF5B AT hotmail DOT com>
NNTP-Posting-Host: cl106161.osu.cz
X-Newsreader: Microsoft Outlook Express 4.72.3155.0
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi,
1.
you can do it MUCH easier if you use alredy written communication library
pmcom (included with DJGPP/v2tk/pmcom10.zip). You have to download it,
install it, compile it (there are makefiles for DJGPP and BC3.1) and at last
link it with your program.
Read manual (everything is writen there). First you have to open port which
you want
ret=COMPortOpen(pParam->com,pParam->rate,pParam->databit,pParam->parity,
      pParam->stpbit,0,EventHandler[pParam->com]);
where EventHandler is function where to control is passed after
communication interrupt occuares.
Thus you have everything set. You can now send, receive data (of course with
pmcom functions)

2.
If you want to use (for some STRANGE reason) low level setting coms. You
have to this:
int BaudRate(char channel, unsigned int rate, char databits, char parity,
char stopbits)
{
  unsigned int port = ComPar[channel].port;
  unsigned char codeRate;
  char codeParity = (parity ? ((parity%2) ? 8 : 24) : 0);
  char codeStopbits = (stopbits==1) ? 0 : 4;
  char codeDatabits = databits-5;;

  if ( rate <= 150 )
    codeRate = 0x0300;
  else if ( rate <= 300 )
    codeRate = 0x0180;
  else if ( rate <= 600 )
    codeRate = 0x00c0;
  else if ( rate <= 1200 )
    codeRate = 0x0060;
  else if ( rate <= 2400 )
    codeRate = 0x0030;
  else if ( rate <= 4800 )
    codeRate = 0x0018;
  else if ( rate <= 9600 )
    codeRate = 0x000c;
  else if ( rate <= 19200 )
    codeRate = 0x0006;
  else if ( rate <= 28800 )
    codeRate = 0x0004;
  else if ( rate <= 38400 )
    codeRate = 0x0003;
  else if ( rate <= 57600 )
    codeRate = 0x0002;
  else
    codeRate = 0x0001;
  outportb(port+3, 0x80);               /* Divisor latch access bit     */
  outportb(port+0, codeRate & 0xFF);    /* Speed - lower byte           */
  outportb(port+1, codeRate >> 8);      /* Speed - higher byte          */
  outportb(port+3, codeDatabits | codeParity | codeStopbits);
     /* The other com. parameters    */
  return 1;
}


>Hello,
>
>This is a follow-up of my previous posting ("Your help would be
>appreciated..."). I am wondering how I can set the baud rate of the
>serial port. I am using outp to send 1's and 0's to pin 4 of COM 2,
>using outp((unsigned short)(0x2f8+4),(unsigned char)0x0F);. If I do not
>set the baud rate, the signals are sent approximately every 1 to 3
>microseconds. At 9600 baud, the duration of each 1 or 0 should be about
>104 usecs. How would I set the baud rate? Is there anything else I would
>need to do before sending the signals, such as initializing the port --
>and then how would I do that? It's supposed to be 8 data bits, no
>parity, 1 stop bit, but I'm already inserting the stop bit by myself.
>
>I'm pretty sure this is wrong, but this is what I'm doing:
>
>ioctl(0x2f8+4,(('X'<<8)+7),224);
>
>...since this wouldn't work:
>
>ioctl(0x2f8+4,TXSBAUD,_COM_9600);
>
>...which I tried because this was in bios.h:
>
>#define _COM_9600          224   /* 9600 baud. */
>
>...and this was in sys/ioctl.h:
>
>enum tty_ioctl {
>  TXISATTY = ('X'<<8),    /* quick path for isatty */
>  TXTTYNAME, /* quick path for ttyname */
>  TXGETLD, /* get line discipline */
>  TXSETLD, /* set line discipline */
>  TXGETCD, /* get control disciplines */
>  TXADDCD, /* add control discipline */
>  TXDELCD, /* delete control discipline */
>  TXSBAUD, /* set integer baud rate */
>  TXGBAUD, /* get integer baud rate */
>  TXSETIHOG, /* set the input buffer limit */
>  TXSETOHOG, /* set the output buffer limit */
>  TXGPGRP, /* get p grp with posix security */
>  TXSPGRP               /* set p grp with posix security */
>};
>
>Please help! Thanks in advance.


- Raw text -


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