You are on page 1of 7

12/19/12

Serial Communication via RS-232: bioscom e ample -ElectroSofts.com

HOME About Us

Electronics Directory FORUM

Articles/ Tutorials Links

eBooks Contact Us

Serial Comm nica ion ia RS232 Por :


By Harsha Perla and Veena Pai

Download example source code

Introduction to RS-232:
RS232 is the most known serial port used in transmitting the data in communication and interface. Even though serial port is harder to program than the parallel port, this is the most effective method in which the data transmission requires less wires that yields to the less cost. The RS232 is the communication line which enables the data transmission by only using three wire links. The three links provides transmit , receive and common ground...

The transmit and receive line on this connecter send and receive data between the computers. As the name indicates, the data is transmitted serially. The two pins are TXD & RXD. There are other lines on this port as RTS, CTS, DSR, DTR, and RTS, RI. The 1 and 0 are the data which defines a voltage level of 3V to 25V and -3V to -25V respectively. The electrical characteristics of the serial port as per the EIA (Electronics Industry Association) RS232C Standard specifies a maximum baud rate of 20,000bps, which is slow compared to today s standard speed. For this reason, we have chosen the new RS-232D Standard, which was recently released. The RS-232D has existed in two types. i.e., D-TYPE 25 pin connector and D-TYPE 9 pin connector, which are male connectors on the back of the PC. You need a female connector on your communication from Host to Guest computer. The pin outs of both D-9 & D-25 are show below. D-Type-9 D-Type-25 pin pin no. no. 3 2 7 8 6 5
electrosofts.com/serial/inde .html

Pin outs RD TD RTS CTS DSR SG

