Вы находитесь на странице: 1из 3

10/11/2011

Reading a PC Serial Port Using C: How To Read a Character From The RS

Reading a PC Serial Port Using C


Ho To Read a Character From The RS-232 COM1 of a Personal Computer
J n 4, 2009 Ma in Bell A home PC can be e p o ead cha ac e f om an e e nal de ice. Vol age con ide a ion a e di c a C/C++ code ha ill pe fo m he info ma ion cap e. ed, a ell

The e a e ime hen i i ef l o be able o e a home comp e o ' alk' i h o he de ice h o gh he po . Fo e ample, an e e nal de ice ha ead he empe a e o h midi in a g eenho e ma be able he info ma ion h o gh a b il -in e ial po . Ano he e ample i hen a mea emen de ice can ppl a ( ep e en ing empe a e o h midi ) ha i con e ed o a n mbe h o gh an Analog e o Digi al Con e (ADC), hich i hen en o a PC e ial po fo eco ding. The a icle Reading a Se ial Po U ing Windo Signal Voltage on RS-232 can be ed fo a Windo (95 o abo e) en i onmen .

e ial o end ol age e

Befo e o de ice can comm nica e i h each o he ia a e ial po , he e a e elec onic con ide a ion . I i nece a fo he o de ice o ha e he ame ignal meaning, all "1" i deno ed b a nega i e ol age, and "0" deno ed b a po i i e ol age. The ope a ing ol age ho ld be he ame fo each de ice, and he RS-232C ol age le el i defined a +3 o +15 Vol fo "0", -3 o -15 Vol fo "1". If he ignal f om he comp e and he de ice a e no he ame, he ill no o k, o he ADC o he de ice o p m be +15 V o -15V. RS-232 Communications Protocol The de ice ha a e comm nica ing m 1. 2. 3. 4. 5. be e pec ing he ame "fo ma " of da a:

Bi p io i (Lea Significan Bi fi ) N mbe of da a bi Whe he pa i bi i ed N mbe of S a & S op bi Bi a e (i.e. ba d a e, o bi / econd) hem p.

The fi e i em abo e a e defined b of a e. In he p og am ed he e, he f nc ion called 'po _ini ' e If he e a e an do b abo hich e ing o e, he man fac e da a hee ho ld be con l ed. Soft are to Read RS-232 Port
martin-bell.suite101.com/reading-a-pc-serial-port-using-c-a122145/print

1/3

10/11/2011

Reading a PC Serial Port Using C: How To Read a Character From The RS

The standard Input / Output library is used: #include The serial ports on a standard home PC (COM1 and COM2) may vary, but the most common address for COM1 is hexadecimal 3f8: #define BASE_ADDR 0x3f8 Declare and Define a Function to Set Up the Port Communications The code used to set up RS-232 port communications is as follows: void port_init(int base_addr); void port_init(int port_num) {outportb(port_num+3,131); /* Set-up mode */ outportb(port_num,24); /* For 4800 divisor = 24 */ outportb(port_num+1,0); /* ... and high byte = 0 */ outportb(port_num+4,0); /* No handshake */ outportb(port_num+3,3); /* Back to Communication mode */ outportb(port_num+1,1); Declare and Define a Function to Read From the Serial Port Reading from the Serial Port is achieved with code such as: char get_char(int port_num); char get_char(int port_num) {char x; while(((inportb(port_num+5))&1)!=1) /* While bit 0 of the Line Status Register is zero */ { /* ... Do nothing */ x=inportb(port_num); /* When Line Status Register is 1, a character is ready to be read so read the serial port */ return(x); /* return the character read from the serial port to the main program */ Summar of Reading a Computer Serial COM1 Port Using RS-232 / EIA-232 It is possible to use a home computer to communicate with external devices such as other computers or external
martin-bell.suite101.com/reading-a-pc-serial-port-using-c-a122145/print 2/3

10/11/2011

Reading a PC Serial Port Using C: How To Read a Character From The RS

en o ,

ing he e ial po .

The ignal ol age of he e e nal de ice ma need o be condi ioned fi , and comm nica ion p o ocol m be ag eed be een he de ice , b hen ha i done, he of a e ho n he e ma be ed o ead info ma ion in o he home comp e . The a icle Ho o Config e an RS-232 Po On A PC To Read de c ibe ho o change da a an fe a e, pa i , o d leng h e c. The ha d a e needed o connec o he e ial cable i eadil a ailable and cheap. Cop right Martin Bell. Contact the author to obtain permission for republication.

martin-bell.suite101.com/reading-a-pc-serial-port-using-c-a122145/print

3/3

Вам также может понравиться