Unix C Program to Access Serial Port Through Port.

Accessing the Serial Port through Port Address

Register
COM 1 – 0X3F8
COM 2 – 0X2F8
IER – Interrupt Enable Register
IIR – Interrupt Identification Register
LCR – Line Control Register
MCR – Modem Control Register
LSR – Line Status Register
MSR – Modem Status Register
IER
0x3F9
0x2F9
IIR
0x3FA
0x2FA
LCR
0x3FB
0x2FB
MCR
0x3FC
0x2FC
LSR
0x3FD
0x2FD
MSR
0x3FE
0x2FE
lcr Unix C Program to Access Serial Port Through Port. com+3)


BITS Information
0 word length : lsb
1 word length : msb
2 stop bits (0-1 stop bit,1-2 stop bits)
3 parity enable (0-no parity,1-parity)
4 parity select (0-odd,1-even parity)
5 parity one
6 break
7 divisor latch access bit(dlab) ( 0-transmit or receive,1-ready to get baud rate information)
Word length select :-
msb lsb wordlength
0      0    5
0      1    6
1      0    7
1      1    8
mcr :- (com+4)
Bit  Abbr   Name
0    DTR   data terminal ready
1    rts       request to send
2    out1    user defined output 1
3    out2    user defined output 2
4    loop    test mode loopback
ier :- (com+1)
bit   interupt set
0     data available
1     transmitter holding register empty
2     receiver line status
3     modem status
4-7 always zero
baud rate divisor latches :-


baudrate   decimal   hex   msb   lsb
300            384          80     1       80
1200          96            60     0       60
2400          48            30     0       30
4800          24            18     0       18
9600         12             0c     0       0c
19200       06             06     0       06
38400       03             03     0       03
76800       02             02     0       02
115200     01             01     0       01
lsr :- (com+5)
bit abbr-name meaning if set
0   dr                data ready
1   oe               overrun error
2   pe               parity error
3   fe                framing error
4   bi                break interrupt
5   thre             transmitter holding register empty
6   tsre             transmitter shift register empty
7   spare         permanently set to zero
msr :- (com+6)
bit name            meaning if set
0   Delta CTS    Clear to send line changed.
1   Delta DSR   Data set ready line changed.
2   teri                 trailing edge ring indicator.
3   Delta RLSD received line signal detect changed.
4   cts                 clear to send input is high.
5   dsr                data set ready input is high.
6   ri                    ring indicator is high.
7   rlsd                received line signal detect is high.
iir :- (com+2)
bit2  bit1
1      1 line status
1      0 received data available
0      1 transmitter holding register empty
0      0 modem status
Ioperm :
Ioperm sets the port access permission bits for the process for num bytes starting from port address from to the value turn_on. The use of ioperm requires root privileges.
Iopl :
iopl changes the I/O privilege level of the current process, as specified in level.This family of functions is used to do low level port input and output.
outb, outw, outl, outsb, outsw, outsl – port output
inb, inw, inl, insb, insw, insl – port input
Sample Program to Read and write to Port
#include< sys/io.h >
#include< stdio.h >
#include< stdlib.h >
int main(){
unsigned int com,lsr,chk=0,i=0;
unsigned char ch=0,buf[500];
FILE *fp;
ioperm(0x3f8,8,0×80); /* Ioperm sets the port access permission bits */
iopl(0); /* iopl changes the I/O privilege level */
com=0x3f8;
lsr=com+5;
ch=inb(com);
ch=0;
i=0;
printf(” RECEIVING STATE…n”);
while(ch!=’Z'){
chk=inb(lsr); /* check for the status of bit is on or off */
if((chk&0×01)==0×01) /* if it is on proceed to read the bit */{
ch=inb(com); /* read a byte from 0x3f8 */
buf[i++]=ch; / * bytes readed from the port are stored in buffer */
printf(“Char Recvd.: %cn”,ch);
}
}
buf[i]=’′;
fp=fopen(“RECV.TXT”,”w”);
fprintf(fp,”%s”,buf);
fclose(fp);
printf(” COMPLETEDn”);
return 0;
}

Related posts:

  1. Unix C Program to access Serial Port through device file
  2. How to redirect the Shell Program output to file and Screen in Unix

Comments are closed.