Function Receive Data (Serial data input) Transmit Data (Serial data output) Request to send (acknowledge to modem that UART is ready to exchange data Clear to send (i.e.; modem is ready to exchange data) Data ready state (UART establishes a link) Signal ground
1/7

2 3 4 5 6 7

12/19/12

Serial Communication via RS-232: bioscom e ample -ElectroSofts.com

5 1 4 9

7 8 20 22

SG DCD DTR RI

Sig a g

Da a Ca ie de ec (Thi i e i ac i e he de de ec a ca ie Da a Te i a Read . Ri g I dica de ec (Bec e ac i e he de i gi g ig a f PSTN

Abo

DTE & DCE:

De ice , hich e e ia cab e f hei c ica i , a e i i ca eg ie . The e a e DCE (Da a C ica i E i e ) a d DTE (Da a Te i a E i e .) Da a C ica i E i e a e de ice ch a de , TA ada e , e e c hi e Da a Te i a E i e i C e Te i a . A ica Da a Te i a De ice i a c e a d a ica Da a C ica i De ice i a M de . Of e e e i a ab DTE DCE DCE DCE eed . DTE DCE i he eed be ee de a d c e, e i e efe ed a e i a eed. Thi h d a fa e eed ha he DCE DCE eed. DCE DCE i he i be ee de , e i e ca ed he i e eed. M e e da i ha e 28.8K 33.6K de . The ef e, e h d e ec he DCE DCE eed be ei he 28.8K 33.6K. C ide i g he high eed f he de e h d e ec he DTE DCE eed be ab 115,200 BPS. (Ma i S eed f he 16550a UART) . The c ica i g a , hich e e, ha e i g f DCE DTE eed . H e e , he eed i 9.6 KBPS, 144 KBPS e c a d he de eed. If e e e a a e ac a a fe i KBPS. Th , hi i The ef e, if DTE da a de a fe i g ha e fi e a 28.8K (DCE- DCE), he he he de c e e i g 115.2 KBPS be ee c e a d h ha e a DCE- DTE eed f 115.2 h he DCE- DTE h d be ch highe ha he de ' c ec i eed. DCE eed i e e a i e fa e ha DCE DCE eed he PC ca e d 115,200 BPS.

Wha i NULL MODEM? N c e i g de c i ed i eZ c ec de DTE' ge he . Thi i c , de c ,ec ed a fe fi e be ee he

electrosofts.com/serial/inde .html

2/7

12/19/12

Serial Communication via RS-232: bioscom e ample -ElectroSofts.com

Figure: Above shows the connections of the Null modem using RS-232D connecter Above-mentioned figure shows the wiring of the null modem. The main feature indicated here is that the to make the computer to chat with the modem rather than another computer. The guest & host computer connected through the TD, RD, and SG pins. Any data that is transmitted through TD line from the Host to Guest is received on RD line. The Guest computer must have the same setup as the Host. The signal ground (SG) line of the both must be shorted so that grounds are common to each computer. The Data Terminal Ready (DTR) is looped back to Data Set Ready and Carrier Detect on both computers. When the Data Terminal Ready is asserted active, then the Data Set Ready and Carrier Detect immediately become active. At this point, the computer thinks the Virtual Modem to which it is connected is ready and has detected the carrier of the other modem. All left to worry about now is the Request to Send and Clear To Send. As both computers communicate together at the same speed, flow control is not needed thus these two lines are also linked together on each computer. When the computer wishes to send data, it asserts the Request to Send high and as it is hooked together with the Clear to Send, It immediately gets a reply that it is ok to send and does so. The Ring indicator line is only used to tell the computer that there is a ringing signal on the phone line. As we do not have, a modem connected to the phone line this is left disconnected To know about the RS232 ports available in your computer, Right click on "My Computer", Goto 'Properties', Select tab 'Device Manager', go to Ports( COM & LPT ), In that you will find 'Communication Port(Com1)' etc. If you right click on that and go to properties, you will get device status. Make sure that you have enabled the port( Use this port is selected). Ho to program the Serial Port using C/C++?

There are two popular methods of sending data to or from the serial port in Turbo C. One is using outportb(PORT_ID, DATA) or outport(PORT_ID,DATA) defined in dos.h. Another method is using bioscom() function defined in bios.h. Using outportb() : The function outportb () sends a data byte to the port PORT_ID . The function outport() sends a data word. These functions can be used for any port including serial port, parallel ports. Similarly to receive data these are used. inport reads a word from a hardware port inportb reads a byte from a hardware port outport outputs a word to a hardware port outportb outputs a byte to a hardware port Declaration: int inport(int portid); unsigned char inportb(int portid); void outport(int portid, int value); void outportb(int portid, unsigned char value);
electrosofts.com/serial/inde .html 3/7

12/19/12

Serial Communication via RS-232: bioscom e ample -ElectroSofts.com

oid o po b(in po id, n igned cha Rema k : inpo o k j like he 80 86 in high b e f om po id + 2. inpo b i a mac o ha ead a b e o po o k j like he 80 86 in high b e o po id + 1. o po b i a mac o ha po id: Inpo - po O po - po al e: Wo d ha o po B e- ha o po b If o call inpo b o o po b o inline code. ie ie o po id; o po id. ie

al e);

c ion IN. I ead

he lo b e of a o d f om po id, he

c ion OUT. I

ie

he lo b e of al e o po id, he

al e A g men

ha inpo and inpo b ead f om; ha o po and o po b ie o

hen do .h ha been incl ded, he a e ea ed a mac o

ha e pand

If o don' incl de do .h, o if o do incl de do .h and # ndef he mac o( ), o ge he f nc ion( ) of he ame name. Re n Val e: # inpo and inpo b e n he al e ead n

# o po and o po b do no e Fo mo e de ail of he e f nc ion U ing bio com:

ead a icle f om beondlogic.com

The mac o bio com () and f nc ion _bio _ e ialcom() a e ed in comm nica ion ing RS-232 connec e . Fi e ha e o e he po i h he need and a ailabili . In hi me hod, ame f nc ion i ed o make he e ing da a o he po and check he a of he po . The e ac ion a e di ing i hed he f nc ion. Along i h ha e a e ending da a and he po o be ed o comm He e a e he dea ail of he T bo C F nc ion fo comm nica ion po Decla a ion: bio com(in cmd, cha ab e, in po ) _bio _ e ialcom(in cmd ,in po , cha ab e) bio com() and _bio _ e ialcom() e he bio e ial comm nica ion o e he I/O po gi en in po .
electrosofts.com/serial/inde .html

hi me hod in he e ial e ing depending on o ing con ol o d, o end ing he fi pa ame e of nica e.

in e

p 0 14 o pe fo m a io

comm nica e he

4/7

12/19/12

Serial Communication via RS-232: bioscom e ample -ElectroSofts.com

cmd: The I/O operation to be performed. cmd (boiscom) 0 1 2 3 cmd(_bios_serialcom)Action _COM_INIT _COM_SEND _COM_RECEIVE _COM_STATUS Initialise the parameters to the port Send the character to the port Receive character from the port Returns rhe current communication port status of the

portid: port to which data is to be sent or from which data is to be read. 0: COM1 1: COM2 2: COM3 abyte: When cmd =2 or 3 (_COM_SEND or _COM_RECEIVE) parameter abyte is ignored. When cmd = 0 (_COM_INIT), abyte is an OR combination of the following bits (One from each group): value of abyte Bioscom 0x02 0x03 0x00 0x04 0x00 0x08 0X10 0x00 0x20 0x40 0x60 0x80 0xA0 0xC0 0xE0 For example, if abyte = 0x8B = (0x80 _COM_CHR8)
electrosofts.com/serial/inde .html

Meaning

_bios_serialcom _COM_CHR7 _COM_CHR8 _COM_STOP1 _COM_STOP2 _COM_NOPARITY _COM_ODDPARITY _COM_EVENPARITY _COM_110 _COM_150 _COM_300 _COM_600 _COM_1200 _COM_2400 _COM_4800 _COM_9600 7 data bits 8 data bits 1 stop bit 2 stop bits No parity Odd parity Even parity 110 baud 150 baud 300 baud 600 baud 1200 baud 2400 baud 4800 baud 9600 baud

0x08

0x00

0x03) = (_COM_1200

_COM_ODDPARITY

_COM_STOP1

5/7

12/19/12

Serial Communication via RS-232: bioscom e ample -ElectroSofts.com

he comm nica ion po i e o 1200 ba d (0 80 = _COM_1200) Odd pa i (0 08 = _COM_ODDPARITY) 1 op bi (0 00 = _COM_STOP1) 8 da a bi (0 03 = _COM_CHR8) To ini iali e he po i h abo e e ing e ha e o i e,

bio com(0, 0 8B, 0); To end a da a o COM1, he fo ma of he f nc ion ill be bio com(1, da a, 0). Simila l bio com(1, 0, 0 ) ill ead a da a b e f om he po . inp The follo ing e ample ill a e ho o e ial po p og am . When a da a i a ailable in he po , i he da a and di pla on o he c een and if a ke i p e ed he ASCII al e ill be en o he po .

# i n c l u d e< b i o s . h > # i n c l u d e< c o n i o . h > # d e f i n eC O M 1 0 # d e f i n eD A T A _ R E A D Y01 0 0 # d e f i n eS E T T I N G S(08 0 00 2 i n m a i n (o i d )

00 0

00 0 )

i n i n ,o u t ,s t a t u s ; b i o s c o m ( 0 ,S E T T I N G S ,C O M 1 ) ;/ * i n i t i a l iet h ep o r t * / c p r i n t f ( " D a t as e n tt o o u : " ) ; h i l e( 1 ) s t a t u s=b i o s c o m ( 3 ,0 ,C O M 1 ) ;/ *a i tu n t i lg e tad a t a * / i f( s t a t u s&D A T A _ R E A D Y ) i f( ( o u t=b i o s c o m ( 2 ,0 ,C O M 1 )&07 F )! =0 ) / * i n p u tad a t a * / p u t c h ( o u t ) ; i f( k b h i t ( ) ) i f( ( i n=g e t c h ( ) )= =2 7 ) / *A S C I Io fE s c * / b r e a k ; b i o s c o m ( 1 ,i n ,C O M 1 ) ; / * o u t p u tad a t a * / r e r n0 ;

When o compile and n he abo e p og am in bo h he comp e , The cha ac e ped in one comp e ho ld appea on he o he comp e c een and ice e a. Ini iall , e e he po o de i ed e ing a defined in mac o e ing . Then e ai ed in an idle loop n il a ke i p e ed o a da a i a ailable on he po . If an ke i p e ed, hen kbhi () f nc ion e n non e o al e. So ill go o ge ch f nc ion he e e a e finding o hich ke i p e ed. Then e a e ending i o he com po . Simila l , if an da a i a ailable on he po , e a e ecei ing i f om he po and di pla ing i on he c een. To check he po , If o ha e a ingle comp e , o can e loop-back connec ion a follo . Thi i mo commonl ed me hod fo de eloping comm nica ion p og am . He e, da a i an mi ed o ha po i elf. Loop-back pl g connec ion i a follo .

electrosofts.com/serial/inde .html

6/7

12/19/12

Serial Communication via RS-232: bioscom e ample -ElectroSofts.com

Fig 2. Loop-back pl g connec ion If o n he abo e p og am i h he connec ion a in hi diag am, he cha ac e en e ed in he ke boa d ho ld be di pla ed on he c een. Thi me hod i helpf l in i ing e ial po p og am i h ingle comp e . Al o o can make change in he po id if o comp e ha 2 232po . Yo can connec he com1 po o com2 of he ame comp e and change he po id in he p og am. The da a en o he po com1 ho ld come o po com2. hen al o ha e e o pe in he ke boa d ho ld appea on he c een. The p og am gi en belo i an e ample o ce code fo e ial comm nica ion p og amme . I i a PC o PC comm nica ion ing RS232. Do nload he code, n ip and n o cha in do mode be een o comp e . U e he p og am o ge mo e idea abo e ial po p og amming. Click here to download example source code: pc2pc. ip Plea e con ac if an p oblem e i in he abo e p og am .

>Interfacing Links:Links related to serial port, bioscom function, rs232, rs485, usb, parallel port etc. >Click here to list other C Programming sites Click he e o end feedback o e email: ha ha@elec o of .com o eena@elec o of .com

No e: E ample gi en abo e a e e ed i h T bo C++ Compile Ve ion 3.0. Ho e e , e do no gi e an g a an fo o king of he p og am in he e ample .

Ads b Google
Hom e |

RS 232 o USB Cable


About Us |

Se ial RS232

USB o Se ial
| e Book s |

RS 232 Se ial Po
Privacy Policy

Article s/ Tutorials | Downloads | Fe e dback | Link s C opyright 2005-2007 e le ctroSofts.com . we bm aste r@e le ctroSofts.com

electrosofts.com/serial/inde .html

7/7

You might also like