Sharing Serial Port across multiple Forms C# VB - c#

I have three Forms in my project. Form2 Connects the Serial Port. Form1 writes and reads data from the Serial Port. Form 3 only needs to write to the serial port, however when i send data to the serial port I get an "Port is closed error". I do not see any difference in the way i set up form 2 and form 3 so i am not sure why Visual Studios is giving me the "port closed" error.
Form 2 will connect to serial port
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports; // added this
namespace ECE_323_LAB_10
{
public partial class Bluetooth_Settings : Form
{
public SerialPort _serial = new SerialPort(); // added this
public Bluetooth_Settings()
{
InitializeComponent();
_serial.BaudRate = int.Parse(baud_rate.Text); // added this
foreach (string s in SerialPort.GetPortNames()) // added this
{
com_port.Items.Add(s);
}
}
private void connet_button_Click(object sender, EventArgs e)
{
try
{
_serial.PortName = com_port.SelectedItem.ToString();
_serial.BaudRate = Convert.ToInt32(baud_rate.SelectedItem);
_serial.Open();
this.Close();
Form1 _main = new Form1();
foreach (Form1 tmpform in Application.OpenForms)
{
if (tmpform.Name == "Form1")
{
_main = tmpform;
break;
}
}
_main.toolStripStatusLabel1.Text = " Connected: " + _serial.PortName.ToString();
_main.toolStripStatusLabel1.ForeColor = Color.Green;
_main.toolStripProgressBar1.Value = 100;
}
catch
{
MessageBox.Show("Please select COM Port/ Baud Rate");
}
}
}
}
Form1 can read and write data to serial port
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using ZedGraph;
using System.Text.RegularExpressions;
namespace ECE_323_LAB_10
{
public partial class Form1 : Form
{
PointPairList list = new PointPairList();
double temp;
int flag = 1;
double x = 0;
int init = 0;
int digit = 0;
double temp1;
private T_settings t_settings;
private Bluetooth_Settings _setting = new Bluetooth_Settings();
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
int text_length = 0;
text_length = richTextBox1.TextLength;
char send_ch = richTextBox1.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_setting._serial.Write("\r"); // sending carraige return
}
else
{
_setting._serial.Write(ch, 0, 1); // sending char to microcontroller
}
}
private void Toggle_Click(object sender, EventArgs e)
{
// 2 = F , 1 = C
char[] ch = new char[1];
ch[0] = 'D';
_setting._serial.Write(ch, 0, 1); // sending char to microcontroller
}
private void Settings_Click(object sender, EventArgs e)
{
t_settings = new T_settings();
t_settings.Show();
}
}
}
Form1 has no errors when i read and write to the serial port. I have left a few lines of codes out for readability.
Now here is the code for Form3, I think i have done setup exactly the same as Form1, however I am getting port is closed error.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports; // added this
namespace ECE_323_LAB_10
{
public partial class T_settings : Form
{
private Bluetooth_Settings _enter = new Bluetooth_Settings();
public T_settings()
{
InitializeComponent();
}
string Sampling_text;
private void Text_Sampling_TextChanged(object sender, EventArgs e)
{
Sampling_text = Text_Sampling.Text;
}
private void Send_Sampling_Click(object sender, EventArgs e)
{
int text_length = 0;
text_length = Sampling_text.Length;
char send_ch = Text_Sampling.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_enter._serial.Write("\r"); // sending carraige return
}
else
{
_enter._serial.Write(ch, 0, 1); // sending char to microcontroller
}
char[] enter = new char[1];
}
}
}
Can someone tell me what I need to add to Form3 so I do not get a port closed error. In my send sampling click method.

