|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
Porta seriale in c sotto linux
ciao a tutti!
avrei bisogno di una mano perche non riesco a ricevere dati dalla seriale ma soltanto a mandarli in questo programmino sto mandado dei comandi ad una stampante, in risposta devo ricevere un byte che mi serve per sapere lo stato della stampante ma al momento della ricezione mi viene restituito questo errore: Resource temporarily unavailable Codice:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
struct termios stdin_saved_attributes;
struct termios tty_saved_attributes;
int tty_fd;
int tty_open(char* tty_dev)
{
struct termios new_attributes;
tty_fd = open(tty_dev,O_RDWR| O_NOCTTY | O_NONBLOCK);
if (tty_fd<0)
{
return -1;
}
else
{
tcgetattr(tty_fd,&tty_saved_attributes);
tcgetattr(tty_fd,&new_attributes);
// Set the new attributes for the serial port
// c_cflag
new_attributes.c_cflag |= CREAD; // Enable receiver
new_attributes.c_cflag |= B9600; // Set baud rate
new_attributes.c_cflag &= ~PARENB;
new_attributes.c_cflag &= ~CSTOPB;
new_attributes.c_cflag &= ~CSIZE;
new_attributes.c_cflag |= CS8;
// 8 data bit
// c_iflag
new_attributes.c_iflag |= IGNPAR; // Ignore framing errors and parity errors.
// c_lflag
new_attributes.c_lflag &= ~(ICANON); // DISABLE canonical mode.
// Disables the special characters EOF, EOL, EOL2,
// ERASE, KILL, LNEXT, REPRINT, STATUS, and WERASE, and buffers by lines.
new_attributes.c_lflag &= ~(ECHO);// DISABLE this: Echo input characters.
new_attributes.c_lflag &= ~(ECHOE);//DISABLE this: If ICANON is also set,the ERASE character erases the preceding input
// character, and WERASE erases the preceding word.
new_attributes.c_lflag &= ~(ISIG); // DISABLE this: When any of the characters INTR, QUIT, SUSP,
// or DSUSP are received, generate the corresponding signal.
new_attributes.c_cc[VMIN]=1; // Minimum number of characters for non-canonical read.
new_attributes.c_cc[VTIME]=0; // Timeout in deciseconds for non-canonical read.
tcsetattr(tty_fd, TCSANOW, &new_attributes);
}
return tty_fd;
}
// Serial version of printf
void tty_printf(char *format, ...)
{
va_list argptr;
char buffer[200];
va_start(argptr,format);
vsprintf(buffer,format,argptr);
va_end(argptr);
write(tty_fd,buffer,strlen(buffer));
}
void termination_handler (int signum)
{
tcsetattr(STDIN_FILENO,TCSANOW,&stdin_saved_attributes);
if (tty_fd>0) tcsetattr (tty_fd,TCSANOW,&tty_saved_attributes);
close(tty_fd);
printf("Exit\n");
exit(0);
}
int main(void)
{
unsigned char str[10];
unsigned char risp;
int a,i,n;
//char str2[5];
//char str3[3];
tty_open("/dev/ttyS0");
if (signal (SIGINT, termination_handler) == SIG_IGN) signal (SIGINT, SIG_IGN);
if (signal (SIGHUP, termination_handler) == SIG_IGN) signal (SIGHUP, SIG_IGN);
if (signal (SIGTERM, termination_handler) == SIG_IGN) signal (SIGTERM, SIG_IGN);
//stato carata
str[0]=0x10;
str[1]=0x04;
str[2]=17;
str[3]='\0';
tty_printf(str);
i=0;
//printf("%d\n",risp);
while(1)
{
if(read(tty_fd,risp,1)<=0)
//perror(errno);
perror("Errore in ricezione\n");
break;
i++;
// printf("%d\n",risp);
}
printf("%d\n",risp);
printf("%d\n",i);
close(tty_fd);
}
|
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
Usa la poll per attendere l'arrivo dei byte. Codice:
if(read(tty_fd,risp,1)<=0) Codice:
tcsetattr(STDIN_FILENO,TCSANOW,&stdin_saved_attributes);
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 Ultima modifica di ilsensine : 14-05-2007 alle 15:56. |
|
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
scusa ma non sono molto pratico delle porte seriali non ho ben capito che cosa mi hai detto di cambiare....
|
|
|
|
|
|
#4 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
lo inizializzo cone i settaggi che imposto al momento dell'apertura della porta tu dici che devo settare inizialmente degli altri valori? io una vola aperta la porta vado a scrivere i vari settaggi e poi li salvo Ultima modifica di lammeBN : 14-05-2007 alle 16:06. |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Se apri un fd qualsiasi (non solo una seriale) indicando O_NONBLOCK, operi in modalità non bloccante -- in questo caso la read non attende l'arrivo dei dati, se non ci sono ritorna immediatamente con errno==EAGAIN (Resource temporarily unavailable).
Quindi prima della read devi attendere l'arrivo dei dati, con qualcosa tipo questo: Codice:
#include <sys/poll.h>
...
struct pollfd pfd;
pfd.fd = tty_fd;
pfd.events = POLLIN;
if (poll(&pfd, 1, 1000 /* timeout ms */)==0)
fprintf(stderr, "Timeout!\n");
else if ((pfd.revents&POLLIN)==0) {
fprintf(stderr, "I/O error (%04x)\n", pfd.revents);
...
} else {
if (read(tty_fd, &risp, 1)<0) {
perror("read");
...
} else
fprintf(stderr, "Ricevuto: 0x%02x\n", risp);
}
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Non mi riferisco a tty_saved_attributes, ma stdin_saved_attributes.
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Ti consiglio di impostare anche CLOCAL. Non credo che devi utilizzare le linee di controllo per i modem.
Accertati anche di usare lo stesso tipo di controllo di flusso del dispositivo.
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
#8 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
guarda io sto programmando una scheda con un sistema linux embedded e le funzioni per l'uso della porta seriale erano gia fatte dai produttori della scheda io x ora ho solo usato le loro funzioni e sto cercando di capire come funzionino dato che come dicevo prima non avevo mai usato delle porte seriali prima d'ora
|
|
|
|
|
|
#9 | |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
Quote:
scusa ma CLOCAL a cosa serve e come dovrei impostarla? |
|
|
|
|
|
|
#10 | |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
Quote:
|
|
|
|
|
|
|
#11 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
Non ho cambiato nulla a parte aver aggiunto un delay di 1sec prima del read e ora l'errore non è piu quello precedente ma Bad address
|
|
|
|
|
|
#12 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
|
#13 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Serve a disabilitare le linee di controllo specifiche per i modem (DTR e DCD). Improbabile che quel dispositivo le supporti.
Si imposta tra i c_cflag.
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
#14 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
in effetti si ma io alla fine dei conti non ho mica capito bene cosa dovrei cambiare e/o aggiongere al mio codice
|
|
|
|
|
|
#15 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Con o senza controllo di flusso hardware? (linee RTS e CTS)
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
#16 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Quote:
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
|
#17 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
|
|
|
|
|
|
#18 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Allora accertati anche che il controllo di flusso hw e quello sw siano disabilitati. Il primo si disabilita tra i c_cflags:
new_attributes.c_cflag &= ~CRTSCTS; il secondo tra i c_iflags: new_attributes.c_iflag &= ~(IXON I XOFF);
__________________
0: or %edi, %ecx; adc %eax, (%edx); popf; je 0b-22; pop %ebx; fadds 0x56(%ecx); lds 0x56(%ebx), %esp; mov %al, %al andeqs pc, r1, #147456; blpl 0xff8dd280; ldrgtb r4, [r6, #-472]; addgt r5, r8, r3, ror #12 |
|
|
|
|
|
#19 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
si setta cosi??
new_attributes.c_cflag |=CLOCAL; qui non mi prende l'istruzione mi da un errore: parse error before numeric constant new_attributes.c_iflag &= ~(IXON IXOFF); Io cmq ho fatto un errore nel passaggio dei parametri alla read avevo dimenticato il & prima di risp Ultima modifica di lammeBN : 14-05-2007 alle 17:13. |
|
|
|
|
|
#20 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
ho usato il tuo codice in questa maniera:
Codice:
if (poll(&pfd, 1, 1000 /* timeout ms */)==0)
fprintf(stderr, "Timeout!\n");
else
if ((pfd.revents&POLLIN)==0)
{
fprintf(stderr, "I/O error (%04x)\n", pfd.revents);
//...
}
else
{
if (read(tty_fd, &risp, 1)<0)
{
perror("read");
//...
}
else
fprintf(stderr, "Ricevuto: 0x%02x\n", risp);
}
read(tty_fd,&risp,1); //QUESTA READ E LE PRINT LE HO LASCIATE PER //ERRORE
perror("Read");
printf("%d\n",risp);
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:50.




















