All pastes #977494 Raw Edit

alesan

public c v1 · immutable
·published 2008-04-08 21:36 UTC
rendered paste body
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include <stdio.h>#include <stdlib.h>#include <strings.h>#include <errno.h>  #define BAUDRATE B9600  int main(int argc, char** argv){	int fd, c, res;	struct termios oldtio, newtio;	char readSerial[255];	char readUser[8];	int value, i;		bzero(readSerial, 255);		if(argc!=2) exit(0);	fd = open(argv[1], O_RDWR | O_NOCTTY); 	if(fd <0) {perror(argv[1]); exit(0); }		tcgetattr(fd, &oldtio); /* save current port settings */		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 5 chars received */		tcflush(fd, TCIFLUSH);	tcsetattr(fd, TCSANOW, &newtio);		while(1)	{		printf("Enter hex value: ");		fgets(readUser, 8, stdin);		value = strtol(readUser, NULL, 0);		printf("Read '%x';\n", value);		res = write(fd, &value, 1);		if(res<0) {printf("--%d %s--", errno, strerror(errno)); perror(argv[1]); exit(0); }		printf("Res = %d\n", res);		printf("Waiting for response...\n");		printf("Result: ");		do		{			res = read(fd, readSerial, 255);			for(i=0; i<res; i++)				printf("%x", readSerial[i]);		} while(res==255);		printf("...\n");	}	tcsetattr(fd, TCSANOW, &oldtio);}