// ================================= // Main // ================================= int main(int argc, char* argv[]) { info("Attempting to connect to Arduino"); Serial port; port.connect("COM4"); if (port._connected) { info("Connected to Arduino"); } char data[BUFFER_SIZE]; // Pre-allocate a buffer flushBuffer(data, BUFFER_SIZE); int dataLength = BUFFER_SIZE; // How many bytes to read int readResult = 0; // How many bytes we read from the serial port while (port._connected) { readResult = port.read(data, dataLength); if (readResult > -1) { printf("RECV: \'%s\' (%i)\n", data, readResult); flushBuffer(data, BUFFER_SIZE); } } return 0; }
int main() { Serial serial; if(serial.connect("/dev/ttyACM0", 57600, SYNC_SEQ, BLK_READ) < 0) { std::cout << "Errore inizializzazione serial\n"; return -1; } int i = 0; serial.flush_io(); while(i < 10) { std::cout << "reading...\n"; char * buff = NULL; int a = serial.serial_read(&buff, READ_ALL); if(a == 0) { std::cout << "Ricevuto\n"; for(int j = 0; j< 58; j++) { std::cout << (char)buff[j]; } std::cout << "\n"; } else{ std::cout << "Non ricevuto\n"; } serial.flush_io(); free(buff); i++; } serial.disconnect(); return 0; }
int main() { Datagram dg; Serial_t serialConfig; t=0; serialConfig.baud_rate = 9600; serialConfig.bits = 8; serialConfig.parity = 0; serialConfig.stop_bits = 1; serialConfig.device = "ttyUSB0"; serial.setConfig(serialConfig); if (serial.connect() == -1) { qDebug() << "Serial connection error!"; exit(1); } qDebug() << "Serial connected!"; qDebug() << "Ping..."; ping(); qDebug() << "DONE"; bool remain = true; while (remain) { qDebug() << "Inquiry or ok?"; receiveDatagram(&serial, &dg); switch (dg.type) { case REQUEST: switch (dg.id) { case INQUIRY: qDebug() << "Inquiry Request..."; inquiry(); qDebug() << "DONE"; break; default: qDebug() << "Unknown"; break; } break; case RESPONSE: switch (dg.id) { case OK: qDebug() << "OK"; remain = false; break; default: qDebug() << "Unknown"; break; } break; default: qDebug() << "Unknown"; break; } } receiveDatagram(&serial, &dg); if (dg.type != REQUEST || dg.id != CONNECT_TO) { qDebug() << "Expected connection!"; qDebug() << "QUIT"; exit(1); } qDebug() << "Connecting to: " << *dg.data; sendDatagram(&serial, RESPONSE, SUCCESS); qDebug() << "DONE"; loop(); return 0; }