[code]
- 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 WindowsFormsApplication1
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.PortName = "COM1";
- serialPort1.BaudRate = 9600;
- serialPort1.DataBits = 8;
- serialPort1.Parity = Parity.None;
- serialPort1.StopBits = StopBits.One;
- serialPort1.Open();
- disconnectBtn.Enabled = true;
- connectBtn.Enabled = false;
- }
- catch
- {
- MessageBox.Show("Error", "Unable To Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- if (connectBtn.Enabled == false)
- {
- textBox1.Text = "Connected";
- }
- }
- private void disconnectBtn_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.Close();
- connectBtn.Enabled = true;
- disconnectBtn.Enabled = false;
- }
- catch
- {
- MessageBox.Show("Error", "Unable To Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- if (disconnectBtn.Enabled == false)
- {
- textBox1.Text = "Disconnected";
- }
- }
- private void button1_Click_1(object sender, EventArgs e)
- {
- serialPort1.Write(new byte[] {0x55}, 0, 1); //OK
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Class1 sp = new Class1();
- sp.serialOut();
- }
- }
- }
[/code]
But, when i click button2 link to class2 to call serial port, it does not work.
[code]
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO.Ports;
- namespace WindowsFormsApplication1
- {
- class Class1
- {
- public void serialOut()
- {
- MainForm _mainForm = new MainForm();
- _mainForm.serialPort1.Open(); //UnauthorizedAccessException was unhandle
- _mainForm.serialPort1.Write(new byte[] { 0x55 }, 0, 1); //InvalidOperationException was unhandle
- }
- }
- }
[/code]
I had set the serialPort1 to public in properties.
How can i solve this? Thank you.
FroglegPosted Mar 13, 2011, 4:28 AM
hello leongPosted Mar 15, 2011, 8:28 AM
FroglegPosted Mar 13, 2011, 3:43 PM
hello leongPosted Mar 13, 2011, 7:25 AM
FroglegPosted Mar 13, 2011, 3:29 AM
It's now working on my machine
Sam HobbsPosted Mar 13, 2011, 12:43 AM
hello leongPosted Mar 12, 2011, 11:46 PM
PS. Scan before open the attach file. Thanks.
Sam HobbsPosted Mar 12, 2011, 3:48 PM
VulpesPosted Mar 12, 2011, 1:52 PM
I am therefore puzzled why this exception has now gone away and you're getting a null reference exception on this line:
MainForm.Fm1.serialPort1.Open(); //NullRefenceException was unhandled
On the face of it, it's simply not possible for either Fm1 to be null (given the nature of Frogleg's amendments) or for serialPort1 (assuming you've added it from the VS designer) to be null. Do you perhaps have another variable called MainForm in Class1 which is hiding the class of the same name?
FroglegPosted Mar 12, 2011, 1:47 PM
hello leongPosted Mar 12, 2011, 1:20 PM
Suthish NairPosted Mar 12, 2011, 12:57 PM
FroglegPosted Mar 12, 2011, 2:17 AM
public partial class MainForm : Form
{
public static MainForm Fm1 = null; // creates single instance<<<<<<<<<
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Fm1 = this;//<<<<<<<<<<<<<<<
}
hello leongPosted Mar 11, 2011, 9:29 PM
But, another problem,
MainForm.Fm1.serialPort1.Open(); //NullRefenceException was unhandle
MainForm.Fm1.serialPort1.Write(new byte[] { 0x55 }, 0, 1); //NullRefenceException was unhandle
How to create new instance for this? Thanks
FroglegPosted Mar 10, 2011, 8:09 AM
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
public static MainForm Fm1 = null; // creates single instance
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Fm1 = this;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
disconnectBtn.Enabled = true;
connectBtn.Enabled = false;
}
catch
{
MessageBox.Show("Error", "Unable To Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (connectBtn.Enabled == false)
{
textBox1.Text = "Connected";
}
}
private void disconnectBtn_Click(object sender, EventArgs e)
{
try
{
serialPort1.Close();
connectBtn.Enabled = true;
disconnectBtn.Enabled = false;
}
catch
{
MessageBox.Show("Error", "Unable To Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (disconnectBtn.Enabled == false)
{
textBox1.Text = "Disconnected";
}
}
private void button1_Click_1(object sender, EventArgs e)
{
serialPort1.Write(new byte[] {0x55}, 0, 1); //OK
}
private void button2_Click(object sender, EventArgs e)
{
Class1 sp = new Class1();
sp.serialOut();
}
}
}
[/code]
But, when i click button2 link to class2 to call serial port, it does not work.
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
class Class1
{
public void serialOut()
{
MainForm.Fm1.serialPort1.Open(); //UnauthorizedAccessException was unhandle
MainForm.Fm1.serialPort1.Write(new byte[] { 0x55 }, 0, 1); //InvalidOperationException was unhandle
}
}
}
This code works with serial port set to public, but with a bit more code you can set it back to private