Errors in Serial Communication Programming
Hi everyone! I'm a new member here.
First of all I would like to address my problem on C++ programming in serial communication. My program is expected to receive float number from the serial port COM7 and show it on exe window. But while I'm trying to compile it, there are quite a lot of errors.
Below is the code that I've written:
Code:
#include <iostream>
using namespace std;
#include "serial.h"
void main() {
CSerial serial;
if (serial.Open(2, 9600))
{
char* lpBuffer = new char[500];
float NumberRead = serial.ReadData(lpBuffer, 500);
cout<<"Temperature = "<<NumberRead<<endl;
delete []lpBuffer;
}
else
AfxMessageBox("Failed to open port!");
}
Is this program written in correct way to receive data from serial port? Any help and advice is highly appreciated. Thanks!
Re: Errors in Serial Communication Programming
Quote:
while I'm trying to compile it, there are quite a lot of errors.
My crystal ball broke down, waiting for a new one... so in the mean time, it would be nice if you would post those errors.
Code:
float NumberRead = serial.ReadData(lpBuffer, 500);
The ReadData doesn't return a float.
Re: Errors in Serial Communication Programming
There are 53 errors. The first error is in the line "BOOL Open( int nPort = 7, int nBaud = 9600 );" in below code (syntax error : missing ';' before identifier 'Open')
Below is my code for source Serial.h which I had downloaded from other site:
Code:
#ifndef __SERIAL_H__
#define __SERIAL_H__
#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04
#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13
class CSerial
{
public:
CSerial();
~CSerial();
BOOL Open( int nPort = 7, int nBaud = 9600 ); //first error here
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;
};
#endif
Hope that you have idea on how to rectify it. Thanks!
Re: Errors in Serial Communication Programming
Code:
#include <iostream>
using namespace std;
#include "serial.h"
CSerial serial;
if (serial.Open(2, 9600))
{
char* lpBuffer = new char[500];
float NumberRead = serial.ReadData(lpBuffer, 500);
cout<<"Temperature = "<<NumberRead<<endl;
delete []lpBuffer;
}
else
AfxMessageBox("Failed to open port!");
Is that all the code you've written? Where's
int main()?
Re: Errors in Serial Communication Programming
Sorry, I forget to paste "void main()" before "CSerial serial;" into this post. But it doesn't matter, the error is still the same.
Re: Errors in Serial Communication Programming
Quote:
The first error is in the line "BOOL Open( int nPort = 7, int nBaud = 9600 );" in below code (syntax error : missing ';' before identifier 'Open')
type 'BOOL' is undefined. 'BOOL' is a MS type, so you need to include the right file that contains the definition, or change it to 'bool'.
Re: Errors in Serial Communication Programming
should really be int main() for c++ programs.