Advertisement
Guest User

Wookie

a guest
Mar 3rd, 2009
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <termios.h>
  5. #include <strings.h>
  6.  
  7. #define SERIALPORT1 "/dev/ttyUSB0"
  8. #define SERIALPORT2 "/dev/ttyUSB1"
  9.  
  10. int main(void)
  11. {
  12.     int fd, fd2, bytes, i;
  13.     unsigned char a = 0xA1;
  14.     char buf[255];
  15.  
  16.     // open the data channel
  17.     fd = open(SERIALPORT1, O_RDWR  | O_NOCTTY | O_NDELAY);
  18.  
  19.     if (fd == -1) {
  20.         printf("Error opening data channel!\n");
  21.     } else {
  22.         printf("Data channel open.\n");
  23.     }
  24.  
  25.     // open the control channel
  26.     fd2 = open(SERIALPORT2, O_RDWR | O_NOCTTY | O_NDELAY);
  27.  
  28.     if (fd2 == -1) {
  29.         printf("Error opening control channel!\n");
  30.     } else {
  31.         printf("Control channel open.\n");
  32.     }
  33.  
  34.     // flush I/O buffers on both serial devices
  35.     tcflush(fd, TCIOFLUSH);
  36.     tcflush(fd2, TCIOFLUSH);
  37.  
  38.     a = 0xA1; // get version info command code
  39.     bytes = write(fd, &a, 1);
  40.  
  41.     if (bytes != 1) {
  42.         printf("Error sending request: %d (%s)\n", bytes, strerror(errno));
  43.     } else {
  44.         // wait 200ms
  45.         usleep(200000);
  46.        
  47.         // zero out the buffer
  48.         bzero(buf, 255);
  49.        
  50.         // read the bytes
  51.         for(i = 0; i < 255; i++) {
  52.             bytes = read(fd, &buf[i], 1);
  53.        
  54.             if(bytes <= 0)
  55.                 break;
  56.         }
  57.        
  58.         // output the version string
  59.         printf("Version: %s\n", buf);
  60.         printf("bytes read: %d\n", i);
  61.     }
  62.    
  63.     // must clean up...
  64.     close(fd);
  65.     close(fd2);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement