document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main() {
  8. /****************************
  9.  CONFIGURACION DEL PUERTO COM
  10. ****************************/
  11. HANDLE hSerial;
  12.    hSerial = CreateFile("COM3",
  13.       GENERIC_READ | GENERIC_WRITE,
  14.       0,
  15.       0,
  16.       OPEN_EXISTING,
  17.       FILE_ATTRIBUTE_NORMAL,
  18.       0);
  19.    if(hSerial==INVALID_HANDLE_VALUE){
  20.       if(GetLastError()==ERROR_FILE_NOT_FOUND){
  21.          cout << "The COM port doesn\'t exist" << endl;
  22.       }
  23.       cout << "Error: Invalid Handle" << endl;
  24.    }
  25.  
  26.    DCB dcbSerialParams = {0};
  27.    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);  // Esta linea esta mal en el PDF (aqui esta bien)
  28.    if (!GetCommState(hSerial, &dcbSerialParams)) {
  29.       cout << "Error getting state" << endl;
  30.    }
  31.    dcbSerialParams.BaudRate=CBR_9600;
  32.    dcbSerialParams.ByteSize=8;
  33.    dcbSerialParams.StopBits=ONESTOPBIT;
  34.    dcbSerialParams.Parity=NOPARITY;
  35.    if(!SetCommState(hSerial, &dcbSerialParams)){
  36.       cout << "Error setting serial port state" << endl;
  37.    }
  38.  
  39.    COMMTIMEOUTS timeouts={0};
  40.    timeouts.ReadIntervalTimeout=50;
  41.    timeouts.ReadTotalTimeoutConstant=50;
  42.    timeouts.ReadTotalTimeoutMultiplier=10;
  43.    timeouts.WriteTotalTimeoutConstant=50;
  44.    timeouts.WriteTotalTimeoutMultiplier=10;
  45.    if(!SetCommTimeouts(hSerial, &timeouts)){
  46.       cout << "Error Setting time out" << endl;
  47.    }
  48.  
  49. /********************
  50.  TRANSMISION DE DATOS
  51. ********************/
  52. char szBuff[1] = {0};
  53. DWORD dwBytesRead = 0;
  54. char palabra[64]; // 8 * 8
  55.  
  56. while (palabra[0] != \'!\') {
  57.    cout << "Palabra (! para salir): ";
  58.    cin >> palabra;
  59.    cout << endl;
  60.    unsigned short int i = 0;
  61.    while (palabra[i] != 0) {
  62.       szBuff[0] = palabra[i];
  63.       if(!WriteFile(hSerial, szBuff, 1, &dwBytesRead, NULL)){
  64.          cout << "No se pudo" << endl;
  65.          break;
  66.       }
  67.       i++;
  68.    }
  69. }
  70. }
');