Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

(Ask) Read string via RS232 for MCS8051

Not open for further replies.


Newbie level 4
Jul 30, 2009
Reaction score
Trophy points
Activity points

Dear All,

Can anyone share sample program in C (keil uvision) to receive string serially. I can send array of character (string) from PC, but i do know how to read it in the MCU.

Thanks a lot for any help.

read rs232 string ?

Well, does your particular MCU have an internal RS232 transceiver? From what I have seen most of them work using interrupts where there have a receive interrupt and there is a register that is filled with the received data.

Now, assuming that the uVision compiler conforms to most of the ANSI C specification I would expect a receiving program using hardware to look something like this:

volatile unsigned char rs232_rx_register;
unsigned char* receivedString; //allocate this somewhere to some length...
unsigned char receivedLength; //something to make this go a bit faster. set to 0 before interrupts are enabled
unsigned char newString; //read this in your main function. it is a flag. set to 0 before interrupts are enabled
 * The RS-232 interrupt
void interrupt()
  ///....code determining that this is indeed the rs232 interrupt....

  receivedString[receivedLength] = rs232_rx_register;
  if (receivedString[receivedLength] == 0x00) { newString = 1; }

  ///....other interrupt code or something....
 * Your main program
void main()
  ///...startup code...
  while(1) //your main loop
    ///...other code...
    //disable interrupts to reduce the chance of reading while writing (it sucks when that happens)
    if (newString==1)
      //do something with this new string that we just got
      receivedLength = 0; //reset everything for the next string
      newString = 0;
    //re-enable interrupts so you can get more strings
    ///...other code...
Real simple with hardware. Also, datasheets are helpful so that you know how to set the baud rate and such.

If you have to use software because you don't have a hardware solution make sure to take a look at how RS232 works. You may also notice that since it uses +12V and -12V you can't plug it directly into your MCU. You will probably need to use a MAX232 or a similar chip to invert and convert the voltage levels into TTL/CMOS levels. If you do end up using the MAX232 in particular, you should remember that it will invert the signal, so that it is normally high rather than normally low. I would write out a software example for you, but it is rather hard to do without knowing exactly what you are using (not to mention that software serial solutions take a while to write and my session would probably expire before I got done :D).

rs 232 code interrupt code

Explaining it in another way,

1.Ur controller will have a transceiver section for asynchronous data transfer which we call a UART.
2.If u want to receive a data from pc using UART, u need to use a sfr present in the controller.Whatever the data is received over serial port, can be read from SBUF, a special function register.
3.Whenever data is received from external means, receive interrupt occurs and it will lead to ISR. Receive interrupt occurs only if u enable serial port interrupt during controller intialization.
4.ISR should be written in such a way that u have to collect the data from SBUF and place it in a array as per ur requirement.This has been clearly explained in the above post.

Not open for further replies.

Part and Inventory Search

Welcome to