• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1744
  • Last Modified:

RS232 interface with c++

Hi,

I am looking to write an application in C++ which opens a connection to a specific serial port and connects to a piece of hardware which accepts plain text commands and then responds with either 'ERROR' or 'OKAY'. I have tested the hardware using windows 'hyperterminal' in which I was able to open a connection to the hardware and type commands and it responded as expected. Can anyone please give me some pointers as to how to do this from a very basic C++ app??

Many Thanks,

Chris
0
chrishughes
Asked:
chrishughes
  • 12
  • 11
  • 7
  • +3
1 Solution
 
jkrCommented:
I'd suggest http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_serial.asp ("Serial Communications in Win32") which is a quite comprehensive article and also tutorial ergarding this issue. It comes with full sample code.
0
 
edin_el_bosnewiCommented:
Hi,

I have recently done a complete application that communicated with a mobile phone over RS232 port.  The first problems I encountered was that .NET 1.1 didn't have a library for RS232 port comunication.  There are however ways to deal with this using old win 32 APIs, which I found little complicated since it uses a syntax for opening and working with a file.  Then I found myself the best possible solution.  

New Visual Studio 2005 Beta with .NET framework 2.0 has a complete new library for working with a serial port.  It is stright forward and a lot less complicated than with APIs.  So I suggest you download the new C++.NET and .NET framework SDK from Microsoft homepage (FREE), and start working.   There is a great article for this at: http://www.codeproject.com/csharp/SerialCommunication.asp
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chrishughesAuthor Commented:
Hi all,

Thank you for your links,

I have had a good read but got no further. My main problem is that all of those links require the use of libraries within visual C++, and I unfortunately do not have a license for MS Visual C++ and so I am using the bloodshed dev-c++ and which ever compiler that uses by default.

Any other suggestions??? HELP!!

Chris
0
 
jkrCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_serial.asp ("Serial Communications in Win32") should work with bloodshed dev-c++
0
 
Jaime OlivaresCommented:
not all the proposed libraries are exclusively for Visual C++, the half of them uses pure WinAPI function calls, if you read carefully...
0
 
chrishughesAuthor Commented:
Ok, Thanks again for your help, but I have got myself really confused! I am not sure which the best way to do things is! I am assuming that I need to find a library import it in my code and then I should be able to call instructions in that library? How can I import the WinAPI function calls!

Appologies if I sound like I have got myself really confused, but I am not sure what way to go from here!

Thanks,

Chris
0
 
edin_el_bosnewiCommented:
Hello,  

You didn't read carefully what I wrote:


New Visual Studio 2005 Beta with .NET framework 2.0 has a complete new library for working with a serial port.  It is stright forward and a lot less complicated than with APIs.  So I suggest you download the new C++.NET and .NET framework SDK from Microsoft homepage (FREE), and start working.   There is a great article for this at: http://www.codeproject.com/csharp/SerialCommunication.asp

This is all FREEEEEEE!
0
 
Jaime OlivaresCommented:
Visual Studio 2005 Beta or Visual Studio Express Edition is not for commercial use, but if you are a studio, it is OK.
If not, I suggest you to spend $110 for a cheap Professional product like Visual C++, with hundreds of features, libraries and tools.
0
 
Jaime OlivaresCommented:
sorry, I meant "if you are a student"
0
 
chrishughesAuthor Commented:
>New Visual Studio 2005 Beta with .NET framework 2.0 has a complete new library for working with a serial port.  It is stright forward and a lot less complicated than with APIs.

Is this library avaliable in Visual studio 2003? I have just found out that we have a license for the 2003 version. If not is there an easy way to add it to the older version?

Many Thanks,

Chris
0
 
edin_el_bosnewiCommented:
System.IO.Ports is an integrated library as a part of new .NET framework and is for use with Visual Studio 2005 only!  However there are third party libraries that have the same function, but most of them are commercial and need to be bought.  Some of those are:

http://www.sax.net/framework/communications/
http://franson.com/serialtools/index.asp?platform=net
http://www.scientificcomponent.com/portcontroller_net.htm?referrer=google-serial-port-source-code

There are only couple of free ones but still have licence issues, I'll write them down later on!

0
 
chrishughesAuthor Commented:
>There is a great article for this at: http://www.codeproject.com/csharp/SerialCommunication.asp

Hi guys,

Ok I have managed to find someone prepeared to let me use their visual studio 2005 beta 2 setup! I have looked at the above article and it is specifically written for c# not c++.. What do I need to include to use the serialport class in C++?

Cheers...
0
 
Jaime OlivaresCommented:
>What do I need to include to use the serialport class in C++?

One of the classes I have suggested earlier.
0
 
chrishughesAuthor Commented:
Hi Jaime,

I am looking to use the serial port class included with visual studio 2005. However I have no idea which file I need to include to use it.

Chris
0
 
Jaime OlivaresCommented:
I have not viewed it, but I suposse it is the .net class, so you will need to work with managed C++.

So I think you will need:
using namespace System::IO::Ports;

so you can use the SerialPort class:
http://msdn2.microsoft.com/library/e96b0a7f(en-us,vs.80).aspx
0
 
chrishughesAuthor Commented:
OK, So I am probably going to sound very stupid here: How do I create a new instance of the serial port object. I have included the namespace!

Thanks,

Chris
0
 
Jaime OlivaresCommented:
To instantiate, you can declare as a member of your main window, by example

using namespace System;
using namespace System::IO::Ports;
// Some other namespaces

public class YourDialog : System.Windows.Forms.Form
{
        SerialPort m_Serial;   // declares a SerialPort member
        // etcetera
}

Later you can create the object:
m_Serial = new SerialPort("COM1:",1200,Parity::None,1);

Then you have to open the port somewhere:
g_Serial->Open();

now you can use port, you will have to read the documentation in the link I have posted above
0
 
chrishughesAuthor Commented:
I am really sorry that i am new to this and have a lot to learn. A sample of my code looks like this:
____________
//A test application to use a serial port for comms
using namespace System;
using namespace System::IO::Ports;
#include "stdafx.h"

int main(int argc, char *argv[])
{
      m_Serial = new SerialPort("COM1:",1200,Parity::None,1);
        m_Serial->Open();
        return 0;
}
__________________

I am not sure how I should deal with the declaration part?

Many Thanks for your help...

Chris
0
 
chrishughesAuthor Commented:
Hi,

> To instantiate, you can declare as a member of your main window, by example
I am writting a command based application so I can;t see how to do this.

Please help, I am really struggling with this!

Thanks,

Chris
0
 
Jaime OlivaresCommented:
Instead of declaring as your main window member, declare it as a global variable:

using namespace System;
using namespace System::IO::Ports;
// Some other namespaces

//Declare it as global:
SerialPort *g_Serial;

int main(int argc, char *argv[])
{
     g_Serial = new SerialPort("COM1:",1200,Parity::None,1);
        g_Serial->Open();
        return 0;
}
0
 
chrishughesAuthor Commented:
Ok, I have created a new console project and entered the following code:

__
using namespace System;
using namespace System::IO::Ports;
// Some other namespaces
#include "stdafx.h"
//Declare it as global:
SerialPort *g_Serial;

int main(int argc, char *argv[])
{
     g_Serial = new SerialPort("COM1:",1200,Parity::None,1);
        g_Serial->Open();
        return 0;
}
__

I get the folowing errors:

c:\............\serielio.cpp(6) : error C2143: syntax error : missing ';' before '*'
c:\............\serielio.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\............\serielio.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\............\serielio.cpp(10) : error C2061: syntax error : identifier 'SerialPort'
c:\............\serielio.cpp(10) : error C2653: 'Parity' : is not a class or namespace name
c:\............\serielio.cpp(11) : error C2227: left of '->Open' must point to class/struct/union/generic type
        type is 'int *'

So I am obviously not linking to the library properly as it does not recognise the datatype?

Any ideas?

Many Thanks,
0
 
Jaime OlivaresCommented:
Are you trying to create a .net managed C++ application or a traditional non-managed application?

0
 
chrishughesAuthor Commented:
I have just created a new project of type visual C++/Win 32 console and not changed any settings. How would I make this a managed application? I assume that that is the best way to go?
0
 
Jaime OlivaresCommented:
0
 
chrishughesAuthor Commented:
Is it possible to do this without using a managed application?
0
 
Jaime OlivaresCommented:
for non-managed examples and classes, look at my first post.
0
 
chrishughesAuthor Commented:
Hi,

Right - I may be finally getting somewhere. I have used the library provided by the first link (
http://www.codeproject.com/system/serial.asp) and I can now write data to the serial port. I am now struggling with using it to read back from the serial port.

The serial.WaitEvent(); works and pauses the execution until an event is recieved. I am just getting confused as to how to read back whatever character was recieved. Ideally I need mt program to stop and wait until it recieves the complete reply which will be indicated by a <CR> on the end of the line.

Any ideas?

Increased points as this has dragged on!
0
 
marcodalzottoCommented:
After opening the com port you should set the event char :

SetEventChar(0x0D, true);
In this way get an event when the 0x0D (CR) is recived.

You call the WaitEvent function to look for events:
WaitEvent (NULL, 10); // 10 is the timeout after which the function will return
if(GetEventType() & CSerial::EEventRcvEv) {
// then you can read form the buffer and get your data
}

To avoid the program to stop you can use a thread or a frequently called function.
0
 
waysideCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jaime_olivares

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

wayside
EE Cleanup Volunteer
0
 
jkrCommented:
All links posted by jaime_olivares (except one) are either MFC or CE and won't work with Bloodshed-C++, thus I'd rather advise a split. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_serial.asp ("Serial Communications in Win32")  still is *the* referece regarding that topic.
0
 
jkrCommented:
I forgot - http://www.codeproject.com/system/serial_com.asp is the one w/o MFC, and it's IMHO a really good one.
0
 
waysideCommented:
I didn't check all of jaime's links to see what was in them, and he did a lot of work to try to help the OP beyond just giving links. So I recommended that he get all the points.
0
 
jkrCommented:
Not going to insist on anything here, just added my 2 cents :o)
0
 
jkrCommented:
>>We have new CVs as you can see

... and I *really* appreciate that!
0
 
waysideCommented:
> This is not a good idea. If you will recommend accept of some points - you need to had checked the links

OK, I went back and checked all the links. I only saw one that seemed completely MFC-based, although others used MFC to make a demo which would not preclude the sample being used in a non-MFC app; however I am not going to download all of the code in the links and test all of it myself.

So, given that jaime's links are good (in fact jkr himself said one of them was "a really good one"), and that he by far tried to provide the most help to the OP, I don't think an even split is fair, so I recommended he get all the points. If the mod wants to do an uneven split, I can live with that. The help doesn't indicate this is a possibility, though.
0
 
jkrCommented:
>>I don't think an even split is fair

Noone said that it had to be even (IMHO). But, before this gets out of hand and generates a lot of traffic and discussion and work for you when it comes to 100pts, I definitely will agree to your recommendation.

Thanks for your effort, and I mean it!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 12
  • 11
  • 7
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now