INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

serial com port recieve data in textbox2 in more line problem

serial com port recieve data in textbox2 in more line problem

serial com port recieve data in textbox2 in more line problem

(OP)
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)
{

}

}
}

RE: serial com port recieve data in textbox2 in more line problem

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close