Reputation: 11
I want to be able to access methods in my "Uart" class from "AtCommand" class. i.e. the AT command class sends and receives commands to and from modem via serial port. I an unable to figure out why the methods in Uart are NOT available in "AtCommand" BUT they ARE available if I try to access them from my Main Form.
Here is the code for both classes, note: there are wriggly red lines under GsmPort.Write and warning it is not available in current context (so I assume scope issue).
using System.IO.Ports;
namespace ClassLessons
{
class Uart
{
public bool Connected { get; set; }
public bool DataInBuffer { get; set; }
public string RxData;
SerialPort port = new SerialPort();
public Uart()
{
this.port.PortName = Properties.Settings.Default.PortName;
this.DataInBuffer = false;
this.RxData = "";
this.port.BaudRate = 115200;
this.port.ReadTimeout = 500;
this.port.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
Connected = false;
try
{
if (!port.IsOpen)
{
port.Open();
Connected = true;
}
}
catch { }
}
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string data = this.port.ReadLine();
RxData = data;
DataInBuffer = true;
}
catch
{
}
}
public void Write(string message)
{
this.port.WriteLine(message);
}
}
} }
AND AtCommand :
namespace ClassLessons
{
class AtCommand
{
Uart GsmPort = new Uart();
GsmPort.Write("Test");
}
}
Upvotes: 1
Views: 66
Reputation: 6915
Your port field is private:
Change:
SerialPort port = new SerialPort();
To:
public SerialPort port = new SerialPort();
And it will be publicly accessible in other classes.
Upvotes: 1
Reputation: 1346
You're calling GsmPort.Write("Test")
outside of a method. You probably want something like:
namespace ClassLessons
{
class AtCommand
{
Uart GsmPort = new Uart();
public AtCommand()
{
GsmPort.Write("Test");
}
}
}
Upvotes: 0