Вы находитесь на странице: 1из 3

// ConsoleApplication1.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <Windows.h>
#include <dos.h>
#include "SerialPortUtils.hpp"

using namespace std;

bool openPort(HANDLE& hCom, char* Port){


hCom = CreateFileA( Port,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-
access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use
OPEN_EXISTING
FILE_ATTRIBUTE_NORMAL, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE) {
return false;
}

return true;
}

void closePort(HANDLE& hCom) {


CloseHandle(hCom);
}

int WritePort(HANDLE hCom,char* dato,int length) {


DWORD bytesW;

printf("Mande a escribir");

WriteFile(hCom, dato, length,&bytesW, NULL); // **** Here it HANGS

printf("Despues de escribir");

return bytesW;
}

__int64 getCurrentTime() {
__int64 ret;
__int64 frequency;

QueryPerformanceCounter((LARGE_INTEGER *)&ret);
QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);

ret *= 1000000;
ret /= frequency;
return ret;
}

int main(int argc, _TCHAR* argv[])


{
unsigned char dato = 0;
SerialPortUtils* serialPort = new SerialPortUtils();
SerialPortData mData;
int scale = 1;
char port[100];
memset(port, 0, sizeof(port));

mData.baudRate = br19200;
mData.dataBits = db8;
mData.parity = None;
mData.stopBits = sb1;

cout << "Ingrese el nombre del puerto: ";


cin >> port; //EVento de teclado
cout << "El puerto elegido es: " << port << endl;

int bResult = serialPort->init(port, mData);

if (bResult < 0) {
printf("Error al inicializar el puerto serial %s\r\n",port);
Sleep(3000);

return -1;
}
else {
printf("Puerto serial %s inicializado correctamente\r\n", port);
}

cout << "Multiplicador de amplitud(xn): ";


cin >> scale;
cout << endl;

__int64 startTimeM1, startTimeM0;

startTimeM1 = getCurrentTime();
startTimeM0 = getCurrentTime();

while (true) {
dato = 0x06;

if (getCurrentTime() - startTimeM0 <= 5 * 1000 * scale) {


dato = dato & 0xFF;
}
else if (getCurrentTime() - startTimeM0 <= 10 * 1000 * scale) {
dato = dato & 0xFD;

}
else {
startTimeM0 = getCurrentTime();
dato = dato & 0xFF;
}

if (getCurrentTime() - startTimeM1 <= 5 * 1000 * scale) {


dato = dato & 0xFF;
}
else if (getCurrentTime() - startTimeM1 <= 20 * 1000 * scale) {
dato = dato & 0xFB;
}
else {
startTimeM1 = getCurrentTime();
dato = dato & 0xFF;
}
serialPort->write(&dato, 1);
//Sleep(1);
printf("Estado Puerto %x \r\n",dato);
}
getchar();
return 0;
}

Вам также может понравиться