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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream>
using namespace std;
#include <stdio.h>
#include <Windows.h>
#include <conio.h>
#include <fstream>
int main(int argc, char* argv[])
{
char INBUFFER[500];
char OUTBUFFER[200];
char testBuffer[]="ABC";
DWORD bytes_read = 0; // Number of bytes read from port
DWORD bytes_written = 0; // Number of bytes written to the port
HANDLE comport = NULL; // Handle COM port
int bStatus;
DCB comSettings; // Contains various port settings
COMMTIMEOUTS CommTimeouts;
ofstream WriteToFile;
strcpy(&OUTBUFFER[0], "The quick brown fox jumped over the lazy dog. \n\r\0");
char* msg_rec,msg_good;
char header[1000],buf[500];
int i=1;
static char* msg_prev="\0";
// Open COM port
if ((comport =
CreateFile("\\\\.\\COM7", // open com7:
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
// error processing code goes here
}
// Set timeouts in milliseconds
CommTimeouts.ReadIntervalTimeout = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 100;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 100;
bStatus = SetCommTimeouts(comport,&CommTimeouts);
if (bStatus != 0)
{
// error processing code goes here
}
// Set Port parameters.
// Make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then change the ones you want and call SetCommState().
GetCommState(comport, &comSettings);
comSettings.BaudRate = 57600;
comSettings.StopBits = ONESTOPBIT;
comSettings.ByteSize = 8;
comSettings.Parity = NOPARITY;
comSettings.fParity = FALSE;
bStatus = SetCommState(comport, &comSettings);
if (bStatus == 0)
{
// error processing code goes here
}
while(!kbhit())
{
bStatus = WriteFile(comport, // Handle
&OUTBUFFER, // Outgoing data
15, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);
if (bStatus != 0)
{
// error processing code here
}
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
10, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}
// code to do something with the data goes here
msg_rec=&INBUFFER[0];
// THIS IS VERY SLOW
{
WriteToFile.open("Sine.txt",std::ofstream::app);
unsigned int inMessageLength=10;
strcat(msg_rec," ");
WriteToFile.write(msg_rec,inMessageLength);
WriteToFile.close();
//Find valid chars only, cut out unusefull data;
for (j=0;j<inMessageLength;j++)
if ((int)msg_rec[j]<0)
break;
//here I break the message
msg_rec[j]='\0';
}
CloseHandle(comport);
system("PAUSE");
return 0;
}
|