Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm sort of new to C++. In a current school project, we need to use a sensor to activate an RFID reader. We're using an arduino microcontroller to send serial data when the sensor is activated. I used the following program to read serial data using C++:

C#
#include "stdafx.h"
#include <iostream>
#include "windows.h"

#using <System.dll>
using namespace std;
using namespace System;
using namespace System::IO::Ports;

ref class PortDataReceived
{
public:
    static void Main()
    {
        SerialPort^ mySerialPort = gcnew SerialPort("COM15");

        mySerialPort->BaudRate = 9600;
        mySerialPort->Parity = Parity::None;
        mySerialPort->StopBits = StopBits::One;
        mySerialPort->DataBits = 8;
        mySerialPort->Handshake = Handshake::None;

        mySerialPort->Open();
        mySerialPort->DataReceived += gcnew SerialDataReceivedEventHandler(DataReceivedHandler);
        Console::WriteLine("Press any key to continue...");
        Console::WriteLine();
        Console::ReadKey();
        mySerialPort->Close();
    }

private:
    static void DataReceivedHandler(Object^ sender, SerialDataReceivedEventArgs^ e)
    {
        SerialPort^ sp = (SerialPort^)sender;
        String^ indata = sp->ReadExisting();
        Console::Write("Data Received:");
        Console::Write(indata);
    }
};

int main()
{
    PortDataReceived::Main();
    return 0;
}



I got that from cplusplus.com.

My problem is that I need the program to proceed to the RFID functions after a certain serial data is detected, that's why we used a serial event handler. What I can't figure out is how to terminate the serial event handling mechanism once it detects serial data without having to press any button (ReadKey()). If i remove readkey, the program does not run as I want it to, that is, it does not wait for the serial data anymore. It just closes. What should be done???

I can use a "polling" function to wait for the serial data, but an event handler would be much more efficient than simply looping and waiting for the right data.
Posted
Comments
johannesnestler 20-Sep-11 3:27am    
The ReadKey - call just means "wait". so in reality you have to implement a kind of endless loop, checking for a break condition (maybe some special key was pressed, the desired data was received, ...). After break condition was met, stop listening for data, and exit the "loop". It would be best to do that all in a special (background) thread. So you can restart the data listener again from you application (if you build the logic for this)
Naniw 20-Sep-11 7:15am    
I was thinking of replacing the Readkey() with a condition but when I tried it didn't work... >_< Can you give a suggestion? Like, when the data received from the serial port is "1", the program goes straight to a function after PortDataReceived::Main(); in the main function. How do I do that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900