Newbie, please help me compile this code

S

Sonoman

Hi all:
I know a bit of c++ but not enough to get this code working. I downloaded
the following code and I am trying to have this code work so I can receive
some data from a serial port. My microprocessor is already sending the data
to COM5. I just need to catch it and save it to a file with the following
c++ code. I have already resolved some erros by adding "/TP" to the
project>settings>c++ tab on VC++6. Can anyone please help me get the errors
out so I can start modifying it to suit my needs? The code is as follows:

**********serial.h
/* serial.h */
#ifndef SERIAL_H
#define SERIAL_H

#define SUCCESS 0
#define CANT_OPEN_PORT 1
#define CANT_WRITE_TO_PORT 2
#define CANT_READ_FROM_PORT 3
#define NO_READINGS 4
#define CANT_CLOSE_PORT 5

#ifdef __cplusplus
extern "C" {
//"C" {
#endif

UINT SetupPort(LPSTR PortName);
UINT SendString(char *command);
UINT ClosePort();
#ifdef __cplusplus
}
#endif

#endif



**********************serial.c
/* serial.c */
#include<windows.h>
#include<windowsx.h>
#include<winbase.h>
#include<string.h>
#include<time.h>
#include"serial.h"

HANDLE hCom;
DCB dcb;
COMMTIMEOUTS CommTimeouts;

UINT SetupPort(LPSTR PortName)
{
/* char temp[6];*/
hCom = CreateFile(PortName, (GENERIC_READ | GENERIC_WRITE), 0, NULL,
OPEN_EXISTING, 0, NULL);
if(hCom == INVALID_HANDLE_VALUE)
return CANT_OPEN_PORT;
SetupComm(hCom,1024,32768);
GetCommState(hCom, &dcb);
BuildCommDCB("baud=9600 parity=N data=8 stop=1", &dcb);
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fOutxCtsFlow = TRUE;
dcb.fOutxDsrFlow = TRUE;
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;

CommTimeouts.ReadIntervalTimeout = 500;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.ReadTotalTimeoutConstant = 100;
CommTimeouts.WriteTotalTimeoutMultiplier = 250;
CommTimeouts.WriteTotalTimeoutConstant = 1;

SetCommTimeouts(hCom, &CommTimeouts);
SetCommState(hCom, &dcb);

PurgeComm(hCom, (PURGE_TXCLEAR | PURGE_TXABORT | PURGE_RXABORT |
PURGE_RXCLEAR));
return SUCCESS;
}

UINT SendString(char *command)
{
DWORD BytesSent, Size;
BOOL fSuccess;

Size = strlen(command);
command[Size] = '\0';

fSuccess = WriteFile(hCom, command, Size, &BytesSent,NULL);
if(!fSuccess)
{
GetLastError();
Sleep(5000);
fSuccess = WriteFile(hCom, command, Size, &BytesSent,NULL);
} else if(BytesSent != Size)
{
Sleep(1000);
fSuccess = WriteFile(hCom, &(command[BytesSent]), Size-BytesSent,
&BytesSent,NULL);
}
if(fSuccess) return SUCCESS;
return CANT_WRITE_TO_PORT;
}

UINT ClosePort()
{
if(CloseHandle(hCom)) // handle to object to close
return(SUCCESS);
return(CANT_CLOSE_PORT);
}




**************MYmain.cpp
#include <iostream>
#include "serial.c"

using namespace std;

int main()
{
int a,b,c;

a = SetupPort("COM5");
//SetupPort(LPSTR PortName);
b = SendString("AT&0");
c = ClosePort();

return 0;
}


*****************ERRORS
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.c
Linking...
serial.obj : error LNK2005: _SetupPort already defined in main.obj
serial.obj : error LNK2005: _SendString already defined in main.obj
serial.obj : error LNK2005: _ClosePort already defined in main.obj
serial.obj : error LNK2005: "struct _COMMTIMEOUTS CommTimeouts"
(?CommTimeouts@@3U_COMMTIMEOUTS@@A) already defined in main.obj
serial.obj : error LNK2005: "void * hCom" (?hCom@@3PAXA) already defined in
main.obj
serial.obj : error LNK2005: "struct _DCB dcb" (?dcb@@3U_DCB@@A) already
defined in main.obj
Debug/main.exe : fatal error LNK1169: one or more multiply defined symbols
found
Error executing link.exe.

main.exe - 7 error(s), 0 warning(s)

Any help will be greatly appreciated.

Sonoman
 
S

Sonoman

Sonoman said:
Thanks for your response. I had included the h file before and I get the
exact same error message.

I apologize, this is the error I get when I include the .h file also:

--------------------Configuration: serial - Win32 Debug--------------------
Compiling...
main.c
c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal
error C1189: #error : "eh.h is only for C++!"
Error executing cl.exe.

serial.exe - 1 error(s), 0 warning(s)
 
A

Alf P. Steinbach

* Sonoman:
I apologize, this is the error I get when I include the .h file also:

--------------------Configuration: serial - Win32 Debug--------------------
Compiling...
main.c
c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal
error C1189: #error : "eh.h is only for C++!"
Error executing cl.exe.

serial.exe - 1 error(s), 0 warning(s)

Note that the error message mentions [main.c], while in your posting
you referred to [main.cpp]. C is not C++. That's what the message says.
 
J

John Harrison

Sonoman said:
I apologize, this is the error I get when I include the .h file also:

serial.h is correct, serial.c is wrong.

You are not really struggling with the C++ language, the code has been
written for you. Instead you are struggling with how to use your compiler
correctly. That is not an issue for this group which discusses the C++
language not the vagaries of different compilers. I suggest you take this
query somewhere it is on topic, for
instance.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,808
Messages
2,569,686
Members
45,452
Latest member
AmberLayde

Latest Threads

Top