Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

serial com port recieve data in textbox2 in more line problem

Status
Not open for further replies.

ahm1985

Programmer
Dec 6, 2012
138
0
0
EG
in code below it work good but i face problem i need to recieve textbox2 in one line not more line how i do that


using System;
using System.IO.Ports;
using System.Windows.Forms;

namespace CommSample
{
public partial class Form1 : Form
{
/// <summary>
/// Initializes a new instance of the <see cref="Form1"/> class.
/// </summary>
public Form1()
{
InitializeComponent();
}

/// <summary>
/// Handles the Idle event of the Application control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
void Application_Idle(object sender, EventArgs e)
{
label3.Text = serialPort1.IsOpen ? "[Open]" : "[Closed]";
}

/// <summary>
/// Handles the Click event of the button1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button1_Click(object sender, EventArgs e)
{

}

/// <summary>
///
/// </summary>
private void TransmitCommand()
{
if (textBox1.Text.Length > 0)
{
if (serialPort1.IsOpen)
{
serialPort1.Write(textBox1.Text + "\r");
}
}
}

/// <summary>
/// Closes the port.
/// </summary>
private void ClosePort()
{
if (serialPort1.IsOpen)
{
serialPort1.DataReceived -= new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.Close();
}
}

/// <summary>
/// Handles the Click event of the closePortToolStripMenuItem control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void closePortToolStripMenuItem_Click(object sender, EventArgs e)
{
ClosePort();
}

/// <summary>
/// Handles the Click event of the exitToolStripMenuItem control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}

/// <summary>
/// Handles the Load event of the Form1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.PortName = Properties.Settings.Default.Port;
serialPort1.BaudRate = Properties.Settings.Default.Baud;
serialPort1.DataBits = Properties.Settings.Default.DataBits;
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), Properties.Settings.Default.Parity);
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Properties.Settings.Default.StopBits);

Application.Idle += new EventHandler(Application_Idle);
}

/// <summary>
/// Opens the port.
/// </summary>
private void OpenPort()
{
serialPort1.Open();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
}

/// <summary>
/// Handles the Click event of the openPortToolStripMenuItem control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenPort();
}

/// <summary>
/// Handles the Click event of the optionsToolStripMenuItem control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
ClosePort();

using (Form2 form = new Form2())
{
if (form.ShowDialog(this) == DialogResult.OK)
{
serialPort1.PortName = Properties.Settings.Default.Port;
serialPort1.BaudRate = Properties.Settings.Default.Baud;
serialPort1.DataBits = Properties.Settings.Default.DataBits;
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), Properties.Settings.Default.Parity);
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Properties.Settings.Default.StopBits);
}
}
}

/// <summary>
/// Handles the DataReceived event of the serialPort1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.IO.Ports.SerialDataReceivedEventArgs"/> instance containing the event data.</param>
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!InvokeRequired)
{
if (e.EventType == SerialData.Chars)
{
string portData = serialPort1.ReadLine();
textBox2.Text =portData;
}
}
else
{
SerialDataReceivedEventHandler invoker = new SerialDataReceivedEventHandler(serialPort1_DataReceived);
BeginInvoke(invoker, new object[] { sender, e });
}
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pollingCheckbox_CheckedChanged(object sender, EventArgs e)
{
if (timer1.Enabled && !pollingCheckbox.Checked)
{
timer1.Enabled = false;
}
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
if (pollingCheckbox.Checked)
{
TransmitCommand();
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}

}
}
 
my problem here in this lines
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!InvokeRequired)
{
if (e.EventType == SerialData.Chars)
{
string portData = serialPort1.ReadLine();
textBox2.Text =portData;
}
}
else
{
SerialDataReceivedEventHandler invoker = new SerialDataReceivedEventHandler(serialPort1_DataReceived);
BeginInvoke(invoker, new object[] { sender, e });
}
}

I wnat when recieve data recieve in one line in text box then convert to integer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top