Thread: How to receive data from serial port?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    How to receive data from serial port?

    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; 04-23-2010 at 03:04 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Most likely not.

    I have never used the library, but by convention, the number returned by serial.ReadData is probably the number of bytes read.

    The ACTUAL data is probably in lpBuffer ([0] to [NumberRead - 1]).

    NumberRead should probably be a size_t, or int (you can't read half a byte).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comodo Firewall Vulnerability (Port 0)
    By Mario F. in forum Tech Board
    Replies: 0
    Last Post: 11-11-2009, 08:56 AM
  2. Serial port: How to know when data has been completely sent
    By arjunajay in forum Windows Programming
    Replies: 8
    Last Post: 03-25-2009, 08:18 AM
  3. Replies: 3
    Last Post: 02-29-2008, 01:29 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM