EDN Admin
Well-known member
Hi everyone! Thanks for taking time to look at my question. Before I begin, I like to state that my experience with C# programming is pretty minimal and Im actually doing a project, which is enhancing on the current C# project of a radar. I have 3 main
forms which I use commonly and a separate piece of hardware which uses assembly language. The 3 forms are frmMain.CS which contains my main program of a radar and it opens and close a serial port so that the values from my hardware can be reflected on my radar.
The 2nd form is the radar.CS which is actually the heart of the project and it contains different methods which I am having trouble calling. The 3rd form is my selectionform.CS which is completely new and part of the enhancement. <br/>
Heres a block diagram to help you all visualize my problem if it helps.<br/>
Launch ---> FrmMain.CS --> (If hardware detects movement, send signal to appear on radar in frmMain.cs + launch selectionform.cs)
<br/>
If ally button of selectionform.CS is pressed, selectionform will hide and return back to frmMain.cs<br/>
If enemy button of selectionform.CS is pressed, selectionform will send e-mail and change the radar item(signal from hardware) to red colour. <br/>
<br/>
Please have a look at my codes and help me see whats wrong.<br/>
<br/>
//selectionform.CS<br/>
using System;<br/>
<br/>
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.Mail;
using System.Net.Mime;
using System.IO;
using System.Text.RegularExpressions;
using System.IO.Ports;
namespace Andy
{
public partial class selection : Form
{
Radar _radar; //Global variables for using RADAR ITEMS
RadarItem item1, item2;
StreamReader reader;
StreamWriter writer;
string RxString;
int radar_angle;
int elvation;
public selection()
{
InitializeComponent();
}
class Email
{
public static void Email1(string[] args)
{
MailMessage m1 = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
m1.From = new MailAddress("***********@gmail.com", "**************");
m1.To.Add(new MailAddress("***********@gmail.com", "*************"));
m1.CC.Add(new MailAddress("CC@domain.com", "Display name CC"));
m1.Subject = "Test";
m1.Body = "Code 232. Defense breached. Deploy unit now.";
FileStream fs = new FileStream("C:\Users\Lenovo\Desktop\WebcamViewer V1.0\Integrated Camera\2012-02-03_03-22-00-PM.JPEG",
FileMode.Open, FileAccess.Read);
Attachment a1 = new Attachment(fs, "Picture1.jpeg",
MediaTypeNames.Application.Octet);
m1.Attachments.Add(a1);
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new
System.Net.NetworkCredential("ânpapddef@gmail.com","yeekanleong");
sc.EnableSsl = true;
sc.Send(m1);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void enemyBtn_Click(object sender, EventArgs e)
{
try
{
_radar.RemoveItem(item1); // (PROBLEM HERE)
item2 = new SquareRadarItem(1, 8, (_radar.Angle() - 180), elvation); (PROBLEM HERE)
_radar.AddItem(item2); //(PROBLEM HERE)
MailMessage mail = new MailMessage(); // (No problem with email)
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("***********@gmail.com");
mail.To.Add("****************@gmail.com");
mail.Subject = "CODE 232";
mail.Body = "Code 232. Deploy unit immediately.";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("**********", "***********"); //username,password
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Command Sent!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
this.Hide();
}
}
}
<br/>
//frmMain.CS<br/>
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace Andy
{
public partial class frmMain : Form
{
Radar _radar;
RadarItem item1,item2;
StreamReader reader;
StreamWriter writer;
string RxString;
int radar_angle;
int elvation;
Process myProc;
public frmMain()
{
InitializeComponent();
// internal item update timer
}
private void frmMain_Load(object sender, EventArgs e) //for loading the radar
{
reader = new StreamReader("file.txt");
_radar = new Radar(pictureBox1.Width);
pictureBox1.Image = _radar.Image;
_radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
_radar.DrawScanInterval = 1;
_radar.DrawScanLine = true;
_radar.PauseScanLine = true;
_radar.SetAZ = int.Parse(reader.ReadLine());
portNameComboBox.DataSource = SerialPort.GetPortNames();
btnStop.Enabled = false;
}
void _radar_ImageUpdate(object sender, ImageUpdateEventArgs e) //for loading the radar
{
// this event is important to catch!
pictureBox1.Image = e.Image;
}
private void btnStart_Click(object sender, EventArgs e) //what happens when the connect button is pressed
{
serialPort1.PortName = portNameComboBox.Text.ToString();
serialPort1.BaudRate = 115200;
serialPort1.Open(); //opening the serial port
if (serialPort1.IsOpen)
{
while (btnStart.Enabled == true)
{
btnStart.Text = "Connected";
btnStart.Enabled = false;
btnStop.Text = "Disconnect";
btnStop.Enabled = true;
}
serialPort1.WriteLine("S");
_radar.PauseScanLine = false;
}
myProc = Process.Start("C:\Program Files (x86)\Logitech\LWS\Webcam Software\Launcher_Main.exe"); //Start the external windows application
}
private void label(object sender, EventArgs e) //function for displaying the object info(distance etc)
{
distance_label.Text = "Intruder Aircraft are detected at " + (radar_angle).ToString() + " degree" + " and is " + Decimal.Parse(RxString) / 1000 + " meter away!";
if (int.Parse(RxString) > 1000)
{
distance_label.Text = "Radar is scanning...";
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) //the info received and the calculations
{
radar_angle = _radar.Angle() - 90;
if (radar_angle < 0)
{
radar_angle = 360 + radar_angle;
}
if (serialPort1.IsOpen)
{
try
{
RxString = serialPort1.ReadTo("mm");
if (int.Parse(RxString) < 1000)
{
_radar.PauseScanLine = true;
this.Invoke(new EventHandler(label));
elvation = (int.Parse(RxString) / 11) + 90;
item1 = new CircleRadarItem(1, 8, (_radar.Angle() - 180), elvation); //(id,size,az,el)
_radar.AddItem(item1);
item2 = new SquareRadarItem(1, 8, (_radar.Angle() - 180), elvation); //(id,size,az,el) NEW ADDITION
selection form = new selection(); //NEW ADDITION Opening up the Friend / Foe Menu Window when aircraft approach
form.ShowDialog();//
}
else if (int.Parse(RxString) > 1000)
{
this.Invoke(new EventHandler(label));
_radar.PauseScanLine = false;
_radar.RemoveItem(item1);
_radar.RemoveItem(item2);
}
}
catch
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) // the function for closing the program, so that the thing dont close with "not responding"
{
reader.Close();
writer = new StreamWriter("file.txt");
writer.WriteLine((radar_angle + 90).ToString());
writer.Close();
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("E");
}
serialPort1.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void distance_label_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
myProc.Kill();
if (serialPort1.IsOpen)
serialPort1.Close();
else
{
MessageBox.Show("Warning! Serial Port is not opened!");
}
login form = new login();
this.Hide();
form.ShowDialog();
this.Close();
}
private void portNameComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btnStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
while (btnStop.Enabled == true)
{
btnStop.Text = "Disconnected";
btnStop.Enabled = false;
btnStart.Text = "Connect";
btnStart.Enabled = true;
serialPort1.WriteLine("T");
_radar.PauseScanLine = true;
serialPort1.Close();
}
}
myProc.Kill(); // Kills the image capture program when launched with button start
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
}
}
<br/>
// The radar.CS is actually a class which contains methods such as RemoveItem(); If you understood my codes, you would have realized in frmMain.CS, I could actually use the Radar.CS library successfully.<br/>
The problem I get is object instance not set to the instance of an object when I click the enemy button and I expect the application to turn the item sent by the hardware on the radar from green to red, and to send an email.<br/>
Please help me. Many thanks!
View the full article
forms which I use commonly and a separate piece of hardware which uses assembly language. The 3 forms are frmMain.CS which contains my main program of a radar and it opens and close a serial port so that the values from my hardware can be reflected on my radar.
The 2nd form is the radar.CS which is actually the heart of the project and it contains different methods which I am having trouble calling. The 3rd form is my selectionform.CS which is completely new and part of the enhancement. <br/>
Heres a block diagram to help you all visualize my problem if it helps.<br/>
Launch ---> FrmMain.CS --> (If hardware detects movement, send signal to appear on radar in frmMain.cs + launch selectionform.cs)
<br/>
If ally button of selectionform.CS is pressed, selectionform will hide and return back to frmMain.cs<br/>
If enemy button of selectionform.CS is pressed, selectionform will send e-mail and change the radar item(signal from hardware) to red colour. <br/>
<br/>
Please have a look at my codes and help me see whats wrong.<br/>
<br/>
//selectionform.CS<br/>
using System;<br/>
<br/>
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.Mail;
using System.Net.Mime;
using System.IO;
using System.Text.RegularExpressions;
using System.IO.Ports;
namespace Andy
{
public partial class selection : Form
{
Radar _radar; //Global variables for using RADAR ITEMS
RadarItem item1, item2;
StreamReader reader;
StreamWriter writer;
string RxString;
int radar_angle;
int elvation;
public selection()
{
InitializeComponent();
}
class Email
{
public static void Email1(string[] args)
{
MailMessage m1 = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
m1.From = new MailAddress("***********@gmail.com", "**************");
m1.To.Add(new MailAddress("***********@gmail.com", "*************"));
m1.CC.Add(new MailAddress("CC@domain.com", "Display name CC"));
m1.Subject = "Test";
m1.Body = "Code 232. Defense breached. Deploy unit now.";
FileStream fs = new FileStream("C:\Users\Lenovo\Desktop\WebcamViewer V1.0\Integrated Camera\2012-02-03_03-22-00-PM.JPEG",
FileMode.Open, FileAccess.Read);
Attachment a1 = new Attachment(fs, "Picture1.jpeg",
MediaTypeNames.Application.Octet);
m1.Attachments.Add(a1);
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new
System.Net.NetworkCredential("ânpapddef@gmail.com","yeekanleong");
sc.EnableSsl = true;
sc.Send(m1);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void enemyBtn_Click(object sender, EventArgs e)
{
try
{
_radar.RemoveItem(item1); // (PROBLEM HERE)
item2 = new SquareRadarItem(1, 8, (_radar.Angle() - 180), elvation); (PROBLEM HERE)
_radar.AddItem(item2); //(PROBLEM HERE)
MailMessage mail = new MailMessage(); // (No problem with email)
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("***********@gmail.com");
mail.To.Add("****************@gmail.com");
mail.Subject = "CODE 232";
mail.Body = "Code 232. Deploy unit immediately.";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("**********", "***********"); //username,password
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Command Sent!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
this.Hide();
}
}
}
<br/>
//frmMain.CS<br/>
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace Andy
{
public partial class frmMain : Form
{
Radar _radar;
RadarItem item1,item2;
StreamReader reader;
StreamWriter writer;
string RxString;
int radar_angle;
int elvation;
Process myProc;
public frmMain()
{
InitializeComponent();
// internal item update timer
}
private void frmMain_Load(object sender, EventArgs e) //for loading the radar
{
reader = new StreamReader("file.txt");
_radar = new Radar(pictureBox1.Width);
pictureBox1.Image = _radar.Image;
_radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
_radar.DrawScanInterval = 1;
_radar.DrawScanLine = true;
_radar.PauseScanLine = true;
_radar.SetAZ = int.Parse(reader.ReadLine());
portNameComboBox.DataSource = SerialPort.GetPortNames();
btnStop.Enabled = false;
}
void _radar_ImageUpdate(object sender, ImageUpdateEventArgs e) //for loading the radar
{
// this event is important to catch!
pictureBox1.Image = e.Image;
}
private void btnStart_Click(object sender, EventArgs e) //what happens when the connect button is pressed
{
serialPort1.PortName = portNameComboBox.Text.ToString();
serialPort1.BaudRate = 115200;
serialPort1.Open(); //opening the serial port
if (serialPort1.IsOpen)
{
while (btnStart.Enabled == true)
{
btnStart.Text = "Connected";
btnStart.Enabled = false;
btnStop.Text = "Disconnect";
btnStop.Enabled = true;
}
serialPort1.WriteLine("S");
_radar.PauseScanLine = false;
}
myProc = Process.Start("C:\Program Files (x86)\Logitech\LWS\Webcam Software\Launcher_Main.exe"); //Start the external windows application
}
private void label(object sender, EventArgs e) //function for displaying the object info(distance etc)
{
distance_label.Text = "Intruder Aircraft are detected at " + (radar_angle).ToString() + " degree" + " and is " + Decimal.Parse(RxString) / 1000 + " meter away!";
if (int.Parse(RxString) > 1000)
{
distance_label.Text = "Radar is scanning...";
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) //the info received and the calculations
{
radar_angle = _radar.Angle() - 90;
if (radar_angle < 0)
{
radar_angle = 360 + radar_angle;
}
if (serialPort1.IsOpen)
{
try
{
RxString = serialPort1.ReadTo("mm");
if (int.Parse(RxString) < 1000)
{
_radar.PauseScanLine = true;
this.Invoke(new EventHandler(label));
elvation = (int.Parse(RxString) / 11) + 90;
item1 = new CircleRadarItem(1, 8, (_radar.Angle() - 180), elvation); //(id,size,az,el)
_radar.AddItem(item1);
item2 = new SquareRadarItem(1, 8, (_radar.Angle() - 180), elvation); //(id,size,az,el) NEW ADDITION
selection form = new selection(); //NEW ADDITION Opening up the Friend / Foe Menu Window when aircraft approach
form.ShowDialog();//
}
else if (int.Parse(RxString) > 1000)
{
this.Invoke(new EventHandler(label));
_radar.PauseScanLine = false;
_radar.RemoveItem(item1);
_radar.RemoveItem(item2);
}
}
catch
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) // the function for closing the program, so that the thing dont close with "not responding"
{
reader.Close();
writer = new StreamWriter("file.txt");
writer.WriteLine((radar_angle + 90).ToString());
writer.Close();
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("E");
}
serialPort1.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void distance_label_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
myProc.Kill();
if (serialPort1.IsOpen)
serialPort1.Close();
else
{
MessageBox.Show("Warning! Serial Port is not opened!");
}
login form = new login();
this.Hide();
form.ShowDialog();
this.Close();
}
private void portNameComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btnStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
while (btnStop.Enabled == true)
{
btnStop.Text = "Disconnected";
btnStop.Enabled = false;
btnStart.Text = "Connect";
btnStart.Enabled = true;
serialPort1.WriteLine("T");
_radar.PauseScanLine = true;
serialPort1.Close();
}
}
myProc.Kill(); // Kills the image capture program when launched with button start
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
}
}
<br/>
// The radar.CS is actually a class which contains methods such as RemoveItem(); If you understood my codes, you would have realized in frmMain.CS, I could actually use the Radar.CS library successfully.<br/>
The problem I get is object instance not set to the instance of an object when I click the enemy button and I expect the application to turn the item sent by the hardware on the radar from green to red, and to send an email.<br/>
Please help me. Many thanks!
View the full article