1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#define BAUDRATE B115200
#define PORT "/dev/ttyUSB0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
void signal_handler_IO ( int status ); // definition of signal handler
volatile int STOP = FALSE;
int wait_flag = TRUE; // TRUE while no signal received
int main()
{
int fd, res, sen;
int i;
struct termios oldtio, newtio;
struct sigaction saio; /* definition of signal action */
char comm[7], resp[10];
for ( i = 0; i < 10; i++ ) { resp[i] = 0x00; }
/* open the device to be non-blocking (read will return immediatly) */
fd = open( PORT, O_RDWR | O_NOCTTY | O_NONBLOCK );
if (fd < 0) { perror(PORT); return 1; }
/* install the signal handler before making the device asynchronous */
saio.sa_handler = signal_handler_IO;
//saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction( SIGIO, &saio, NULL );
/* allow the process to receive SIGIO */
fcntl( fd, F_SETOWN, getpid() );
/* Make the file descriptor asynchronous (the manual page says only
O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
fcntl( fd, F_SETFL, FASYNC );
//tcgetattr(fd,&oldtio); /* save current port settings */
/* set new port settings for canonical input processing */
newtio.c_cflag = BAUDRATE | CRTSCTS | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
//newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
//newtio.c_lflag = 0;
newtio.c_cc[VMIN] = 0;
newtio.c_cc[VTIME] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr( fd, TCSANOW, &newtio );
comm[0] = 'V'; comm[1] = '\r';
printf("Write start...\n");
sen = write( fd, comm, 2 );
printf("Write stop...\nsen = %d\n", sen);
printf("Read start...\n");
res = read( fd, resp, 4 );
printf("Read stop...\n");
resp[res] = '\0';
printf("Response = %s\n", resp );
/* restore old port settings */
tcsetattr( fd, TCSANOW, &oldtio );
return 0;
}
/***************************************************************************
* signal handler. sets wait_flag to FALSE, to indicate above loop that *
* characters have been received. *
***************************************************************************/
void signal_handler_IO (int status)
{
printf("received SIGIO signal.\n");
wait_flag = FALSE;
}
|