Ok, if you're totally new then concepts around OOP will take time to learn. Let me try and give you a solution to get you out of trouble.
private T_settings t_settings = null;
private Bluetooth_Settings _bsSettings = null;
public Form1()
{
InitializeComponent();
if (_bsSettings == null) _bsSettings = new Bluetooth_Settings();
_bsSettings.Show();
}
private void Form1_Shown(Object sender, EventArgs e) {
{
//Either here or in the constructor instantiate T_settings with a reference to the
//_bsSettings this way it will still be in scope, the same instance you've connected.
if (t_settings == null) t_settings = new T_settings(_bsSettings);
}
public partial class T_settings : Form
{
private Bluetooth_Settings _bsSettings = new Bluetooth_Settings();
public T_settings(Bluetooth_Settings bsSettings)
{
InitializeComponent();
//Pass a reference of the BS Settings class (one that's already connected)
this._bsSettings = bsSettings;
}
...
private void Send_Sampling_Click(object sender, EventArgs e)
{
int text_length = 0;
text_length = Sampling_text.Length;
char send_ch = Text_Sampling.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_bsSettings._serial.Write("\r"); // sending carraige return
}
else
{
_bsSettings._serial.Write(ch, 0, 1); // sending char to microcontroller
}
}

Related

System.StackOverflowException when launching application

When I launch my application it gives me an error at " Form1 Test = new Form1();" in my class. Here is my code. I want to use labels from my form so therefore I used "form1 test".
I made a class so I can call my methods from it in my Mainform as I need to code my application with classes. When I launched the application for the first time it worked, but then after trying again it didn't work anymore.
Main form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tester
{
public partial class Form1 : Form
{
Zombie zombie = new Zombie();
int levens = 3;
public Form1()
{
InitializeComponent();
test1.Text = "Levens: " + "" + levens;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Zombie()
{
foreach (Control control in Controls)
{
PictureBox pic = control as PictureBox;
if (pic != null)
{
pic.Top += 1;
if (pic.Top > 600 && pic.Visible == true)
{
pic.Top = 0;
test1.Text = $"Levens: {--levens}";
}
else if (pic.Top > 600 && pic.Visible == false)
{
pic.Visible = true;
pic.Top = 0;
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
zombie.MakeZombie(5, this);
}
}
}
Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tester
{
class Zombie
{
Random random = new Random();
Form1 Test = new Form1();
private int score = 0;
public void MakeZombie(int aantal, Form formInstance)
{
for (int i = 0; i < aantal; i++)
{
PictureBox picture = new PictureBox();
picture.Image = Properties.Resources.ZombieDik;
picture.Size = new Size(200, 200);
picture.Location = new Point(random.Next(1500), 0);
picture.SizeMode = PictureBoxSizeMode.Zoom;
picture.Click += zombie_Click;
picture.BackColor = Color.Transparent;
formInstance.Controls.Add(picture);
}
}
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
pic.Visible = false;
score++;
Test.label2.Text = $"Score: {score}";
Test.Controls.Remove(pic);
pic.Dispose();
}
}
}
In zombie_Click() you can get a reference to the Form from the sender itself:
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
Form1 f1 = pic.FindForm() as Form1;
score++;
f1.label2.Text = $"Score: {score}";
pic.Dispose();
}

Wireless Serial Communication

I have created this Arduino sketch that communicates with my pc through the Console library and using the serial monitor over my network. Essentially, when I enter the digit 1, my Arduino stepper motor moves 30 steps etc. However, it is inconvenient to have to type in the serial monitor. I was hoping anyone would know how I can create an executable windows form or something that can connect over my network, to the Arduino Yun console.
#include <Stepper.h>
#include <Console.h>
char incomingByte;
Stepper stepper(64,8,9,10,11);
int stepCount = 1;
void setup() {
Bridge.begin();
Console.begin();
while (!Console);
stepper.setSpeed(60);
}
void loop() {
if (Console.available() > 0)
{
incomingByte = Console.read();
if (incomingByte == '1')
{
stepCount += 30;
if (stepCount > 0 && stepCount < 4096)
{
stepper.step(30);
Console.println(stepCount);
}
else{stepCount = stepCount - 30;}
}
if (incomingByte == '2')
{
stepCount = stepCount - 30;
if (stepCount > 0 && stepCount < 4096)
{
stepper.step(-30);
Console.println(stepCount);
}
else{stepCount += 30;}
}
if (incomingByte == '3')
{
stepCount += 200;
if (stepCount > 0 && stepCount < 4096)
{
stepper.step(200);
Console.println(stepCount);
}
else{stepCount = stepCount - 200;}
}
if (incomingByte == '4')
{
stepCount = stepCount - 200;
if (stepCount > 0 && stepCount < 4096)
{
stepper.step(-200);
Console.println(stepCount);
}
else{stepCount += 200;}
}
}
}
Here is the windows form app I was hoping would work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace Remote_Focuser
{
public partial class Form1 : Form
{
private SerialPort myPort;
public Form1()
{
Console.Read();
InitializeComponent();
Init();
}
private void Fwd_30_Button_Click(object sender, EventArgs e)
{
myPort.WriteLine("1");
}
private void Backward_30_Button_Click(object sender, EventArgs e)
{
myPort.WriteLine("2");
}
private void Forward_200_Button_Click(object sender, EventArgs e)
{
myPort.WriteLine("3");
}
private void Backward_200_Button_Click(object sender, EventArgs e)
{
myPort.WriteLine("4");
}
private void Init()
{
myPort = new SerialPort();
myPort.BaudRate = 9600;
myPort.PortName = "10.1.1.211";
myPort.Open();
}
}
}
Thanks heaps in advance, and yes I have probably made a big mistake, I am not too proficient yet... :)

Try and catch, always catching exception

what i am attempting to do is, get user input from a text box, convert it to an int and then use that. i got everything to work except the the try and catch. incase the person puts a letter instead of a number. with the code below it always catches something. i have no idea what is catches something. i've taken out the bool test and if i put in a letter it will just throw the exception then go to the beeping. other then waiting for a valid input.
please excuse my messy code, i am still a beginner c# programmer :D thanks in advanced!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
bool tone = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool test = true;
speedInput.Clear();
beep.Clear();
int beepspeed = 90;
int speed = 100;
string speedtext = this.speedInput.Text;
string beeptext = this.beep.Text;
try
{
test = true;
beepspeed = Convert.ToInt32(beeptext);
speed = Convert.ToInt32(speedtext);
}
catch (Exception)
{
MessageBox.Show("numbers above 37 only!!");
test = false;
}
if (test)
{
for (int i = 0; i < beepspeed; i++)
{
if (this.tone)
{
Random ran = new Random();
int rand = ran.Next(400, 3000);
Console.Beep(rand, speed);
}
else
{
Console.Beep(1000, speed);
}
}
}
}
private void radioButtonYes_CheckedChanged(object sender, EventArgs e)
{
this.tone = true;
}
private void radioButtonNo_CheckedChanged(object sender, EventArgs e)
{
this.tone = false;
}
}
}
You are cleaning the content of the inputs at the beginning of the button1_click
speedInput.Clear();
beep.Clear();
Then when you try to convert empty string to int32 it fails
beepspeed = Convert.ToInt32(beeptext);
speed = Convert.ToInt32(speedtext);

