Link to home
Start Free TrialLog in
Avatar of chrishughes
chrishughes

asked on

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
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Avatar of jkr
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.
Avatar of edin_el_bosnewi
edin_el_bosnewi

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
Avatar of chrishughes

ASKER

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
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++
not all the proposed libraries are exclusively for Visual C++, the half of them uses pure WinAPI function calls, if you read carefully...
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
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!
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.
sorry, I meant "if you are a student"
>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
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!

>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...
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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
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
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
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
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;
}
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,
Are you trying to create a .net managed C++ application or a traditional non-managed application?

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?
Is it possible to do this without using a managed application?
for non-managed examples and classes, look at my first post.
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!
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.
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
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.
I forgot - http://www.codeproject.com/system/serial_com.asp is the one w/o MFC, and it's IMHO a really good one.
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.
Not going to insist on anything here, just added my 2 cents :o)
>>We have new CVs as you can see

... and I *really* appreciate that!
> 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.
>>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!