İ have been trying to make application to reed data from serial(baudrate 9600) and write to a text box than write the same data to graph but it wont work. it either doesn’t do anything or the application froze until my RAM runs out and from what i can see moment i connect to my arduino(i checked with arduino İDE it sends the data correctly) my ram usage goes from 32-50 to 1gb and internet doesn’t have any solution i couldn’t even find the same problem
here’s my code
`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Security.Cryptography.X509Certificates;
using System.Data.Common;
using System.Windows.Forms.DataVisualization.Charting;
using System.Web.UI.DataVisualization.Charting;
namespace ArduinoSerial_Teknofest_
{
public partial class ArduinoSerial : Form
{
#region public veriables
public class Globals
{
public static Boolean Connected = false;
public static Boolean İfDisconnected = false;
}
/// To write information to console
public void WriteToConsole(string messege, string TextColor)
{
ConsoleLogs.SelectionColor = Color.FromName(TextColor);
ConsoleLogs.SelectedText += "/// " + messege + Environment.NewLine;
}
#endregion
public ArduinoSerial()
{
InitializeComponent();
// Get a list of serial port names.
PortsCB.Items.AddRange(SerialPort.GetPortNames());
//DataCharts.Series
}
private void ArduinoSerial_Load(object sender, EventArgs e)
{ /*
//Get the resulation of screen
System.Drawing.Rectangle ScreenResulation = Screen.PrimaryScreen.WorkingArea;
//Set size of form to 50% of screen size
this.Size = new System.Drawing.Size(Convert.ToInt32(0.5 * ScreenResulation.Width),Convert.ToInt32(0.5 * ScreenResulation.Height));
this.Location = new System.Drawing.Point(10, 10);
*/
}
public void Scan_click(object sender, EventArgs e)
{
// Get a list of serial port names.
string[] ports = SerialPort.GetPortNames();
PortsCB.Items.Clear();
PortsCB.Items.AddRange(ports);
WriteToConsole("Found PORTS = ", "Green");
foreach (string port in ports)
{
WriteToConsole(port, "Green");
}
}
public void ConnectButton_Click(object sender, EventArgs e)
{
string SelectedBaudRate = BaudRateCB.Text;
string SelectedPort = PortsCB.Text;
if (Device.IsOpen == false)
{
///Try to open the port if cant give an error
try
{
///Get user info
Device.BaudRate = int.Parse(SelectedBaudRate);
Device.PortName = SelectedPort;
Device.Open();
///Check if the port open, if it is disable user input bloccks
if (Device.IsOpen == true)
{
ConnectButton.Text = "Disconnect";
BaudRateCB.Enabled = false;
PortsCB.Enabled = false;
DataMenu.Enabled = true;
WriteToConsole("Connected", "green");
Device.Write("get/device/info");
}
}
catch (System.IO.IOException) { WriteToConsole("Something went wrong", "red"); }
///if post is used by other program output a error
catch (System.UnauthorizedAccessException)
{
WriteToConsole("port busy", "red");
Device.Close();
}
///if port is allready open output an error
catch (System.InvalidOperationException)
{
WriteToConsole("Port is already open", "red");
Device.Close();
}
catch (System.ArgumentNullException)
{
if (PortsCB.Text == "" && BaudRateCB.Text == "") { WriteToConsole("Please select a port and baudrate", "red"); }
else if (PortsCB.Text == "") { WriteToConsole("Please select a port", "red"); }
else if (BaudRateCB.Text == "") { WriteToConsole("Please select a baudrate", "red"); }
}
catch (System.ArgumentException)
{
if (PortsCB.Text == "" && BaudRateCB.Text == "") { WriteToConsole("Please select a port and baudrate", "red"); }
else if (PortsCB.Text == "") { WriteToConsole("Please select a port", "red"); }
else if (BaudRateCB.Text == "") { WriteToConsole("Please select a baudrate", "red"); }
}
catch (System.FormatException)
{
if (PortsCB.Text == "" && BaudRateCB.Text == "") { WriteToConsole("Please select a port and baudrate", "red"); }
else if (PortsCB.Text == "") { WriteToConsole("Please select a port", "red"); }
else if (BaudRateCB.Text == "") { WriteToConsole("Please select a baudrate", "red"); }
}
}
else if (Device.IsOpen == true)
{
WriteToConsole("Disconnected!!", "green");
Device.Close();
ConnectButton.Text = "Connect";
BaudRateCB.Enabled = true; ;
PortsCB.Enabled = true;
DataMenu.Enabled = false;
ConsoleLogs.Clear();
PositionGraph.Series["gyroX"].Points.Clear();
PositionGraph.Series["gyroY"].Points.Clear();
ChartCounter = 0;
}
}
string recivedData;
//List<double> time = new List<double>();
void Parachute_Status(string data)
{
int intData = int.Parse(data);
switch (intData)
{
case 1:
parachute1_Status.Text = "Not triggered";
parachute2_Status.Text = "Not triggered";
break;
case 2:
parachute1_Status.Text = "Triggered";
parachute2_Status.Text = "Not triggered";
break;
case 3:
parachute1_Status.Text = "Not triggered";
parachute2_Status.Text = "Triggered";
break;
case 4:
parachute1_Status.Text = "Triggered";
parachute2_Status.Text = "Triggered";
break;
}
}
private void dataRecived(object sender, SerialDataReceivedEventArgs e)
{
try
{
recivedData = Device.ReadLine();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "hata");
}
this.BeginInvoke(new EventHandler(ProcessData));
}
int ChartCounter;
private void gyroChart(string gyroDataX, string gyroDataY)
{
while (Device.IsOpen == true)
{
ChartCounter++;
PositionGraph.Series["gyroX"].Points.AddXY(double.Parse(gyroDataX), ChartCounter);
PositionGraph.Series["gyroY"].Points.AddXY(double.Parse(gyroDataY), ChartCounter);
}
}
private void ProcessData(object sender, EventArgs e)
{
try
{
string[] splitted_data = recivedData.Split('/');
//gyroChart(splitted_data[0], splitted_data[1]);
WriteToConsole(recivedData, "red");
//irtifa_Box.Text = splitted_data[0];
//GPS_Box.Text = splitted_data[1];
//Enlem_Box.Text = splitted_data[2];
//Boylam_Box.Text = splitted_data[3];
/*YGPS_Box.Text = splitted_data[4];
YEnlem_Box.Text = splitted_data[5];
YBoylam_Box.Text = splitted_data[6];
KGPS_Box.Text = splitted_data[7];
KEnlem_Box.Text = splitted_data[8];
KBoylam_Box.Text = splitted_data[9];
JiroskopX_Box.Text = splitted_data[0];
JiroskopY_Box .Text = splitted_data[1];
JiroskopZ_Box .Text = splitted_data[12];
gyroChart(JiroskopX_Box.Text, JiroskopY_Box.Text);
İvmeX_Box.Text = splitted_data[13];
İvmeY_Box.Text = splitted_data[14];
İvmeZ_Box.Text = splitted_data[15];
Aci_Box.Text = splitted_data[16];
CRC_Box.Text = splitted_data[17];
Parachute_Status(splitted_data[18]);
Heat_Box.Text = splitted_data[19];
*/
}
catch (System.IO.IOException)
{
WriteToConsole("error", "red");
return;
}
catch (System.IndexOutOfRangeException)
{
WriteToConsole("error", "red");
return;
}
catch (System.InvalidOperationException)
{
WriteToConsole("error", "red");
return;
}
} //Evde dock sistemlerine bak ve arayüz gelişimi için açık sayfalara bak
}
}
i cant give the error since i am Turkish and gives the main error in Turkish
i tried changing the baudrate but didnt work and since it was only idea i got stuck
user25017301 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.