Ejemplo n.º 1
0
int main(int argc, char* argv[])
{
    SerialOptions options;
    options.setDevice("/dev/ttyUSB0");
    options.setBaudrate(115200);
    options.setTimeout(seconds(3));
    //options.setFlowControl(SerialOptions::software);
    //options.setParity(SerialOptions::even);
    //options.setCsize(7);
    SerialStream serial(options);
    serial.exceptions(ios::badbit | ios::failbit); //Important!
    serial<<"Hello world"<<endl;
    try {
        string s;
        //serial>>s;
        getline(serial,s);
        cout<<s<<endl;
    catch(TimeoutException&)
    {
        serial.clear(); //Don't forget to clear error flags after a timeout
        cerr<<"Timeout occurred"<<endl;
    }
    return 0;
}
Ejemplo n.º 2
0
SerialDeviceImpl::SerialDeviceImpl(const SerialOptions& options)
        : io(), port(io), timer(io), timeout(options.getTimeout()),
        result(resultError), bytesTransferred(0), readBuffer(0),
        readBufferSize(0)
{
    try {
        //For this code to work, there should always be a timeout, so the
        //request for no timeout is translated into a very long timeout
        if(timeout==posix_time::seconds(0)) timeout=posix_time::hours(100000);

        port.open(options.getDevice());//Port must be open before setting option

        port.set_option(serial_port_base::baud_rate(options.getBaudrate()));

        switch(options.getParity())
        {
            case SerialOptions::odd:
                port.set_option(serial_port_base::parity(
                        serial_port_base::parity::odd));
                break;
            case SerialOptions::even:
                port.set_option(serial_port_base::parity(
                        serial_port_base::parity::even));
                break;
            default:
                port.set_option(serial_port_base::parity(
                        serial_port_base::parity::none));
                break;
        }

        port.set_option(serial_port_base::character_size(options.getCsize()));

        switch(options.getFlowControl())
        {
            case SerialOptions::hardware:
                port.set_option(serial_port_base::flow_control(
                        serial_port_base::flow_control::hardware));
                break;
            case SerialOptions::software:
                port.set_option(serial_port_base::flow_control(
                        serial_port_base::flow_control::software));
                break;
            default:
                port.set_option(serial_port_base::flow_control(
                        serial_port_base::flow_control::none));
                break;
        }

        switch(options.getStopBits())
        {
            case SerialOptions::onepointfive:
                port.set_option(serial_port_base::stop_bits(
                        serial_port_base::stop_bits::onepointfive));
                break;
            case SerialOptions::two:
                port.set_option(serial_port_base::stop_bits(
                        serial_port_base::stop_bits::two));
                break;
            default:
                port.set_option(serial_port_base::stop_bits(
                        serial_port_base::stop_bits::one));
                break;
        }
    } catch(std::exception& e)
    {
        cout << "Fehler in Serial Options" <<endl;
        throw ios::failure(e.what());
    }
}
Ejemplo n.º 3
0
//! perform on-line full analysis of a data stream (classifier inside)
//! @param &one_device: reference to the device used for collecting the data
//! @param port:	USB port for data acquisition
//! @return:		---
void SensingBracelet::onlineSensingBracelet(Device &one_device, char* port)
{
    bool flag = false;          // flag on updateInterval method (true --> Full; false --> Simple)

	int nSamples = 0;			// number of acquired samples
	string sample;				// current sample acquired via USB
	mat actsample;				// current sample in matrix format
	int ax, ay, az;				// accelerometer current sample components
	int gx, gy, gz;				// gyroscope current sample components
	char dev;					// flag --> device type
	string motion;				// flag --> level of motion at the wrist
	vector<float> poss;			// models possibilities
	vector<float> past_poss;	// models previous possibilities

	string waste = " ";

	// instantiate and initialize a Classifier
	string dF = datasetFolder.substr(0,datasetFolder.length()-1);
	dF = dF.substr(9);
	Classifier hC(dF);
	mat window = zeros<mat>(hC.window_size, 3);
	mat gravity = zeros<mat>(hC.window_size, 3);
	mat body = zeros<mat>(hC.window_size, 3);

	// initialize the possibilities and past possibilities
	for(int i = 0; i < nbM; i++)
	{
		poss.push_back(0);
		past_poss.push_back(0);
	}
	
	// set up the serial communication (read-only)
	SerialOptions options;
	options.setDevice(port);
	options.setBaudrate(9600);
	options.setTimeout(seconds(1));
	options.setParity(SerialOptions::noparity);
	options.setCsize(8);
	options.setFlowControl(SerialOptions::noflow);
	options.setStopBits(SerialOptions::one);
	SerialStream serial(options);
	serial.exceptions(ios::badbit | ios::failbit);

	// extract known activities intervals from the stream of raw acceleration data
	while(peiskmt_isRunning())
	{
		try
		{
			// read the current sample
			getline(serial,sample);
			istringstream stream(sample);
			stream >>dev >>ax >> ay >>az >>gx >>gy >>gz >>motion;
			actsample <<ax <<ay <<az;
			//DEBUG: cout<<"Acquired: " <<ax <<" " <<ay <<" " <<az <<" ";

			// update the window of samples to be analyzed
			hC.createWindow(one_device.codedRange, one_device.sensingRange, actsample, window, hC.window_size, nSamples);
			if (nSamples >= hC.window_size)
			{
				// analyze the window and compute the models possibilities
				for(int i = 0; i < nbM; i++)
					past_poss[i] = poss[i];
				hC.analyzeWindow(one_device.samplingFrequency, window, gravity, body);
				hC.compareAll(gravity, body, poss);

				// publish the dynamic tuples
				hC.publishDynamic(poss);
                if (flag == true)
                {
                    // perform accurate a-posteriori activity analysis
				    for(int i = 0; i < nbM; i++)
					    updateIntervalFull(i,nSamples,poss[i],past_poss[i],1,waste);
                }
                else
                {
                    // perform quick-and-dirty activity analysis
				    for(int i = 0; i < nbM; i++)
					    updateIntervalSimple(i,nSamples,poss[i],past_poss[i]);
                }
			}
		}
		catch(TimeoutException&)
		{
			serial.clear();
			cerr<<"Timeout occurred"<<endl;
		}
	}
}