Serial Port doesn't start automatically during startup

I am using MODBUS library to communicate with a device using hardware serial. When I power arduino with external power supply with the device connected, Serial communication doesn't start automatically.

If I connect Serial Monitor first, remove and then connect the device, it communicates.

How can I overcome this problem?

For further verification, I uploaded "TwoPortReceive" example code. Rx and Tx LEDs blink only if I connect the serial port first. If I remove the serial cable (USB) with external power, Rx continue to blink.

But if I start with external power without connecting USB cable, Rx Tx don't blink indicating that Serial communication not started.

Please suggest.

Regards

Care to post the sketch in question ?

The code used is pasted below:

#include <SimpleModbusMaster.h>
#include <SoftwareSerial.h>

//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate//200
#define retry_count 10

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2 

#define LED 9

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 14

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
 PACKET1,
 PACKET2,
 PACKET3,
 PACKET4,
 PACKET5,  
 TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
unsigned int prevReq = 0;
// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
 mySerial.begin(9600);

 // Initialize each packet
 modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 0, 3, 0);  
 modbus_construct(&packets[PACKET2], 5, READ_HOLDING_REGISTERS, 3, 5, 3);
 modbus_construct(&packets[PACKET3], 9, READ_HOLDING_REGISTERS, 5, 1, 8);  
 modbus_construct(&packets[PACKET4], 10, READ_HOLDING_REGISTERS, 8, 2, 9);  
 modbus_construct(&packets[PACKET5], 11, READ_HOLDING_REGISTERS, 11, 3, 11);    
 
 // Initialize the Modbus Finite State Machine
 modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);

 pinMode(LED, OUTPUT);
 mySerial.println("Started");
}

void loop()
{
modbus_update();


 mySerial.print("Output: ");
 
 mySerial.print(packets->id);
 mySerial.print("   ");  
 
 mySerial.print(packets->function);
 mySerial.print("   ");    
 
 mySerial.print(packets->address);
 mySerial.print(packets->successful_requests);
 mySerial.print("   ");    
 
 mySerial.print(packets->data);
 mySerial.print("   ");    

 mySerial.print("   ");
 mySerial.print(packets->local_start_address);


for (int i = 0; i < TOTAL_NO_OF_REGISTERS; i = i + 1) {
 mySerial.print(float(regs[i]));
 mySerial.print("   ");
}  
 mySerial.println("");

 delay(1000);

}

Hope the above helps in identifying the solution.
Thanks

Please edit your post and use code tags.

Type
** **[code]** **
before the code
Type
** **[/code]** **
after the code

So your code is shown in a scrolling windows and we don't have those italics.

Next, to my knowledge

The Uno (?) does not flash RX and TX leds if it's not connected to a PC; the leds are connected to the 16U2 'usb' micro, not to the 328 micro. This does not mean that there is no serial communication, only that there is no serial communication via USB. The TX and RX lines of the 328 are still active.

In your scenario, you might fool the 16U2 and it will flash the leds when you first connect to the PC.

Note:
when powering up, you should always have 3 flashes on pin13 ledin very short succession; this is the bootloader trying to communicate with the PC.

Thanks Sterretje for your inputs.

Even though I'm using Arduino for many years, never posted in the forum. I was not aware of the code tags. That's why pasted the code as text. In future, I'll take care of your suggestion regarding posting of code.

You are absolutely right. Since I haven't connected the USB, the 16U2's Tx,Rx LEDs won't blink, but the communication takes place through Tx, Rx of 328P.

Thanks for your valuable input.