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
|
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <windows.h>
using namespace std;
int main()
{
char sendcode1[]="1";
char sendcode2[]="2";
string ss;
ss ="\\\\.\\COM8" ;
HANDLE arduinoSerial=0;
DWORD bytesread = 1;
arduinoSerial = CreateFile(ss.c_str() ,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);//creating the comunication with the scale
if (arduinoSerial == INVALID_HANDLE_VALUE)
{
cout<<"COM Communication Error!\nPlease Check the USB connection.\n";
CloseHandle(arduinoSerial);
}
DCB dcbSerialParams = {0};
COMMTIMEOUTS CommTimeouts= {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
dcbSerialParams.fBinary = TRUE;
dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
dcbSerialParams.fRtsControl = RTS_CONTROL_ENABLE;
dcbSerialParams.fOutxCtsFlow = TRUE;
dcbSerialParams.fOutxDsrFlow = TRUE;
dcbSerialParams.fDsrSensitivity= TRUE;
dcbSerialParams.fAbortOnError = TRUE;
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
CommTimeouts.ReadTotalTimeoutConstant = 500;
SetCommTimeouts (arduinoSerial, &CommTimeouts);//used for the timeouts
SetCommState(arduinoSerial, &dcbSerialParams);//parameters of the scale
WriteFile(arduinoSerial, sendcode1 , 1, &bytesread, NULL);
Sleep(500);
WriteFile(arduinoSerial, sendcode1 , 1, &bytesread, NULL);
cout<<"light on\n";
Sleep(4500);
WriteFile(arduinoSerial,sendcode2, 1, &bytesread,NULL);
Sleep(5000);
}
|