SerialPort
im tring to send AT command to my gsm modem using serialport. but its not working properly. Reading event not working!. please help me to doing this.
--
#region
Namespace Inclusionsusing
System;using
System.IO.Ports;using
System.Windows.Forms;#endregion
namespace
SerialPortExample{
classSerialPortProgram{
// Create the serial port with basic settingsprivateSerialPort port =newSerialPort("COM1", 9600,Parity.None, 8,StopBits.One);[
STAThread]staticvoid Main(string[] args){
SerialPortProgram sp =newSerialPortProgram();sp.SendSampleData();
Application.Run();}
privatevoid SendSampleData(){
Console.WriteLine("Sending Data:" +port.IsOpen);port.WriteLine(
"AT");}
private SerialPortProgram(){
Console.WriteLine("Incoming Data:");// Attach a method to be called when there is data waiting in the port's bufferport.DataReceived +=
newSerialDataReceivedEventHandler(port_DataReceived);port.ErrorReceived +=
newSerialErrorReceivedEventHandler(sp_ErrorEvent);//port.ReceivedBytesThreshold = 1;Console.WriteLine("ReadBufferSize:" + port.ReadBufferSize);//port.ReadBufferSize = 2047;port.ReceivedBytesThreshold = 100;
port.ReadTimeout = 3000;
// 3 second timeoutport.NewLine =
"\r";// Begin communicationsport.Open();
//port.DtrEnable = true;}
privatevoid sp_ErrorEvent(object sender,SerialErrorReceivedEventArgs e){
Console.WriteLine(">>e");}
privatevoid port_DataReceived(object sender,SerialDataReceivedEventArgs e){
Console.WriteLine(">>");// Show all the incoming data in the port's buffer//Console.WriteLine(port.ReadExisting());int c;while (port.BytesToRead > 0){
c = port.ReadByte();
Console.WriteLine(">"+c);}
}
}
}
-
console output :
Incoming Data:
ReadBufferSize:4096
Sending Data:True
-
Thanks

