Advertisement
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 MODEMDEVICE "/dev/ttyS1"
- #define _POSIX_SOURCE 1 /* POSIX compliant source */
- #define FALSE 0
- #define TRUE 1
- volatile int STOP=FALSE;
- int main(int argc, char** argv)
- {
- int fd,c, res;
- struct termios oldtio,newtio;
- char buf[255];
- char buf2[255];
- int i, 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(argv[1], 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] = 5; /* 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);
- }
- printf("New termios structure set\n");
- // for (i = 0; i < 255; i++) {
- // buf[i] = 'a';
- // }
- //
- // /*testing*/
- // buf[25] = '\n';
- gets(buf);
- int n= strlen(buf)+1;
- res = write(fd,buf,n);
- printf("%d bytes written\n", res);
- /*
- O ciclo FOR e as instru��es seguintes devem ser alterados de modo a respeitar
- o indicado no gui�o
- */
- char* fr;
- strcpy(buf2, "");
- while (STOP==FALSE) { /* loop for input */
- read(fd,fr,1); /* returns after 5 chars have been input */
- strcat(buf2,fr); /* so we can printf... */
- if (!strcmp(fr,"\0")) STOP=TRUE;
- }
- printf ("%s\n",buf2);
- if ( tcsetattr(fd,TCSANOW,&oldtio) == -1) {
- perror("tcsetattr");
- exit(-1);
- }
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement