Faisal Ansari

Faisal Ansari

  • NA
  • 451
  • 831k

How to receive data from serial ports ?

Mar 30 2012 2:52 AM
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

Answers (1)