guys i have make some other post before few months! i cand find solution please help me if someone know any solution!!
im reading with serial port from a micro controller board and i have a
problem!
the program works OK but the memory usage increasing! begins
with 13mb and goes to 100mb in an hour
i have try to use
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
in the
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadLine();
this.Invoke(new EventHandler(DisplayText));
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
but still nothing!! any idea?
Loading
Ryan AlfordPosted Dec 2, 2008, 9:07 AM
Bechir BejaouiPosted Dec 1, 2008, 5:03 AM
try{
//code
}
catch(Exception caught){}
georgePosted Nov 30, 2008, 2:13 PM
label1.Invoke(tickerDelegate1, new object[] { _serialPort.ReadLine() });
the error input string is not in correct format! but not always!!
is any way to avoid this?
thanks
Bechir BejaouiPosted Nov 30, 2008, 10:22 AM
Ryan AlfordPosted Nov 29, 2008, 12:48 PM
Bechir BejaouiPosted Nov 28, 2008, 7:14 PM
Ryan AlfordPosted Nov 27, 2008, 11:01 AM
Here is a better and easier way of communicating to a serial port:
=============================================
public partial class Form1 : Form
{
SerialPort _serialPort;
// delegate is used to write to a UI control from a non-UI thread
private delegate void SetTextDeleg(string text);
private void Form1_Load(object sender, EventArgs e)
{
_serialPort = new SerialPort("COM5", 19200, Parity.None, 8, StopBits.One, Handshake.None);
_serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(500);
string data = _serialPort.ReadLine();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
}
private void si_DataReceived(string data)
{
textBox1.Text = data.Trim();
}
}
=============================================
1. There is need to create string objects for the SerialPort's settings. The values can be passed to the constructor of the SerialPort class
2. There is absolutely no need explicitly create a new Thread, since the SerialDataReceivedEventHandler occurs on a different thread than the UI thread.
Bechir BejaouiPosted Nov 26, 2008, 4:18 PM
georgePosted Nov 25, 2008, 12:23 PM
//////////////////////////// console mode working
using System;
using System.IO.Ports;
using System.Threading;
public class PortChat
{
static bool _continue;
static SerialPort _serialPort;
public static void Main()
{
string name;
string message;
string portName;
string baudRate;
string parity;
string dataBits;
string stopBits;
string handshake;
portName = "COM5";
baudRate = "19200";
parity = "None";
dataBits = "8";
stopBits = "One";
handshake = "None";
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = portName;
_serialPort.BaudRate = Convert.ToInt16(baudRate);
_serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
_serialPort.DataBits = int.Parse(dataBits);
_serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
_serialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), handshake);
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
_serialPort.WriteLine(
String.Format("<{0}>: {1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
}
public static void Read()
{
while (_continue)
{
try
{
string message = _serialPort.ReadLine();
Console.WriteLine(message);
}
catch (TimeoutException) { }
}
}
}
///////////////////////////////win32 form problem with te separate thread
//////// i cant see any data in textbox1 if i don't add the Serial(); inside public Form1() but the form loads normally without an error
//////// and if i add Serial(); inside public Form1 as below!!! the form never loads but the serial communication begins and works but i get no errors
/// i believe is the thread but i cant find a way
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;
using System.Threading;
namespace test_pci
{
public partial class Form1 : Form
{
public bool _continue;
public SerialPort _serialPort;
public string name;
public string message;
public string portName;
public string baudRate;
public string parity;
public string dataBits;
public string stopBits;
public string handshake;
public Form1()
{
InitializeComponent();
portName = "COM5";
baudRate = "19200";
parity = "None";
dataBits = "8";
stopBits = "One";
handshake = "None";
Serial();
}
public void Serial()
{
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = portName;
_serialPort.BaudRate = Convert.ToInt16(baudRate);
_serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
_serialPort.DataBits = int.Parse(dataBits);
_serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
_serialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), handshake);
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
//Console.WriteLine("Type QUIT to exit");
while (_continue)
{
//message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
_serialPort.WriteLine(
String.Format("<{0}>: {1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Read()
{
while (_continue)
{
try
{
//string message = _serialPort.ReadLine();
//Console.WriteLine(message);
textBox1.Text = _serialPort.ReadLine();
}
catch (TimeoutException) { }
}
}
}
}
Ryan AlfordPosted Nov 25, 2008, 11:07 AM
Bechir BejaouiPosted Nov 24, 2008, 7:09 PM
I can't guarantee that could solve your problem but you can always test it