Serial communication in C++

Jun 15, 2011 at 12:51am
Hi!

I am trying to figure out how serial communication in C++ works. Have found this page, http://www.codeproject.com/KB/system/serial.aspx, and have spent a lot of time of reading and googling. I just want to send messages out on the serial COM1-port. So I did like this:

Included his serial.h and serial.cpp from the source-code and did my own serialmain.cpp where I pasted his code for writing to the port:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#define STRICT
#include <tchar.h>

#include <windows.h>

#include "Serial.h"


int WINAPI _tWinMain
          (
           HINSTANCE /*hInst*/, 
           HINSTANCE /*hInstPrev*/, 
           LPTSTR    /*lptszCmdLine*/, 
           int       /*nCmdShow*/
          )
{
    CSerial serial;

    // Attempt to open the serial port (COM1)

    serial.Open(_T("COM1"));

    // Setup the serial port (9600,N81) using hardware handshaking

    serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
    serial.SetupHandshaking(CSerial::EHandshakeHardware);

    // The serial port is now ready and we can send/receive data. If

    // the following call blocks, then the other side doesn't support

    // hardware handshaking.

    serial.Write("Hello world");

    // Close the port again

    serial.Close();
    return 0;
}


As a result of this I get the following errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1>serial.obj : error LNK2005: _wWinMain@16 already defined in serialmain.obj
1>serialmain.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CSerial::~CSerial(void)" (??1CSerial@@UAE@XZ) referenced in function _wWinMain@16
1>serial.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall CSerial::~CSerial(void)" (??1CSerial@@UAE@XZ)
1>serialmain.obj : error LNK2019: unresolved external symbol "public: virtual long __thiscall CSerial::Close(void)" (?Close@CSerial@@UAEJXZ) referenced in function _wWinMain@16
1>serial.obj : error LNK2001: unresolved external symbol "public: virtual long __thiscall CSerial::Close(void)" (?Close@CSerial@@UAEJXZ)
1>serialmain.obj : error LNK2019: unresolved external symbol "public: virtual long __thiscall CSerial::Write(char const *,unsigned long *,struct _OVERLAPPED *,unsigned long)" (?Write@CSerial@@UAEJPBDPAKPAU_OVERLAPPED@@K@Z) referenced in function _wWinMain@16
1>serial.obj : error LNK2001: unresolved external symbol "public: virtual long __thiscall CSerial::Write(char const *,unsigned long *,struct _OVERLAPPED *,unsigned long)" (?Write@CSerial@@UAEJPBDPAKPAU_OVERLAPPED@@K@Z)
1>serialmain.obj : error LNK2019: unresolved external symbol "public: virtual long __thiscall CSerial::SetupHandshaking(enum CSerial::EHandshake)" (?SetupHandshaking@CSerial@@UAEJW4EHandshake@1@@Z) referenced in function _wWinMain@16
1>serial.obj : error LNK2001: unresolved external symbol "public: virtual long __thiscall CSerial::SetupHandshaking(enum CSerial::EHandshake)" (?SetupHandshaking@CSerial@@UAEJW4EHandshake@1@@Z)
1>serialmain.obj : error LNK2019: unresolved external symbol "public: virtual long __thiscall CSerial::Setup(enum CSerial::EBaudrate,enum CSerial::EDataBits,enum CSerial::EParity,enum CSerial::EStopBits)" (?Setup@CSerial@@UAEJW4EBaudrate@1@W4EDataBits@1@W4EParity@1@W4EStopBits@1@@Z) referenced in function _wWinMain@16
1>serial.obj : error LNK2001: unresolved external symbol "public: virtual long __thiscall CSerial::Setup(enum CSerial::EBaudrate,enum CSerial::EDataBits,enum CSerial::EParity,enum CSerial::EStopBits)" (?Setup@CSerial@@UAEJW4EBaudrate@1@W4EDataBits@1@W4EParity@1@W4EStopBits@1@@Z)
1>serialmain.obj : error LNK2019: unresolved external symbol "public: virtual long __thiscall CSerial::Open(wchar_t const *,unsigned long,unsigned long,bool)" (?Open@CSerial@@UAEJPB_WKK_N@Z) referenced in function _wWinMain@16
1>serial.obj : error LNK2001: unresolved external symbol "public: virtual long __thiscall CSerial::Open(wchar_t const *,unsigned long,unsigned long,bool)" (?Open@CSerial@@UAEJPB_WKK_N@Z)
1>serialmain.obj : error LNK2019: unresolved external symbol "public: __thiscall CSerial::CSerial(void)" (??0CSerial@@QAE@XZ) referenced in function _wWinMain@16
1>serial.obj : error LNK2001: unresolved external symbol "public: __thiscall CSerial::CSerial(void)" (??0CSerial@@QAE@XZ)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup


I am using Visual C++ 2008 Express as a compiler and I have unchecked the box which says precompiled headers. What am I doing wrong?

Thanks a bunch!
Jun 15, 2011 at 2:36am
From the looks of it, I would say the project from codeproject generates a static library. This means that once you compile that source code, you end up with a .lib file (seems it should be serialu.lib). You need to tell your linker to use it. The easiest would be to add #pragma comment(lib, "serialu.lib") to your code after line #6.
Topic archived. No new replies allowed.