CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    11

    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!
    Last edited by coolrox86; April 23rd, 2010 at 03:59 PM.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Errors in Serial Communication Programming

    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.

  3. #3
    Join Date
    Apr 2010
    Posts
    11

    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!

  4. #4
    Join Date
    Jun 2005
    Posts
    315

    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()?

  5. #5
    Join Date
    Apr 2010
    Posts
    11

    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.

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Errors in Serial Communication Programming

    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'.

  7. #7
    Join Date
    Jun 2005
    Posts
    315

    Re: Errors in Serial Communication Programming

    "void main()"
    should really be int main() for c++ programs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured