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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <string.h>
// Flow control flags
#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04
//////////////////////////////////////////////////////////
using namespace std;
// variables used with the com <strong class="highlight">port</strong>
BOOL bPortReady;
DCB dcb;
COMMTIMEOUTS CommTimeouts;
BOOL bWriteRC;
BOOL bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
////////////////////////////////////////////////////////////
HANDLE SerialInit(char *ComPortName, int BaudRate)
{
HANDLE hComm;
hComm = CreateFile(ComPortName,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
0, // no overlapped I/O
NULL); // null template
bPortReady = SetupComm(hComm, 1, 128); // set buffer sizes
bPortReady = GetCommState(hComm, &dcb);
dcb.BaudRate = BaudRate;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
//dcb.Parity = EVENPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fAbortOnError = TRUE;
// set XON/XOFF
dcb.fOutX = FALSE; // XON/XOFF off for transmit
dcb.fInX = FALSE; // XON/XOFF off for receive
// set RTSCTS
dcb.fOutxCtsFlow = FALSE; // turn on CTS flow control
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; //
// set DSRDTR
dcb.fOutxDsrFlow = FALSE; // turn on DSR flow control
dcb.fDtrControl = DTR_CONTROL_ENABLE; //
dcb.fDtrControl = DTR_CONTROL_DISABLE; //
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //
bPortReady = SetCommState(hComm, &dcb);
// Communication timeouts are optional
bPortReady = GetCommTimeouts (hComm, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = 5;
CommTimeouts.ReadTotalTimeoutConstant = 5;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 5;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
bPortReady = SetCommTimeouts (hComm, &CommTimeouts);
return hComm;
}
/////////////////////////////////////////////////////////
char SerialRead(HANDLE *hComm)
{
char rxchar;
BOOL bReadRC;
static DWORD iBytesRead;
bReadRC = ReadFile(*hComm, &rxchar, 1, &iBytesRead, NULL);
printf("BYTES READ: %s", iBytesRead);
return rxchar;
}
////////////////////////////////////////////////////////
void SerialWrite(HANDLE *hComm, char valor)
{
BOOL bWriteRC;
static DWORD iBytesWritten;
bWriteRC = WriteFile(*hComm, &valor, 1, &iBytesWritten,NULL);
return;
}
////////////////////////////////////////////////////////
int main()
{
HANDLE my=SerialInit("COM1",9600);
char letter;
printf("test\n");
HANDLE *ptr;
*ptr=my;
SerialWrite(ptr,'A');
SerialWrite(ptr,'T');
SerialWrite(ptr,'\n');
SerialRead(ptr);
getch();
return 0;
}
///////////////////////////////////////////////////////////
|