Hej bertel jeg kan ikke lige se for mig om den regner det rigtigt ud det med m/s i dette program kan du se om det er rigtigt???
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <time.h>
void SetTimeOut(HANDLE Port, int MSec)
{
COMMTIMEOUTS commtimeouts;
GetCommTimeouts(Port, &commtimeouts);
commtimeouts.ReadIntervalTimeout = MSec;
commtimeouts.ReadTotalTimeoutMultiplier = 1;
commtimeouts.ReadTotalTimeoutConstant = MSec;
SetCommTimeouts(Port, &commtimeouts);
}
unsigned int Start, End;
void Rx(HANDLE Port)
{
char Buffer[128];
DWORD Length = 0;
DWORD ErrorType = 0;
do
{
ReadFile(Port, Buffer, 128, &Length, 0);
ClearCommError(Port, &ErrorType, 0);
}
while(Length == 0 && ErrorType == 0);
Start = clock();
cout << "Du har sat de to ledninger sammen 1 gang" <<endl;
}
void Rx1(HANDLE Port)
{
char Buffer[128];
DWORD Length = 0;
DWORD ErrorType = 0;
double diff;
do
{
ReadFile(Port, Buffer, 128, &Length, 0);
ClearCommError(Port, &ErrorType, 0);
End = clock();
diff = (double )(End - Start)/CLOCKS_PER_SEC;
}
while((Length == 0 && ErrorType == 0) || diff < 0.50);
cout << "Delay: " << (double )(End - Start)/CLOCKS_PER_SEC << endl;
}
int main(void)
{
DCB dcb;
HANDLE ComPort = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
GetCommState(ComPort, &dcb);
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fErrorChar = TRUE;
dcb.fParity = TRUE;
dcb.Parity = ODDPARITY;
dcb.fAbortOnError = TRUE;
dcb.fOutxCtsFlow = 1;
SetCommState(ComPort, &dcb);
SetTimeOut(ComPort, 1);
float hjul = 0.6908; //0.6908 meter
float ms;
float tidisek;
while(1)
{
Rx(ComPort);
Rx1(ComPort);
tidisek = End / 1000;
ms = hjul / tidisek;
cout << "Hjulet korer " << ms << " m/s" << endl;
Start = 0;
End = 0;
}
return 0;
}