Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Non-Canonical Input Processing*/
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <stdio.h>
- #include <string.h>
- #define BAUDRATE B38400
- #define MODEMDEVICE "/dev/ttyS0"
- #define _POSIX_SOURCE 1 /* POSIX compliant source */
- #define FALSE 0
- #define TRUE 1
- #define F 0x7e
- #define A 0x03
- #define S 0x03
- #define U 0x07
- volatile int STOP=FALSE;
- int main(int argc, char** argv)
- {
- // CREATING TRAMA
- unsigned char setup[5];
- setup[0]=F;
- setup[1]=A;
- setup[2]=S;
- setup[3]=setup[1]^setup[2];
- setup[4]=F;
- int fd,c, res;
- struct termios oldtio,newtio;
- int i=0, sum = 0, speed = 0;
- /*if ( (argc < 2) ||
- ((strcmp("/dev/ttyS0", argv[1])!=0) &&
- (strcmp("/dev/ttyS1", argv[1])!=0) )) {
- printf("Usage:\tnserial SerialPort\n\tex: nserial /dev/ttyS1\n");
- exit(1);
- }/*
- /*
- Open serial port device for reading and writing and not as controlling tty
- because we don't want to get killed if linenoise sends CTRL-C.
- */
- fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
- if (fd <0) {perror(argv[1]); exit(-1); }
- if ( tcgetattr(fd,&oldtio) == -1) { /* save current port settings */
- perror("tcgetattr");
- exit(-1);
- }
- bzero(&newtio, sizeof(newtio));
- newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
- newtio.c_iflag = IGNPAR;
- newtio.c_oflag = 0;
- /* set input mode (non-canonical, no echo,...) */
- newtio.c_lflag = 0;
- newtio.c_cc[VTIME] = 3; /* inter-character timer unused */
- newtio.c_cc[VMIN] = 1; /* blocking read until 5 chars received */
- /*
- VTIME e VMIN devem ser alterados de forma a proteger com um temporizador a
- leitura do(s) próximo(s) caracter(es)
- */
- tcflush(fd, TCIOFLUSH);
- if ( tcsetattr(fd,TCSANOW,&newtio) == -1) {
- perror("tcsetattr");
- exit(-1);
- }
- /*
- O ciclo FOR e as instruções seguintes devem ser alterados de modo a respeitar
- o indicado no guião
- */
- printf("New termios structure set(Receiving Message)\n");
- char buf[5];
- char buf2[5];
- int aux=0;
- int counter=0;
- int sendAgain=1;
- for(counter ; counter < 3; counter++){
- aux=0;
- printf("Sending Data\n");
- res = write(fd,setup,5);
- while (STOP==FALSE )
- {
- res = read(fd,buf,1);
- if(!res) break;
- printf("%x - %d\n", buf[0], res);
- buf2[aux] = buf[0];
- aux++;
- switch (aux)
- {
- case 1:
- if (buf[0] != F)
- {
- STOP = TRUE;
- printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
- }
- break;
- case 2:
- if (buf[0] != A)
- {
- STOP = TRUE;
- printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
- }
- break;
- case 3:
- if (buf[0] != U)
- {
- STOP = TRUE;
- printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
- }
- break;
- case 4:
- if (buf[0] != (A^U))
- {
- STOP = TRUE;
- printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
- }
- break;
- case 5:
- if (buf[0] != F)
- {
- STOP = TRUE;
- printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
- }
- break;
- }
- if (buf[0]=='\0' || aux == 5){
- STOP=TRUE;
- break;
- }
- }
- if(aux==5) break;
- }
- if ( tcsetattr(fd,TCSANOW,&oldtio) == -1) {
- perror("tcsetattr");
- exit(-1);
- }
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment