read write serial port

I have found several classes online that say they make using the serial ports easy. I am never able to get any of them to compile. Is there a bare bones example of how to read/write to a serial port all bundled in a project for Visual C++ Express Edition

Answer this question

read write serial port

  • Juan Reyes

    I don't see that the class you are showing here needs MFC. The class seams to me like plain C++ code with Platform SDK.

    And yes you can just use the to communicate with the serial port via the Platform SDK functions. You need to install at least this!



  • TerryTuell

    I have installed the SDL Platform. I believe I have done this correctly. I did everything it said to and the build test ran OK.

    This is the location of (one of) the classes I am trying to get to work. There are more complicated ones out there but this one seems the streamlined without all the bells and whistles.

    http://www.codeguru.com/Cpp/I-N/network/serialcommunications/article.php/c2503/

    I am a few years out of practice with C++. I do my best but I may not have all the necessary header files and such. Can I merge this .cpp and .h into a colsole app or will it have to be a window app

    I have tried to copy over bits and pieces of the code one function at a time. I always run into this:

    error C2664: 'wsprintfW' : cannot convert parameter 1 from 'char [15]' to 'LPWSTR'

    Which you have already pointed out the problem too. I have been reading what you suggested. Would it not be easier to change my code to only use 8 bit characters I have tried to use #define _MBCS in the code. Of course I am not sure I am doing it correctly. Everything I have read talks about mapping things differently depending on _UNICODE or _MBCS and such. But I never see any reference to my erros in particular. (Not surprising.) I am trying your TCHAR method but I am fumbling at this point.


  • Dick Donny

    Stop the presses!!

    I revisited your TCHAR post. I had tried this already but I must have done something wrong. I can now get this to compile under a windows app.

    However...

    When I try to use this in a console app I get the following error:

    serial3.obj : error LNK2019: unresolved external symbol __imp__wsprintfW referenced in function "public: int __thiscall CSerial::Open(int,int)" ( Open@CSerial@@QAEHHH@Z)

    Am I missing something or is this thing written only for a windows based app


  • Devboy

    Seams that you defined _UNICODE for you program bat the code you copied was written for MBCS. I can see this because wsprintf maps to wsprintfW

    Either switch to MBCS or use wchar_t..

    You need to learn more about TCHAR, MBCS and UNICODE.

    In this small sample rewrite in this way:

    TCHAR szPort[15];
    TCHAR szComParams[50];

    DCB dcb;
    wsprintf( szPort, _T(
    "COM%d"), nPort );



  • f-l-i-p

    Umm....

    kernel32.lib $(NoInherit)

    Is that good or bad NOT giving up. Have you read the part of this thread that talks about tchar Not sure if relevent but I did modify the code as stated.

    Thanks for the help.


  • bernardus

    Ok it has been a little while but I have gotten slightly educated. I see now that VC++ express does not support every example I have found on the web. Mainly MFC. I do not fully understand what MFC is but I know I do not have it. Is there any way to read/write to the serial port using VC++ Express edition
  • Mohamed Raafat

    Hey!! I looked at the same place with the window app that worked. It was blank and had the check box below checked. (Inherent from parent or project defaults.) Now it compiles!! Would you suggest a different setting


  • LTN

    What does the line say in Project Properties -> General -> Linker -> Input -> Additional Dependencies

    Don't give up on _UNICODE now... you're very close to compiling the application.



  • NePasSql

    It depends what classes you found. You need a class based purely on the Win32 API. Even than you need to install the PSDK as documented. If you used one using MFC and ATL you must fail because MFC and ATL are not part of VC Express.

    http://msdn.microsoft.com/visualc/learning/vcexpvids/default.aspx



  • T Gray

    Yes I did that step. Step 5 was also a success. And this app compiles when I have a win32 window project. It only fails on a win32 console project.

    I am guessing that there is something in the cserial::open() function that needs something that a win32 window app has when the project is created.

    I have never done a window app, only console ones. The reason behind this app is to be able to communicate from a pc to microcontrollers. I am not worried about it working world wide. Just here and now on xp or 2000....

    How would one compile something under _UNICODE or _MBCS

    Is it a properties setting or a #define


  • Pedro Ferreira

    I have installed the SDK. Wish I had that video for that part. But I got the project to build and execute so I must have done it right.

    I was able to get through a few of the errors in the class code I had. Things like BOOL had to be lower case. It has been a few years since my OOP class so I am struggling with the rest of the errors in the code. Here is a quick snippet example:

    (From a class declaration in a header file.)

    class CSerial

    {

    public:

    CSerial();

    ~CSerial();

    bool Open( int nPort = 2, int nBaud = 9600 );

    bool Close( void );

    int ReadData( void *, int );

    int SendData( const char *, int );

    int ReadDataWaiting( void );

    bool IsOpened( void ){ return( m_bOpened ); }

    protected:

    bool WriteCommByte( unsigned char );

    HANDLE m_hIDComDev;

    OVERLAPPED m_OverlappedRead, m_OverlappedWrite;

    bool m_bOpened;

    };

    The compiler has a problem with HANDLE and OVERLAPPED. Complaining about a missing ";" on those lines. And subsequent areas in the code where they are used.

    There must be some code out there that will do the trick for me. I don't need anything fancy, just want to communicate with a microcontroller in a very simple and controlled environment.


  • Nagie_Stopy

    You are not including the standard windows header files.

    In this case there would be no complaint about the BOOL in uppercase. Note that there is a define from int to BOOL in the PSDK bcause bool (lowercase) is C++ only.

    You are missing to include <windows.h>!



  • DarthVic

    In my opinion, you're better off in the long term converting the code to compile under _UNICODE. _UNICODE is not only faster under Windows 2000/XP, if you want your app to run reliably on systems outside the US, you'll want to enable unicode.

    You can check out chapter 2 of Programming Windows by Charles Petzold for reasons why unicode was created and adopted by Windows NT/2000/XP/2003/Vista. Note that in order to get your app to run on Win9x, you'll want to link with the Microsoft Layer for Unicode.

    About your linker error, Did you follow the extra PSDK steps described in http://msdn.microsoft.com/vstudio/express/visualc/usingpsdk/default.aspx (esp. step 4).



  • oguevarra

    yes!! Great help. That took care of 110 out of 113 errors. Now it seems like the only thing left is a type casting problem

    snip

    BOOL CSerial::Open( int nPort, int nBaud )

    {

    if( m_bOpened ) return( TRUE );

    char szPort[15];

    char szComParams[50];

    DCB dcb;

    wsprintf( szPort, "COM%d", nPort );

    m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );

    if( m_hIDComDev == NULL ) return( FALSE );

    memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );

    memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

    snip (error lines in bold)

    errors...

    c:\documents and settings\mcbride7\my documents\visual studio 2005\projects\serial5\serial5\serial5.cpp(32) : error C2664: 'wsprintfW' : cannot convert parameter 1 from 'char [15]' to 'LPWSTR'

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    c:\documents and settings\mcbride7\my documents\visual studio 2005\projects\serial5\serial5\serial5.cpp(33) : error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char [15]' to 'LPCWSTR'

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    c:\documents and settings\mcbride7\my documents\visual studio 2005\projects\serial5\serial5\serial5.cpp(47) : error C2664: 'wsprintfW' : cannot convert parameter 1 from 'char [50]' to 'LPWSTR'

    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    any ideas


  • read write serial port