本文整理汇总了C++中Serial::connect方法的典型用法代码示例。如果您正苦于以下问题:C++ Serial::connect方法的具体用法?C++ Serial::connect怎么用?C++ Serial::connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Serial
的用法示例。
在下文中一共展示了Serial::connect方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
// =================================
// 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;
}
示例2: main
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;
}
示例3: main
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;
}