interfacing software with arduino uno

I wish to ask that why my arduino program cannot comunicate by using the rs232. The pins I used are Pin 0 and Pin 1 to transfer and received the data. Below is my coding but I dont know where I program wrongly, can anyone help me?

sketch_sep08a.ino (4.61 KB)

   while(Serial.available()<9){
       //light Led 
   } 
   if (Serial.available()>7) {

The while loop is going to spin until there are 9 bytes of data to read. It’s silly, then, to test for there being more than 7 byes. 9 will ALWAYS be more than 7.

Do not post code with huge chunks commented out. Delete that crap.

Do not post piss-poorly formatted code. Use Tools + Auto Format first.

Some comments in the code to explain what you think it’s doing would be good.

So would a schematic.

Why are you connecting the device to the hardware serial pins? Keep those for debugging, until you KNOW that the code is working. Use SoftwareSerial to create a software serial port on two other pins.

Sorry I am new in coding, so you mean that I need to debug it by using SoftwareSerial function?

You should use SoftwareSerial to make the connection to your RS232 device and leave the Tx0 and Rx0 pins free to communicate with the Arduino Serial Monitor.

Note that “proper” RS232 uses voltages that can damage an Arduino and you may need something like a MAX232 to convert the RS232 signals to the TTL levels that the Arduino uses.

The code examples in Serial Input Basics may be useful. They can be easily adapted to work with SoftwareSerial.

…R