[ create a new paste ] login | about

Link: http://codepad.org/0yvz8jk4    [ raw code | fork ]

C, pasted on Dec 4:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <termios.h>
#include <time.h>
#include <sys/time.h>
#include <netdb.h> 


int openport(const char *device, speed_t baud, int setup)
{
	int filedes;
	struct termios serparams;

	if ((filedes=open(device, O_RDWR | O_NONBLOCK))==-1) {
		/* Can't open device */
		fprintf(stderr,"%s: ",device);
		perror(device);
		exit(1);
	}

	if (setup) {
		bzero(&serparams, sizeof(serparams));

		cfsetspeed(&serparams, baud);
		serparams.c_cflag |= CLOCAL | CS8 | CREAD;

		if (tcflush(filedes, TCIFLUSH)) {
			fprintf(stderr,"%s: ",device);
			perror("tcflush");
			exit(1);
		}
		if (tcsetattr(filedes, TCSANOW, &serparams)) {
			fprintf(stderr,"%s: ",device);
			perror("tcsetattr");
			exit(1);
		}
	}

	return filedes;
}



int main(void)
{
	int fd;
	char buf[100];
	FILE* fp;
	fd_set readfd, exceptfd;
	fd = openport("/dev/ttyUSB0", B9600, 1);


	fp = fopen("stm3MsgDump", "w");
        FD_ZERO(&readfd);
        FD_ZERO(&exceptfd);
        FD_SET(fd,&readfd);
        FD_SET(fd,&exceptfd); 
	
	while(1)
	{
		read(fd, buf, 100);
		printf("start:%s\n", buf);
		
		fp = fopen("stm3MsgDump", "a+");	
		fwrite(buf, sizeof(buf[0]), strlen(buf), fp);
		fclose(fp);

		memset(buf, 0, sizeof(buf));
		for(int i = 0 ; i < 1000;i++);
	}


	return 0;
}


Create a new paste based on this one


Comments: