C Code help

Discussion in 'Programmer's Corner' started by Xeeshan Qureshi, May 12, 2009.

  1. Xeeshan Qureshi

    Thread Starter Member

    Jan 13, 2009
    14
    0
    Hi!
    Days ago, I posted a query about bioscom() function in c++[turbo c], in reference with serial port programming. Though I was unable to get through with that problem but instead found outportb(), inportb() functions in c for the same purpose.
    Following are the serial port settings which I copy pasted into my code and it worked. Now I want to understand these settings (i.e. the code)...
    Please help me.

    #define PORT1 0x3F8
    outportb(PORT1 + 1 , 0);
    outportb(PORT1 + 3 , 0x80);
    outportb(PORT1 + 0 , 0x0c);
    outportb(PORT1 + 1 , 0x00);
    outportb(PORT1 + 3 , 0x03);
    outportb(PORT1 + 2 , 0xC7);
    outportb(PORT1 + 4 , 0x0B);
     
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    This code writes bytes of data to five serial ports: 0x3F8, 0x3F9, 0x3FA, 0x3FB, and 0x3FC.

    I found the same code as above on the internet, with comments, so that might help you understand the purpose of each line of code above. Here's a link to where this code appears: http://www.tek-tips.com/viewthread.cfm?qid=624898.
    Code ( (Unknown Language)):
    1.  
    2. /* This program is used to pull data from the serial port */
    3.  
    4. #include <dos.h>
    5. #include <stdio.h>
    6. #include <conio.h>
    7.  
    8. #define PORT1 0x3F8  /* Defines Serial Port Base Address (COM1 */
    9.  
    10. void main(void){
    11.     unsigned char c = 0;
    12.     unsigned char chrctr = 0;
    13.     /*int exit = 1; */
    14.  
    15.     outportb(PORT1 + 1, 0); /* Turn off interrupts */
    16.  
    17.     /* PORT1 Communication Settings */
    18.  
    19.     outportb(PORT1 + 3, 0x80); /* Set DLAB ON */
    20.     outportb(PORT1 + 0, 0x0C); /* Set the baud rate to 9600 */
    21.  
    22.     outportb(PORT1 + 1, 0x00); /* Set Baud - Divisor latch HIGH */
    23.     outportb(PORT1 + 3, 0x03); /* 8 bits, no parity, 1 stop */
    24.     outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */
    25.     outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2) */
    26.  
     
  3. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
Loading...