Reading data from serial port

i am c# developer, easy to send data to serial fort using com1 but fac

Page 1 of 1

2 Replies - 4576 Views - Last Post: 24 March 2009 - 04:50 AM Rate Topic: -----

#1 sahu.shikha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 09

Reading data from serial port

Post icon  Posted 24 March 2009 - 03:38 AM

namespace SerialPort
{
	public partial class Form1 : Form
	{
		PropertyPage pp = new PropertyPage();

		//create an Serial Port object

		System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort();
		public Form1()
		{
			InitializeComponent();
		}
		
	
		private void Form1_Load(object sender, EventArgs e)
		{
	  try
	{
		//first check if the port is already open
		//if its open then close it
		if (sp.IsOpen == true) sp.Close();

		////set the properties of our SerialPort Object
		//sp.BaudRate = int.Parse(_baudRate);	//BaudRate
		//sp.DataBits = int.Parse(_dataBits);	//DataBits
		//sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits),_stopBits);	//StopBits
		//sp.Parity = (Parity)Enum.Parse(typeof(Parity),_parity);	//Parity
		 sp.PortName = "COM1";   //PortName
		//now open the port
		 sp.Open();
		//display message
		//DisplayData(MessageType.Normal, "Port opened at " + DateTime.Now + "\n");
		//return true
		//return true;
		sp.ReadTimeout = 20000;
	  }
	catch (Exception ex)
	{
	   // DisplayData(MessageType.Error, ex.Message);
		//return false;
	}
   
		 
		}

		private void SaveProperty_Click(object sender, EventArgs e)
		{
			if (pp.bRate == "" && pp.sBits == "")
			{
				Text1.Text = "BaudRate = " + sp.BaudRate.ToString();
			   
			   stopBitText.Text  = "StopBits = " + sp.StopBits.ToString();
			}
			else
			{
				Text1.Text = "BaudRate = " + pp.bRate;
			 stopBitText.Text = "StopBits = " + pp.sBits;
			}

		  dataBitText.Text  = "DataBits = " + sp.DataBits.ToString();
		  parityText.Text = "Parity = " + sp.Parity.ToString();
		  readTimeOutText.Text = "ReadTimeout = " + 
		  sp.ReadTimeout.ToString();
			
		   

			//try
			//{
			//	//open serial port
			//	//sp.Open();
			//	//set read time out to 500 ms
			//	sp.ReadTimeout = 192000;
			//}
			//catch (System.Exception ex)
			//{
			//   //Text1.Text	= ex.Message;
			//}
		}

		private void StartCommunication_Click(object sender, EventArgs e)
		{
			textBox1.Visible = true;
			ReadButton.Visible = true;
			Send.Visible = true;

		}

		private void ReadButton_Click(object sender, EventArgs e)
		{
			//textBox1.Text = "";

			//textBox1.Text = sp.ReadLine();
			//serialPort.DataReceived += new SerialDataReceivedEventHandler(this.serialPort_DataReceived);
			//sp.DataReceived+= new  SerialDataReceivedEventHandler(this.serialPort_DataReceived);
			//sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataRecievedOnPort);
			//sp.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

			sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
		}
		void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			//Thread.Sleep(500);
			string data = sp.ReadLine();
			textBox1.Text = data;
			//this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
		}
		private void si_DataReceived(string data)
		{
			//textBox1.Text = data.Trim();
		}
		//private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
		//{
		//	string str = sp.ReadExisting();
		//}
		private void DataRecievedOnPort(object sender, SerialDataReceivedEventArgs e)
		{

		   textBox1.Text =sp.ReadLine();
		
		}

		private void Send_Click(object sender, EventArgs e)
		{
			try
			{
				//write line to serial port
				sp.WriteLine(textBox1.Text);
				//clear the text box
				textBox1.Text = "";
			}
			catch (System.Exception ex)
			{
			  // Text1.Text = ex.Message;
			}


		}



***added code tags***
-jjsaw5

Is This A Good Question/Topic? 0
  • +

Replies To: Reading data from serial port

#2 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: Reading data from serial port

Posted 24 March 2009 - 04:11 AM

:code:


Could you please provide us with more information.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Reading data from serial port

Posted 24 March 2009 - 04:50 AM

Post your question in the body of your post, not the description.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1