using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;
namespace BalloonSat
{
public partial class Form1 : Form
{
public SerialPort sp;
string dataReceived = string.Empty;
private delegate void SetTextDeleg(string text);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Instantiates a new Serial port and declares the speed at 57,600 bits per second
sp = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
this.sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
// Open the Serial Port
sp.Open();
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
Thread.Sleep(100);
// Read data from serial until it finds a carriage return
string x = sp.ReadLine();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { x });
}
catch
{
}
}
private void si_DataReceived(string data)
{
dataReceived = data.Trim();
serialDataTextBox.Text = dataReceived;
}
}
}
39 Replies - 3020 Views - Last Post: 26 August 2011 - 08:36 AM
#1
Read Data From Serial and Display in a Textbox
Posted 21 August 2011 - 09:28 PM
Replies To: Read Data From Serial and Display in a Textbox
#2
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 05:09 AM
#3
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 08:38 AM
#4
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 08:46 AM
#5
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 08:53 AM
#6
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 09:01 AM
#7
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 09:09 AM
#8
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 09:43 AM
#9
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 10:10 AM
this.sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
Thread.Sleep(100);
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { x });
dataReceived = data.Trim();
#10
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 10:17 AM
So with the third breakpoint at the BeginInvoke line, does x have a value?
#11
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 10:32 AM
Here's an actual example of its use:
http://msdn.microsof...tareceived.aspx
Note that he's using ReadExisting, and he's not sleeping the thread or anything like that. You don't need to sleep the thread here, the method wouldn't be called if there wasn't already data waiting for you.
Also, are you sure you even need to Invoke to update the text box? You're not spawning new threads, so unless the SerialPort class does that behind the scenes, it's unnecessary. And even if it is necessary, you're over-complicating it significantly.You could replace your delegate, your method, and your BeginInvoke call with something like this:
Invoke(new Action(() => serialDataTextBox.Text = x.Trim()));
#12
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 10:43 AM
Did you mean like this?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;
namespace BalloonSat
{
public partial class Form1 : Form
{
public SerialPort sp;
string dataReceived = string.Empty;
private delegate void SetTextDeleg(string text);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Instantiates a new Serial port and declares the speed at 57,600 bits per second
sp = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
this.sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
// Open the Serial Port
sp.Open();
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
// Read data from serial
string data = sp.ReadExisting();
Invoke(new Action(() => serialDataTextBox.Text = data.Trim()));
}
catch
{
}
}
}
}
#13
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 10:56 AM
Other than that, I'd suggest you not suppress exceptions. You'd never know if something went wrong in your reading code, because you've got an empty catch clause. For debugging purposes, either remove the try/catch completely, or do something like this:
catch (Exception ex){
//production error handling goes here
#if DEBUG
//throw the exception so the debugger can catch it for you,
//but only if we're in debug mode.
throw ex;
#endif
}
But better if you implement some kind of handling.
#14
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 11:19 AM
Using the generic type 'System.Action<T>' requires 1 type arguments
I looked it up and it says it takes the parameter of the method that it encapsulates. So what does that mean?
#15
Re: Read Data From Serial and Display in a Textbox
Posted 22 August 2011 - 11:57 AM
Instead, I would use the MethodInvoker delegate (which has been around since .NET 1.1, and is faster to invoke than Action):
this.Invoke(new MethodInvoker(() => serialDataTextBox.Text = data.Trim());
This post has been edited by CodingSup3rnatur@l-360: 22 August 2011 - 11:59 AM
|
|

New Topic/Question
Reply




MultiQuote




|