Condividi:        

Gestione Porta Seriale

Per tutti i tuoi problemi Linux entra qui e risolvili!

Moderatori: Anthony47, gunter, zendune

Gestione Porta Seriale

Postdi marcozenon » 21/01/11 15:34

Buongiorno. Ho cercato nei vecchi post qualcosa che potesse servirmi, ma non l'ho trovato; nel caso, scusatemi se replico una richiesta già pervenuta (spero mi risponderete lo stesso).
Con il mio pc Linux, devo fare un programmino in C che legga ciò che arriva sulla seriale ed eventualmente spedire qualcosa indietro sulla seriale. E' di per sè un prg semplice e ciò che sto per postare è ancor più semplificato.

int set_port(void)
{
struct termios options;
int fd; /* File descriptor for the port */
/* Get the current options for the port... */
tcgetattr(fd, &options);
/* Set the baud rates to 4800... */
cfsetispeed(&options, B4800);
cfsetospeed(&options, B4800);
/* No parity (8N1) */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* Enable the receiver and set local mode... */
options.c_cflag |= (CLOCAL | CREAD);
/* Set the new options for the port... */
tcsetattr(fd, TCSANOW, &options);
}

int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) { /* Could not open the port. */
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else {
fcntl(fd, F_SETFL, 0);
}
return (fd);
}

int main (void)
{
int com, num_caratteri;
unsigned char buffer[256];
set_port();
if (com=open_port()== -1) {
printf("Impossibile aprire la porta.\r\n");
return 1;
}

printf ("\r\n");
num_caratteri = read(com, buffer, 256); // legge seriale
printf ("dati in arrivo %s \r\n", buffer);
printf ("dati %d", num_caratteri);
printf ("\r\n");
write(com, "Pippo\r\n", 7); // scrive seriale.
printf ("\r\n");
return 0;
}

La comunicazione mi serve in 4800 bps e (8-N-1).
Pensavo fosse giusto così, invece sembra che invece di dialogare con la seriale, dialoghi con la MIA TASTIERA... nel senso che mentre attende i dati dalla seriale (comando read) anche se arriva qualcosa dalla seriale non lo registra, mentre se scrivo con la mia tastiera SI... Invece col comando write, scrive sulla mia console, ma non manda nulla sulla seriale...
Dove sbaglio?
Grazie, Marco
marcozenon
Utente Junior
 
Post: 40
Iscritto il: 03/12/06 16:14

Sponsor
 

Torna a Software Linux


Topic correlati a "Gestione Porta Seriale":


Chi c’è in linea

Visitano il forum: Nessuno e 13 ospiti