Solved

Keep serial port open in console app

Posted on 2013-12-17
11
405 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
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 51

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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling arrays 14 33
Build JSON from table records 17 40
Converting C# Objects into JSON file 7 39
Check my code out(2) 1 33
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now