|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#21 | |
|
Senior Member
Iscritto dal: Jul 2002
Città: Milano
Messaggi: 19149
|
Quote:
|
|
|
|
|
|
|
#22 | |
|
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 |
|
|
|
|
|
|
#23 | |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
Quote:
si ti allego il codice ma ho modificato un bel po di cose e ora mi pare funzioni adeguatamente: 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>
#include <sys/poll.h>
struct termios stdin_saved_attributes;
struct termios tty_saved_attributes;
int tty_fd;
unsigned char risp;
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
new_attributes.c_cflag |=CLOCAL;
new_attributes.c_cflag &= ~CRTSCTS;
//new_attributes.c_iflag &= ~(IXON IXOFF);
// 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);
}
//procedura di attesa per risposta da stampante
int att()
{
int flag=0;
struct pollfd pfd;
pfd.fd = tty_fd;
pfd.events = POLLIN;
//******attesa risposta*****//
if (poll(&pfd, 1, 1000 /* timeout ms */)==0)
{
fprintf(stderr, "Timeout!\n");
flag=1;
}
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);
}
printf("%d %d",flag,risp);
return (flag);
}
//procedura per il controllo degli stati della stampante
int main(void)
{
unsigned char str[10];
int flag=0;
/*struct pollfd pfd;
pfd.fd = tty_fd;
pfd.events = POLLIN;
*/
if((tty_open("/dev/ttyS2"))==-1)
{ perror("Porta non aperta!");
exit (-1);
}
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);
flag=att();
//printf("%d\n", flag);
//printf("%d\n",risp);
if (flag==1)
if(read(tty_fd,&risp,1)<0)
perror("Read");
//printf("%d\n",risp);
if(risp&0x20)
printf("Manca Carta!!\n");
/*else
printf("Carta presente\n");*/
/*sensore rotolo*/
str[0]=0x10;
str[1]=0x04;
str[2]=4;
str[4]='\0';
tty_printf(str);
flag=att();
//printf("%d\n",flag);
//printf("%d",risp);
if (flag==1)
if(read(tty_fd,&risp,1)<0)
perror("Read");
printf("%d\n",risp);
if((risp&0x0c)==0x0c)
printf("Carta in esaurimento!!\n");
/*if((risp&0x0c)==0)
printf("Carta Ok\n");
close(tty_fd);
}
|
|
|
|
|
|
|
#24 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Roma
Messaggi: 15625
|
Sembra sostanzialmente corretto, non dovrebbe andarti in timeout...
__________________
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 |
|
|
|
|
|
#25 |
|
Junior Member
Iscritto dal: May 2007
Messaggi: 17
|
non non ci va! grazie per l'aiuto a buon rendere!
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 13:23.




















