PDA

View Full Version : [c++][linux]lettura seriale bilancia


oltremare
11-08-2011, 06:06
Ciao, io non ho grandi capacità nella programmazione epperò sto tentando di
interfacciare una bilacia al computer (mi barterebbe leggere i valori di pesata e visualizzare
un paio di moltiplicazioni.
Ho un cavo con adattatore seriale USB cinese ma che funziona infatti mi risulta /dev/ttyUSB0
Ho ripreso questo codice di esempio dalla documentazione di libserial
compila e linka (g++ -lserial nomefile)


#codice ripreso da un esmpio incluso nella doc di libserial


#include <SerialStream.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>

int
main( int argc,
char** argv )
{
//
// Open the serial port.
//
using namespace LibSerial ;
SerialStream serial_port ;
serial_port.Open( "/dev/ttyUSB0" ) ;
if ( ! serial_port.good() )
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Error: Could not open serial port."
<< std::endl ;
exit(1) ;
}
//
// Set the baud rate of the serial port. PER ME 1200
//
serial_port.SetBaudRate( SerialStreamBuf::BAUD_1200 ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not set the baud rate." << std::endl ;
exit(1) ;
}
//
// Set the number of data bits. DOVREBBE ANDARE BENE COSI'
//
serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not set the character size." << std::endl ;
exit(1) ;
}
//
// Disable parity. PER ME DISPARI
//
serial_port.SetParity( SerialStreamBuf::PARITY_ODD ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not disable the parity." << std::endl ;
exit(1) ;
}
//
// Set the number of stop bits. VA BENE COSI'
//
serial_port.SetNumOfStopBits( 1 ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not set the number of stop bits."
<< std::endl ;
exit(1) ;
}
//
// Turn off hardware flow control. DOVREBBE ANDARE BENE
//
serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not use hardware flow control."
<< std::endl ;
exit(1) ;
}
//
// Do not skip whitespace characters while reading from the
// serial port.
//
// serial_port.unsetf( std::ios_base::skipws ) ;
//
// Wait for some data to be available at the serial port.
//
while( serial_port.rdbuf()->in_avail() == 0 )
{
usleep(100) ;
}
//
// Keep reading data from serial port and print it to the screen.
//
while( serial_port.rdbuf()->in_avail() > 0 )
{
char next_byte;
serial_port.get(next_byte);
std::cerr << std::hex << static_cast<int>(next_byte) << " ";
usleep(100) ;
}
std::cerr << std::endl ;
return EXIT_SUCCESS ;
}


Il programma apre correttamente il dispositivo e si ferma al ciclo while di attesa dati.
Il problema è come posso investigare il funzionamento del programma cosa testare per sapere dove è
l'errore?