Advertisement
Guest User

cube_pc

a guest
Jan 7th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "cube.h"
  4. #include <termios.h>
  5. #include <fcntl.h>
  6. #include <strings.h>
  7. #include <windows.h> // windows
  8.  
  9. void cube_push (unsigned char data[8][8])
  10. {
  11.     int x,y,i;
  12.  
  13.     i= 0;
  14.  
  15.     unsigned char buffer[200];
  16.  
  17.     buffer[i++] = 0xff; // escape
  18.     buffer[i++] = 0x00; // reset to 0,0
  19.  
  20.     for (x=0;x<8;x++)
  21.     {
  22.         for (y=0;y<8;y++)
  23.         {
  24.             buffer[i++] = data[x][y];
  25.             if (data[x][y] == 0xff)
  26.             {
  27.                 buffer[i++] = data[x][y];
  28.             }
  29.         }
  30.     }
  31.    
  32.     // windows 
  33.     HANDLE hSerial;
  34.     PDWORD dwBytesWrite = 0;
  35.    
  36.     if(!WriteFile(hSerial, &buffer, i, dwBytesWrite, NULL)){
  37.         //error occurred. Report to user.
  38.     }
  39.    
  40.     //write(tty,&buffer,i); // linux
  41. }
  42.  
  43. int cube_init (void)
  44. {
  45. // windows
  46.    
  47.     // opening port 3
  48.     HANDLE hSerial;
  49.    
  50.     hSerial = CreateFile("COM3",
  51.                         GENERIC_READ | GENERIC_WRITE,
  52.                         0,
  53.                         0,
  54.                         OPEN_EXISTING,
  55.                         FILE_ATTRIBUTE_NORMAL,
  56.                         0);
  57.                        
  58.     if(hSerial==INVALID_HANDLE_VALUE){
  59.         if(GetLastError()==ERROR_FILE_NOT_FOUND){
  60.             //serial port does not exist. Inform user.
  61.         }
  62.     //some other error occurred. Inform user.
  63.     }
  64.    
  65.     // setting parameters
  66.     DCB dcbSerialParams = {0};
  67.    
  68.     dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
  69.    
  70.     if (!GetCommState(hSerial, &dcbSerialParams)) {
  71.         //error getting state
  72.     }
  73.    
  74.     dcbSerialParams.BaudRate=CBR_38400;
  75.     dcbSerialParams.ByteSize=8;
  76.     dcbSerialParams.StopBits=ONESTOPBIT;
  77.     dcbSerialParams.Parity=NOPARITY;
  78.    
  79.     if(!SetCommState(hSerial, &dcbSerialParams)){
  80.         //error setting serial port state
  81.     }
  82.    
  83. /* linux
  84.     //FILE *ftty;
  85.  
  86.     //ftty = fopen("/dev/ttyUSB0","a");
  87.    
  88.    
  89.     struct termios io;
  90.  
  91.     char *tty_path = "/dev/ttyUSB0";
  92.  
  93.     //tty = open(tty_path, O_RDWR | O_NOCTTY | O_NDELAY); // <-  ORIGINAL
  94.     tty = open(tty_path, O_RDWR);
  95.    
  96.    
  97.     if (tty <0) {perror(tty_path); exit(-1); }
  98.    
  99.     bzero(&io, sizeof(io));
  100.     //io.c_cflag = B2400 | CRTSCTS | CS8 | CLOCAL | CREAD;
  101.     //io.c_cflag = B2400 | CRTSCTS | PARENB | CS8 | CLOCAL | CREAD;
  102.     io.c_cflag = B38400 | PARENB | CS8 | CLOCAL | CREAD;
  103.     io.c_iflag = IGNPAR;
  104.     io.c_oflag = 0;
  105.    
  106.     // set input mode (non-canonical, no echo,...)
  107.     io.c_lflag &= ~OPOST;
  108.     //io.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  109.    
  110.     io.c_cc[VTIME]    = 0;   // inter-character timer unused
  111.     io.c_cc[VMIN]     = 0;   // blocking read until 5 chars received
  112.    
  113.     // Flush buffer
  114.     tcflush(tty, TCIFLUSH);
  115.     // write config to tty
  116.     tcsetattr(tty,TCSANOW,&io);
  117.    
  118.  
  119.     //fcntl(tty, F_SETFL, 0);
  120. */
  121.     return 1;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement