How to read data from Serial Port

I read the guide but I still don't know how...

Page 1 of 1

5 Replies - 11593 Views - Last Post: 06 April 2009 - 09:36 AM Rate Topic: -----

#1 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 16-March 09

How to read data from Serial Port

Post icon  Posted 04 April 2009 - 02:49 PM

Hi everyone!
I've got problems with reading my serial port in Visual C# 2008. After reading this guide I still don't know what I have to do to read data from serial port, although I know how to write data to serial port. What I already done is, drag the SerialPort from toolbox components to the form, and then used this function to initialize the Serial port:
		public void inicial_serial_port()
		{
			if (!(serialPort1.IsOpen))
			{
				serialPort1.PortName = PORT;
				serialPort1.BaudRate = 115200;
				serialPort1.Parity = System.IO.Ports.Parity.None;
				serialPort1.Handshake = System.IO.Ports.Handshake.None;
				serialPort1.DataBits = 8;
				serialPort1.StopBits = System.IO.Ports.StopBits.One;
				serialPort1.Open();
			}
		} 


Then I tried this code to read data from serial port:
		public Form1()
		{
			InitializeComponent();
			inicial_serial_port();
			//retrieve number of bytes in the buffer
			int bytes = serialPort1.BytesToRead;
			//create a byte array to hold the awaiting data
			byte[] comBuffer = new byte[bytes];
			//read the data and store it
			serialPort1.Read(comBuffer, 0, bytes);				   
			string str;
			System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
			str = enc.GetString(comBuffer);
			textBox3.Text = str;
		}


What do I have to do to get any data from my serial port?

This post has been edited by wuu: 04 April 2009 - 02:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to read data from Serial Port

#2 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 16-March 09

Re: How to read data from Serial Port

Posted 06 April 2009 - 06:33 AM

C'mon can really no one help me?
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: How to read data from Serial Port

Posted 06 April 2009 - 06:44 AM

when you send a command to the device you are connected to, are you expecting it to send back a response? or do you need to read it manually to get the response? Those are two separate tasks.
Was This Post Helpful? 0
  • +
  • -

#4 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 16-March 09

Re: How to read data from Serial Port

Posted 06 April 2009 - 07:42 AM

View Posteclipsed4utoo, on 6 Apr, 2009 - 05:44 AM, said:

when you send a command to the device you are connected to, are you expecting it to send back a response? or do you need to read it manually to get the response? Those are two separate tasks.


I'm expecting both. When I send data to my device which is microcontroller I expect that microcontroller will response. Also microcontroller can talk without sending command to him simply by applying digital signals to its peripheral units.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: How to read data from Serial Port

Posted 06 April 2009 - 08:26 AM

you can use the DataRecieved event for the serial port object...

_serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);



http://www.dreaminco...snippet2764.htm

this will catch the response coming back from the device.

This post has been edited by eclipsed4utoo: 06 April 2009 - 08:26 AM

Was This Post Helpful? 2
  • +
  • -

#6 wuu  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 16-March 09

Re: How to read data from Serial Port

Posted 06 April 2009 - 09:36 AM

Tnx eclipsed4utoo!
I didn't know how to handle events. Now I think I will manage it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1