Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TERMINAL "/dev/ttyS2"
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <termios.h>
- #include <unistd.h>
- int set_interface_attribs(int fd, int speed)
- {
- struct termios tty;
- if (tcgetattr(fd, &tty) < 0) {
- printf("Error from tcgetattr: %s\n", strerror(errno));
- return -1;
- }
- cfsetospeed(&tty, (speed_t)speed);
- cfsetispeed(&tty, (speed_t)speed);
- tty.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
- tty.c_cflag &= ~CSIZE;
- tty.c_cflag |= CS8; /* 8-bit characters */
- tty.c_cflag &= ~PARENB; /* no parity bit */
- tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
- tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
- /* setup for non-canonical mode */
- tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
- tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
- tty.c_oflag &= ~OPOST;
- /* fetch bytes as they become available */
- tty.c_cc[VMIN] = 1;
- tty.c_cc[VTIME] = 1;
- if (tcsetattr(fd, TCSANOW, &tty) != 0) {
- printf("Error from tcsetattr: %s\n", strerror(errno));
- return -1;
- }
- return 0;
- }
- void set_mincount(int fd, int mcount)
- {
- struct termios tty;
- if (tcgetattr(fd, &tty) < 0) {
- printf("Error tcgetattr: %s\n", strerror(errno));
- return;
- }
- tty.c_cc[VMIN] = mcount ? 1 : 0;
- tty.c_cc[VTIME] = 5; /* half second timer */
- if (tcsetattr(fd, TCSANOW, &tty) < 0)
- printf("Error tcsetattr: %s\n", strerror(errno));
- }
- int main()
- {
- char *portname = TERMINAL;
- int fd;
- int wlen;
- char *xstr = "Hello!\n";
- int xlen = strlen(xstr);
- // FILE *fp;
- // fp = fopen("out.txt", "w");
- fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
- if (fd < 0) {
- printf("Error opening %s: %s\n", portname, strerror(errno));
- return -1;
- }
- /*baudrate 115200, 8 bits, no parity, 1 stop bit */
- set_interface_attribs(fd, B115200);
- // set_mincount(fd, 0); /* set to pure timed read */
- /* simple output */
- wlen = write(fd, xstr, xlen);
- if (wlen != xlen) {
- printf("Error from write: %d, %d\n", wlen, errno);
- }
- tcdrain(fd); /* delay for output */
- /* simple noncanonical input */
- // do {
- unsigned char buf[80];
- // unsigned char MeasData[512];
- int rdlen;
- int i, time, offset, date, p, buffsize;
- unsigned short lambda, type, pages;
- float Yvalue;
- rdlen = read(fd, buf, sizeof(buf) - 1);
- if (((int)buf[0]==122)&&((int)buf[2])==20)
- {
- printf("LD 07....OK \n");
- pages = *(unsigned short *)(&buf[12]);
- printf("PAGES TO READ %d\n", pages);
- //unsigned char MeasData[512*4096];
- //unsigned char * MeasData = ( unsigned char * )malloc( 512*pages * sizeof( unsigned char ) );
- unsigned char MeasData[512*pages];
- // unsigned char *MeasData = malloc(sizeof(unsigned char)*(512*pages));
- for (i=0;i<512*pages; i++)
- {
- rdlen = read(fd, buf, sizeof(buf)-1);
- MeasData[i] = buf[12];
- }
- for (i=0; i<512*pages; i++)
- {
- date = *(int *)(&MeasData[0+offset]);
- time = *(int *)(&MeasData[4+offset]);
- lambda = *(unsigned short *)(&MeasData[8+offset]);
- Yvalue = *(float *)(&MeasData[12+offset]);
- type = *(unsigned short*)(&MeasData[16+offset]) ;
- printf("Date: %02d.%02d", (date&0xFF),((date>>8)&0xFF));
- printf(" Time: %02d:%02d:%02d", ((time>>16)&0xFF),((time>>8)&0xFF),(time&0xFF));
- printf(" λ: 0.%d:", lambda);
- printf(" Yvalue: %f", Yvalue);
- printf(" type: %d", type);
- printf("\n");
- offset += 32;
- }
- //fwrite(&MeasData, sizeof(MeasData), 1, fp);
- //fclose(fp);
- if (rdlen > 0) {
- /* display hex */
- // unsigned char *p;
- // printf("Read %d:", rdlen);
- // for (p = buf; rdlen-- > 0; p++)
- // printf(" 0x%x", *p);
- // printf("\n");
- } else if (rdlen < 0) {
- printf("Error from read: %d: %s\n", rdlen, strerror(errno));
- } else { /* rdlen == 0 */
- printf("Timeout from read\n");
- }
- /* repeat read to get full message */
- // } //while (1);
- }else
- {
- printf ("Restart App\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement