Serial Port Communication

not able to read values from combobox and display it in richtextbox ca

Page 1 of 1

2 Replies - 3937 Views - Last Post: 21 January 2009 - 03:53 AM Rate Topic: -----

#1 pradeepjsettikere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-January 09

Serial Port Communication

Posted 21 January 2009 - 03:36 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


namespace Test
{

	public partial class SerialPort : Form
	{
		//static SerialPort USB_RS235;
		//byte port_open = 0;

		//SerialPort port = new SerialPort();

		public string PortName;
		public string Parity;
		public string Databits;
		public string Baudrate;
		public string Stopbits;
		//public string Name;

		public SerialPort()
		{
			InitializeComponent();
			//USB_RS235 = new SerialPort();
		}

		private void btncancel_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void SerialPort_Load(object sender, EventArgs e)
		{
			SerialPort port = new SerialPort();
				//Reading portnames
			this.cbComport.Items.Clear();
			string[] ports = System.IO.Ports.SerialPort.GetPortNames();
			foreach (string port in ports)
			{
				this.cbComport.Items.Add(port);
			}
			//cbComport.Text = "Select Port";
			this.cbComport.SelectedIndex = 0;

			//	//Reading parity
			this.cbparity.Items.Clear();
			Parity[] parities = (Parity[])Enum.GetValues(typeof(Parity));
			foreach (Parity parity in parities)
			{
				this.cbparity.Items.Add(parity.ToString());
			}
			this.cbparity.SelectedIndex = 0;

				//Reading stopbits
			this.cbStopbits.Items.Clear();
			StopBits[] stopbit = (StopBits[])Enum.GetValues(typeof(StopBits));
			foreach (StopBits stopbits in stopbit)
			{
				this.cbStopbits.Items.Add(stopbits.ToString());
			}
			this.cbStopbits.SelectedIndex = 0;

			//this.cbDatabits.SelectedIndex = 0;
			//this.cbBaudrate.SelectedIndex = 0;



		}
		public bool OpenPort()
		{
			if (port.open = true)
				port.Close();
			port.PortName = cbComport.Text;
			port.Parity = cbparity.Text;
			port.Databits = cbDatabits.Text;
			port.Baudrate = cbBaudrate.Text;
			port.Stopbits = cbStopbits.Text;
			port.open();
		   
		//private void cbComport_SelectedIndexChanged(object sender, EventArgs e)
		//{
		//	//if (port_open != 0)
		//	//{
		//	//	USB_RS235.Close();
		//	//	port_open = 0;
		//	//}
		//	port.Close();

		//	port.PortName = cbComport.Text;
		//	port.Parity = cbparity.Text

		//	USB_RS235.PortName = cbComport.Text;
		//	USB_RS235.Parity = cbparity.Text;
		//	USB_RS235.DataBits = cbDatabits.Text;
		//	USB_RS235.BaudRate = cbBaudrate.Text;
		//	USB_RS235.StopBits = cbStopbits.Text;
		//	USB_RS235.Name = textBox1.Text;



		}

		private void btnok_Click(object sender, EventArgs e)
		{
			byte[] buff = new byte[6];
			port.open();
			buff[0] = 0;
			buff[1] = 1;
			buff[2] = 2;
			buff[3] = 3;
			buff[4] = 4;
			buff[5] = 5;	   
			MessageBox.Text = string.Format("Data returned={0},{1},{2},{3}", buff[0], buff[1], buff[2], buff[3]);
		}


		}

		}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Serial Port Communication

#2 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: Serial Port Communication

Posted 21 January 2009 - 03:42 AM

Hallo and welcome to DIC!

At first, please use our code-tags!

Eclipsed4utoo made a tutorial about this. Look at : Serial Port Communication

hope this helps

regards

Ben.Ihde
Was This Post Helpful? 0
  • +
  • -

#3 pradeepjsettikere  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-January 09

Re: Serial Port Communication

Posted 21 January 2009 - 03:53 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


namespace Test
{

	public partial class SerialPort : Form
	{
		//static SerialPort USB_RS235;
		//byte port_open = 0;

		//SerialPort port = new SerialPort();

		public string PortName;
		public string Parity;
		public string Databits;
		public string Baudrate;
		public string Stopbits;
		//public string Name;

		public SerialPort()
		{
			InitializeComponent();
			//USB_RS235 = new SerialPort();
		}

		private void btncancel_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void SerialPort_Load(object sender, EventArgs e)
		{
			SerialPort port = new SerialPort();
				//Reading portnames
			this.cbComport.Items.Clear();
			string[] ports = System.IO.Ports.SerialPort.GetPortNames();
			foreach (string port in ports)
			{
				this.cbComport.Items.Add(port);
			}
			//cbComport.Text = "Select Port";
			this.cbComport.SelectedIndex = 0;

			//	//Reading parity
			this.cbparity.Items.Clear();
			Parity[] parities = (Parity[])Enum.GetValues(typeof(Parity));
			foreach (Parity parity in parities)
			{
				this.cbparity.Items.Add(parity.ToString());
			}
			this.cbparity.SelectedIndex = 0;

				//Reading stopbits
			this.cbStopbits.Items.Clear();
			StopBits[] stopbit = (StopBits[])Enum.GetValues(typeof(StopBits));
			foreach (StopBits stopbits in stopbit)
			{
				this.cbStopbits.Items.Add(stopbits.ToString());
			}
			this.cbStopbits.SelectedIndex = 0;

			//this.cbDatabits.SelectedIndex = 0;
			//this.cbBaudrate.SelectedIndex = 0



		}
		public bool OpenPort()
		{
			if (port.open = true)
				port.Close();
			port.PortName = cbComport.Text;
			port.Parity = cbparity.Text;
			port.Databits = cbDatabits.Text;
			port.Baudrate = cbBaudrate.Text;
			port.Stopbits = cbStopbits.Text;
			port.open();
		   
		//private void cbComport_SelectedIndexChanged(object sender, EventArgs e)
		//{
		//	//if (port_open != 0)
		//	//{
		//	//	USB_RS235.Close();
		//	//	port_open = 0;
		//	//}
		//	port.Close();

		//	port.PortName = cbComport.Text;
		//	port.Parity = cbparity.Text

		//	USB_RS235.PortName = cbComport.Text;
		//	USB_RS235.Parity = cbparity.Text;
		//	USB_RS235.DataBits = cbDatabits.Text;
		//	USB_RS235.BaudRate = cbBaudrate.Text;
		//	USB_RS235.StopBits = cbStopbits.Text;
		//	USB_RS235.Name = textBox1.Text;



		}

		private void btnok_Click(object sender, EventArgs e)
		{
			byte[] buff = new byte[6];
			port.open();
			buff[0] = 0;
			buff[1] = 1;
			buff[2] = 2;
			buff[3] = 3;
			buff[4] = 4;
			buff[5] = 5;	   
			MessageBox.Text = string.Format("Data returned={0},{1},{2},{3}", buff[0], buff[1], buff[2], buff[3]);
		}


		}

		}





The code inside private void serial port _load where i am reading values into combobox.I don't no how to display this data in richtext box.please help me.In my form i have used five combo boxes.Now these are displaying the values.Now i need to open the port and putting these values in a buffer and display it in arichtext box.

** Edit ** :code:

This post has been edited by pradeepjsettikere: 21 January 2009 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1