Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Keep serial port open in console app

Posted on 2013-12-17
11
Medium Priority
?
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
11 Comments
 
LVL 44

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 59

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 44

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 44

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 44

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 44

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 44

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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