I am working with COM2 (RX1, TX1) and COM4 (RX3, TX3) of the USBizi board and I am facing a peculiar problem with the data received delegate function of the serial port class.
COM2 is connected to a Bluetooth Serial adapter board and COM4 is connected to a FTDI USB-Serial board. They both have the same data received delegate function as shown below.
The Serial Port initialization code
try
{
// The Bluetooth module is connected to RX1 and TX1 pins of USBizi.
// COM1 string refers to RX0 TX0, Accordingly RX1 TX1 refers to COM2
BTSerialPort = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One);
BTSerialPort.ErrorReceived += new SerialErrorReceivedEventHandler(BTSerialPort_ErrorReceived);
BTSerialPort.DataReceived += new SerialDataReceivedEventHandler(BTSerialPort_DataReceived);
BTSerialPort.Open();
// FTDI Serial Port
FTDISerialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
FTDISerialPort.ErrorReceived += new SerialErrorReceivedEventHandler(BTSerialPort_ErrorReceived);
FTDISerialPort.DataReceived += new SerialDataReceivedEventHandler(BTSerialPort_DataReceived);
FTDISerialPort.Open();
}
catch (Exception EXP)
{
Debug.Print("Error Initializing and Opening Serial Port: " + EXP.Message);
}
Here is the Data Received Event Handler (Green refers to a LED connected to a PWM port)
static void BTSerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Green.Set(100, 50);
// Read data from the IN buffer
int NoOfBytesReceived = BTSerialPort.BytesToRead;
NoOfBytesReceived = FTDISerialPort.BytesToRead;
byte[] ReceivedData = new byte[NoOfBytesReceived];
// Read only the first character as it indicates whether we want Command or Data mode
int NoOfBytesRead = BTSerialPort.Read(ReceivedData, 0, NoOfBytesReceived);
NoOfBytesRead = FTDISerialPort.Read(ReceivedData, 0, NoOfBytesReceived);
//Echo what you received
BTSerialPort.Write(ReceivedData, 0, NoOfBytesRead);
FTDISerialPort.Write(ReceivedData, 0, NoOfBytesRead);
}
When the board is connected to my computer through the USB slave cable and I deploy the program from within VS 2008, I am able to catch the data received event. All of these things happen as expected
1. I can place a breakpoint inside the event handler and catch the event when I send a character through the serial port.
2. The green LED starts to glow when I send a character
3. The character is echoed back to my terminal program
But, when I remove the board out of my computer and power it up with a battery and send serial data from either of the COM ports, the event is not triggered. None of the above three happens.
In the Beginner's Guide book, there is no example showing the use of Data Received Event function.
I am curious why my board is acting the way it is.
Thanks
Sreekar