Hello,
My question is that, i am creating an application in which application i have to receive data from serial ports and i am trying this code
class Program
{
static void Main(string[] args)
{
SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity =
Parity.None;
mySerialPort.StopBits =
StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake =
Handshake.None;
mySerialPort.DataReceived +=
new SerialDataReceivedEventHandler(DataRecivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataRecivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string inData = sp.ReadExisting();
Console.WriteLine("Data Recived...");
Console.WriteLine(inData);
}
}
but exception occured "The semaphore timeout period has expired" what does it mean and what is the solution about it please reply soon Thanks
Loading
Glenn PattonPosted Jun 26, 2012, 3:59 AM
Umm I think part of the problem is the way you have it structured. The ReadKey() will pick up the first character pressed on the keyboard and then exit, if there is no data at the port it will simply exit. It would display any text at the serial port that can be seen by the event handler try putting in the line
mySerialPort.WriteLine("Hello World!!\n");
after the serial port open and above the ReadKey(); this will send a piece of data out that can be read by the event handler if the pins 2&3 of the D-type 9 way are shorted together. A simple jumper from a PATA IDE connector will do the job!
Good Luck
Glenn
Oops! almost forgot..... by the looks of your code you have included System.IO.Ports it the
Using section
using System.IO.Ports; but just be sure to. Sorry early morning and lack of coffee!
Glenn