I want to be able to access methods in my "Uart" class from "AtCommand" class. ie 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");
}
}
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.
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");
}
}
}
您需要同时制作 Uart 和 AtCommand公共课程
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.