Solved

Keep serial port open in console app

Posted on 2013-12-17
11
446 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 58

Accepted Solution

by:
Julian Hansen earned 500 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

628 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