/**************************************************** * This program will send commands to the uC * * programmed with the atmega168-thermo program and * * echo characters from the microcontroller terminal * * device to stdout until the process is terminated * * by a signal. The default terminal is "/dev/ttyS0 * * This program is similar to the serial.c program * * except this program will interpret the messages * * received by the device, namely the temperature * * conversion results * ****************************************************/ #define _GNU_SOURCE 1 /* use GNU extensions (usleep) */ #include #include #include #include #include #include #include #include #include #include #define BAUDRATE B9600 #define MODEMDEVICE "/dev/ttyUSB0" int main( int argc, char** argv ) { int device_fd; int16_t int_temp; float float_temp; struct termios device_oldtio, device_newtio; uint8_t c[11]; time_t raw_time; struct tm * str_time; char chr_time[80]; device_fd = open( argc>1?argv[1]:MODEMDEVICE, O_RDWR | O_NOCTTY ); if (device_fd <0) { perror(argc>1?argv[1]:MODEMDEVICE); exit(1); } tcgetattr(device_fd, &device_oldtio); memset(&device_newtio, '\0', sizeof(device_newtio)); device_newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; device_newtio.c_iflag = IGNPAR; device_newtio.c_oflag = 0; /* set input mode (non-canonical, no echo,...) */ device_newtio.c_lflag = 0; device_newtio.c_cc[VTIME] = 0; /* inter-character timer * 0.1s */ device_newtio.c_cc[VMIN] = 11; /* blocking read until 1 chars received */ tcsetattr(device_fd,TCSANOW, &device_newtio); while (1) { tcflush(device_fd, TCIFLUSH); write(device_fd, "T", 1); read(device_fd, c, 11); int_temp = 0; int_temp |= ((unsigned int) c[1]); int_temp |= ((unsigned int) c[2]) << 8; float_temp = int_temp * 0.0625; time(&raw_time); str_time = localtime(&raw_time); strftime(chr_time, 80, "%c", str_time); printf("Time: %s C: %f F: %f\n", chr_time, float_temp, (float_temp*9.0/5.0)+32); sleep(1); } tcsetattr(device_fd,TCSANOW,&device_oldtio); close(device_fd); return(0); }