#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
int openSerialPort(const char *portName) {
int serialPort = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);
if (serialPort == -1) {
perror("Error al abrir el puerto serie");
return -1;
}
// Configuración del puerto serie (puedes ajustar según tus necesidades)
struct termios options;
tcgetattr(serialPort, &options);
cfsetispeed(&options, B9600); // Velocidad de baudios
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD); // Habilitar recepción y configuración local
tcsetattr(serialPort, TCSANOW, &options);
return serialPort;
}
void closeSerialPort(int serialPort) {
close(serialPort);
}
int main() {
const char *portName = "/dev/ttyUSB0"; // Reemplaza con tu puerto USB
int serialPort = openSerialPort(portName);
if (serialPort != -1) {
// Realiza operaciones de lectura y escritura en el puerto serie
// ...
closeSerialPort(serialPort);
}
return 0;
}