Trying and succeeding to communicate with a device by serial when using a simple console application and the SerialPort is instantiated in the code as follows. There's a DataReceived event handler that's also working fine, I know the device and the code works.
I now want to build up methods and therefore put them in their own class for calling from another source, but I'm having a little difficulty in that it when I do this, it doesn't seem to want to send or receive data. So, my new console code looks like:
The corresponding DevControl class looks like this:
Am I missing something glaringly obvious? The sent command should cause an action on the device, which it isn't. The IsOpen value of Lambda just before that call is set to true, so it's made the connection though it would seem.
Other places mention DtsControl and such properties, but it was working in the pure console app code without altering anything like this.
Code:
class Program
{
static SerialPort sp = new SerialPort("COM8", 9600, Parity.None, 8, StopBits.One);
static bool _continue = true;
public static string serialBuffer;
static void Main(string[] args)
{
I now want to build up methods and therefore put them in their own class for calling from another source, but I'm having a little difficulty in that it when I do this, it doesn't seem to want to send or receive data. So, my new console code looks like:
Code:
static void Main(string[] args)
{
string result;
DevControl GEN = new DevControl("COM8", 9600, 6);
result = GEN.TestConnect();
Console.WriteLine(result);
Console.ReadLine();
}
The corresponding DevControl class looks like this:
Code:
public class DevControl
{
private static SerialPort Lambda;
private static string serialBuffer = "";
public DevControl(string COMport, int BaudRate, int Address)
{
Lambda = new SerialPort(COMport, BaudRate, Parity.None, 8, StopBits.One);
Lambda.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
public static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
serialBuffer = Lambda.ReadExisting();
serialBuffer = serialBuffer.Replace("\0", "\n");
}
public string TestConnect()
{
Lambda.Open();
Lambda.WriteLine("OUT ON");
while (!serialBuffer.Contains("\n"))
{
Thread.Sleep(50);
}
string returnValue = serialBuffer;
serialBuffer = "";
return returnValue;
}
}
Am I missing something glaringly obvious? The sent command should cause an action on the device, which it isn't. The IsOpen value of Lambda just before that call is set to true, so it's made the connection though it would seem.
Other places mention DtsControl and such properties, but it was working in the pure console app code without altering anything like this.
Last edited: