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>
- #define BAUDRATE B38400
- #define _POSIX_SOURCE 1 /* POSIX compliant source */
- #define MODEMDEVICE "/dev/ttyS0"
- #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)
- {
- int fd,c, res;
- struct termios oldtio,newtio;
- char buf[255];
- char SET[255];
- unsigned char ua[5];
- /*
- 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.
- */
- system ("clear");
- 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] = 0; /* inter-character timer unused */
- newtio.c_cc[VMIN] = 1; /* blocking read until 1 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);
- }
- printf("New termios structure set\n");
- int aux = 0, state = 0;
- while (state < 5)
- {
- res = read(fd,buf,1);
- printf("%x - %d\n", buf[0], res);
- switch (state)
- {
- case 0:
- if (buf[0] == F)
- {
- SET[state] = buf[0];
- state++;
- }
- break;
- case 1:
- if (buf[0] == A)
- {
- SET[state] = buf[0];
- state++;
- }
- else if (buf[0] != F && buf[0] != A)
- state--;
- break;
- case 2:
- if (buf[0] == S)
- {
- SET[state] = buf[0];
- state++;
- }
- else if (buf[0] == F)
- state--;
- else
- state = 0;
- break;
- case 3:
- if (buf[0] == (A ^ S))
- {
- SET[state] = buf[0];
- state++;
- }
- else if (buf[0] == F)
- state = 1;
- else
- state = 0;
- break;
- case 4:
- if (buf[0] == F)
- {
- SET[state] = buf[0];
- state++;
- }
- else
- state = 0;
- break;
- }
- }
- printf ("Printing SET:\n");
- int i=0;
- for(i; i < state; i++)
- printf("%x", SET[i]);
- printf("\n");
- ua[0] = F;
- ua[1] = A;
- ua[2] = U;
- ua[3] = (A ^ U);
- ua[4] = F;
- res = write(fd, ua, 5);
- printf("%d bytes written\n",res);
- tcsetattr(fd,TCSANOW,&oldtio);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment