Domanda: Non puoi installare python e scrivere un programma in python?
Comunque in C (in unix ancora meglio) i dispositivi appaiono come dei file e si trovano sotto /dev, le seriali pure o no cominciano con tty, nel caso di semplice convertitore usb seriale tipo Arduino UNO il device si chiama /dev/ttyACM0.
Visto che i device sono file, si posso aprire e chiudere con open(), ma visto che sono dei file particolari ci sono altre funzioni per impostare la seriale.
Prova questo codice e dimmi se lavora.
#include <unistd.h>
#include <stdio.h>
#include <termios.h>
#include <sys/fcntl.h>
int main( int argc, char *argv[] )
{
struct termios termOptions;
char port[1024];
int ttyFid;
if( argc == 2 )
{
// Copy the port from the argument
strcpy( port, argv[1] );
printf( "Read port name: %s\n", port );
}
else
{
// No argument or incorrect number of arguments read:
printf( "Usage: setSer /dev/ttySX\n" );
return -1;
}
// Open the tty:
ttyFid = open( port, O_RDWR );
if (ttyFid == -1)
{
printf( "Error unable to open port: %s\n", port );
return -1;
}
// Get the current options:
tcgetattr( ttyFid, &termOptions );
// Set the input/output speed to 921.6kbps
cfsetispeed( &termOptions, B921600 );
cfsetospeed( &termOptions, B921600 );
// Now set the term options (set immediately)
tcsetattr( ttyFid, TCSANOW, &termOptions );
// All done
printf( "Done, port speed set on: %s\n", port );
}
Ciao.