Serial Communication via RS-232_ Bioscom Example -ElectroSofts
Short Description
Download Serial Communication via RS-232_ Bioscom Example -ElectroSofts...
Description
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.
Pin outs
Function
3
2
RD
Receive Data (Serial data input)
2
3
TD
Transmit Data (Serial data output)
7
4
RTS
8
5
CTS
6
6
DSR
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)
5
7
SG
Signal ground
electrosofts.com/serial/inde .html
1/7
12/19/12
Serial Communication via RS-232: bioscom e ample -ElectroSofts.com
Abo
5
7
SG
1
8
DCD
4
20
DTR
9
22
RI
Sig a g
d
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
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
de i g
electrosofts.com/serial/inde .html
i c
ed i eZ
c ec de
DTE' ge he . Thi i c , de c ,ec
ed
a
fe fi e be ee
he
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
al e);
Rema k : inpo o k j like he 80 86 in high b e f om po id + 2.
c ion IN. I ead
he lo b e of a o d f om po id, he
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
ie
c ion OUT. I
ie
he lo b e of al e o po id, he
al e A g men
po id: Inpo - po O po - po
ha inpo and inpo b ead f om; ha o po and o po b
ie o
al e: Wo d ha o po
ie
o po id;
B e- ha o po b
ie
o po id.
If o call inpo b o o po b o inline code.
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
# o po and o po b do no e Fo mo e de ail of he e f nc ion
n
ead a icle f om beondlogic.com
U ing bio 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
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.
.
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
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)
cmd(_bios_serialcom)Action
0
_COM_INIT
Initialise the parameters to the port
1
_COM_SEND
Send the character to the port
2
_COM_RECEIVE
Receive character from the port
3
_COM_STATUS
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
Meaning
_bios_serialcom _COM_CHR7
7 data bits
0x03
_COM_CHR8
8 data bits
0x00
_COM_STOP1
1 stop bit
0x04
_COM_STOP2
2 stop bits
0x00
_COM_NOPARITY
0x08
_COM_ODDPARITY
Odd parity
0X10
_COM_EVENPARITY
Even parity
0x00
_COM_110
110 baud
0x20
_COM_150
150 baud
0x40
_COM_300
300 baud
0x60
_COM_600
600 baud
0x80
_COM_1200
1200 baud
0xA0
_COM_2400
2400 baud
0xC0
_COM_4800
4800 baud
0xE0
_COM_9600
9600 baud
No parity
For example, if abyte = 0x8B = (0x80 _COM_CHR8) electrosofts.com/serial/inde .html
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 .
#include #include #define COM1 0 #define DATA_READY 0 100 #define SETTINGS ( 0 80 0 02 in main( oid)
0 00
0 00)
in in, out, status; bioscom(0, SETTINGS, COM1); /*initiali e the port*/ cprintf("Data sent to ou: "); hile (1) status = bioscom(3, 0, COM1); /* ait until get a data*/ if (status & DATA_READY) if ((out = bioscom(2, 0, COM1) & 0 7F) != 0) /*input a data*/ putch(out); if (kbhit()) if ((in = getch()) == 27) /* ASCII of Esc*/ break; bioscom(1, in, COM1); /*output a data*/ re rn 0;
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 .
RS 232 o USB Cable
Ads b Google Hom e
|
About Us
electrosofts.com/serial/inde .html
|
Se ial RS232
USB o Se ial
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
|
RS 232 Se ial Po e Book s
|
Privacy Policy
7/7
View more...
Comments