Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Keep serial port open in console app

Posted on 2013-12-17
11
Medium Priority
?
486 Views
Last Modified: 2014-01-23
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...
0
Comment
Question by:Blowfelt82
  • 6
  • 3
10 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39723543
>>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.
0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 39723571
This is not an issue of keeping the serial connection open - it will stay open until you close it. This is an issue of keeping your application running.

What I suspect is happening is that your application is running and completing - because there is nothing to force it to stop and wait.

You need to structure your application to go into a waite state - you could use one of the Wait (signal) functions to force the application to halt and then run the listener in a thread - where the thread writes to a buffer and the wait function loops around checking the buffer for output.
0
 

Author Comment

by:Blowfelt82
ID: 39723573
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!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39723678
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).
0
 

Author Comment

by:Blowfelt82
ID: 39723680
JulianH, that solution sounds good... Any ideas on how to implement this? I am a novice!
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39723693
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.
0
 

Author Comment

by:Blowfelt82
ID: 39723756
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.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39725856
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.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39750840
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 ?
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 39805669
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.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question