Link to home
Start Free TrialLog in
Avatar of Blowfelt82
Blowfelt82

asked on

Keep serial port open in console app

I want to develop a C# console application that opens a serial port connection and listens on the port for any input. When input is received I want the application to send a response based on that input.

What I need to know is a way to keep the serial connection open within the console application? When I open the SerialPort object and attach a listener the console closes immediatly after this so the listener never has a chance to execute... Any ideas?

Also if anyone has any examples of the send/receive messages part of this problem they would be appreciated...
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>When I open the SerialPort object and attach a listener the console closes immediatly after this so the listener never has a chance to execute... Any ideas?

My first guess is you haven't coded things correctly.  Showing what you are doing will help spot any errors.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Blowfelt82
Blowfelt82

ASKER

My console application is based largely on..

http://www.dreamincode.net/forums/topic/35775-serial-port-communication-in-c%23/

the functionality is more of less the same, except it is setup for a console application?

I call the code as follows:

SerialPortManager SPM = new SerialPortManager(baudRate, parity, stopBits, dataBits, portName);

SPM.openPort();

I cannot copy the code as it is on a secure server, and would involve me copying line by line!
I've had a check at the link and it does not show a complete application.
To repeat myself - you have probably not coded things correctly.  Just saying it is something similar to XYZ isn't too helpful.
Posting the code where you loop and wait until the user closes the console app might give a hint.  Also the section where you open the port - maybe it is going out of scope and then destroyed for example.

(If there isn't a loop it may well be the very first response you receive is resulting in the app closing).
JulianH, that solution sounds good... Any ideas on how to implement this? I am a novice!
The comment from JulianH refers to just one of many possible things - you need to identify the problem first.  Which is why I would like you to show what you are doing.


The following is something one sees despite it being stupid.
I must do something.
This is something.
Therefore I must do this.
I have a class 'SPM' with a function:

private SerialPort portListener = new SerialPort();

public void ReceiveData(object sender, SerialDataReceivedEventArgs e)
{
 string m = portListener.ReadExisting();
 Console.WriteLine(">>>" + m);
}

public void WriteData(string m)
{
 if(!(portListener.IsOpen == true)) portListener.Open();
 portListener.Write(m);
 Console.WriteLine("<<<" + m);
}

public bool openPort()
{
 if(portListener.IsOpen == true)
 {
  portListener.Close();
 }
portListener.BaudRate = int.Parse(9600);
// equivelent lines for data bits, parity, stop bits, port name...
portListener.open()
portListener.DataReceived += new SerialDataReceivedEventHandler(ReceiveData);



}

Open in new window


I am calling this from my consoles Main structure

static SPM SerialInterface;

static void Main(string[] args)
{
 SerialInterface = new SPM();
 SerialInterface.openPort();
 Console.ReadLine();
}

Open in new window



I was hoping to see a read event fired once a connection is made to the COM port. Also want this to stay open and listen - the thread option looked good. If I remove the readline obviously it just closes itself!

Also any advice on how I can sent sequential messages down the COM port would be useful.
You have this in the main section:
Console.ReadLine();
In your handler you use
Console.WriteLine

I guess everything functions correctly, a response is written then read and the console app closes.
The code you supplied in comment #a39723756 should have kept your program running until something was read in from the console.  What was not working / what was it in the comment you accepted that indicated and solved your problem ?
For me this should be a delete.  The comment that was accepted guessed it was because the program didn't have anything to keep it running.  The code supplied later showed a line which should have kept the program active - wrong guess.