Ping over a period of Time

I have a windows form application that basically pings an ip and then returns an image with a tooltip that displays the rtt to that ip.
What i want to do is have the the form ping that ip every 20 seconds, so that the form and images change. If i could get that to work then I would like to some how store maybe 4 rtt's and then show an average of the 4 in the tooltip.
So far the form is only pinging once, I've played around with a timer but I don't really know what I am doing. Here is my code so far.
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.Net.NetworkInformation;
using System.ServiceProcess;
using System.Threading;
using System.ComponentModel;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
Ping pingClass = new Ping();
PingReply pingReply = pingClass.Send("10.209.123.123");
label4.Text = (pingReply.RoundtripTime.ToString());
//+ "ms");
label5.Text = (pingReply.Status.ToString());
if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
this.pictureBox1.Load("greenLAT.png");
if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
this.pictureBox1.Load("yellowLAT.png");
if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
this.pictureBox1.Load("redLAT.png");
ToolTip tt = new ToolTip();
tt.SetToolTip(this.pictureBox1, "Your current network delay is " + label4.Text + "ms");
timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//MessageBox.Show("Timeout!");
Refresh();
}
}
}
Try this:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Net;
using System.Windows.Forms;
using System.Net.NetworkInformation;
namespace DXWindowsApplication4
{
public partial class Form2 : Form
{
private readonly Timer _timer;
private readonly Ping _pingClass;
private readonly IPAddress _ipAddress;
private readonly int _timeout;
private Image _greenImage;
private Image _yellowImage;
private Image _redImage;
private int _pingCount;
private long _avgRtt;
public Form2()
{
InitializeComponent();
IPAddress.TryParse("98.138.253.109", out _ipAddress); // yahoo.com Ip address
_timer = new Timer();
_timeout = 3000;
_pingClass = new Ping();
_pingClass.PingCompleted += PingClassPingCompleted;
}
void PingClassPingCompleted(object sender, PingCompletedEventArgs e)
{
RefreshPing(e.Reply);
}
public void FormLoad(object sender, EventArgs e)
{
_timer.Tick += TimerTick;
_timer.Interval = 4000;
_timer.Start();
}
private void TimerTick(object sender, EventArgs e)
{
_pingClass.SendAsync(_ipAddress, _timeout);
}
private void RefreshPing(PingReply pingReply)
{
label4.Text = (pingReply.RoundtripTime.ToString(CultureInfo.InstalledUICulture));
label5.Text = (pingReply.Status.ToString());
_avgRtt = (_avgRtt * _pingCount++ + pingReply.RoundtripTime)/_pingCount;
if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
{
SetImage(pictureBox1, Images.Green);
}
if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
{
SetImage(pictureBox1, Images.Yellow);
}
if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
{
SetImage(pictureBox1, Images.Red);
}
ToolTip tt = new ToolTip();
tt.SetToolTip(pictureBox1, "Your average network delay is " + _avgRtt + "ms");
Refresh();
}
private void SetImage(PictureBox pBox, Images images)
{
switch (images)
{
case Images.Green:
if (_greenImage == null)
{
_greenImage = new Bitmap("greenImage.png");
}
pictureBox1.Image = _greenImage;
break;
case Images.Yellow:
if (_greenImage == null)
{
_yellowImage = new Bitmap("yellowImage.png");
}
pictureBox1.Image = _yellowImage;
break;
case Images.Red:
if (_redImage == null)
{
_redImage = new Bitmap("redImage.png");
}
pictureBox1.Image = _greenImage;
break;
default:
throw new InvalidEnumArgumentException("invalid enum name");
}
}
}
internal enum Images
{
Green,
Yellow,
Red
}
}

Caller ID Check if Caller has ended Call

I have a program that gets the incoming number, date and time. I want to check however if the person who is ringing me has put the phone down, how can I do this?
Below is the code which I currently have:
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;
namespace CallerID
{
public partial class CallerID : Form
{
int timesTicked = 0;
Point defaultLocation = new Point();
Point newLocation = new Point();
public CallerID()
{
InitializeComponent();
port.Open();
SetModem(); // SetModem(); originally went after WatchModem();
WatchModem();
//SetModem();
telephoneTimer.Interval = 16;
telephoneTimer.Tick += new EventHandler(telephoneTimer_Tick);
defaultLocation = pictureBox1.Location;
newLocation = pictureBox1.Location;
}
void telephoneTimer_Tick(object sender, EventArgs e)
{
if (timesTicked <= 2)
newLocation.X++;
if (timesTicked >= 4)
newLocation.X--;
if (timesTicked == 6)
{
timesTicked = 0;
pictureBox1.Location = defaultLocation;
newLocation = defaultLocation;
}
pictureBox1.Location = newLocation;
timesTicked++;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
WatchModem();
}
private SerialPort port = new SerialPort("COM3");
string CallName;
string CallNumber;
string ReadData;
private void SetModem()
{
port.WriteLine("AT+VCID=1\n");
//port.WriteLine("AT+VCID=1");
port.RtsEnable = true;
}
private void WatchModem()
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
public delegate void SetCallerIdText();
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
ReadData = port.ReadExisting();
//Add code to split up/decode the incoming data
//if (lblCallerIDTitle.InvokeRequired)
if (ReadData.Contains("NMBR"))
{
lblData.Invoke(new SetCallerIdText(() => lblData.Text = ReadData));
}
//else
// lblCallerIDTitle.Text = ReadData;
}
private void lblData_TextChanged(object sender, EventArgs e)
{
telephoneTimer.Start();
button1.Visible = true;
}
}
}
Please ignore the Timer Code as that is just for animation.
Have you tried the PinChanged event? Normally Carrier Detect will go low when the remote end disconnects.

Categories