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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
class CSerialComm
{
public:
BOOL m_bPortReady;
HANDLE m_hCom;
DCB PortDCB;
COMMTIMEOUTS m_CommTimeouts;
BOOL bWriteRC;
BOOL bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
BOOL m_CommOpen;
BOOL _bPortReady;
char sBuffer[128];
char cPortName[5];
CSerialComm();
virtual ~CSerialComm();
BOOL OpenSerialPort();
BOOL Read(char *buffer, unsigned int ByteCount);
BOOL Write(char *buffer);
BOOL CloseSerialPort();
DWORD GetModemInfo();
};
DWORD dwError;
CSerialComm::CSerialComm()
{
}
CSerialComm::~CSerialComm()
{
CloseHandle(m_hCom);
}
BOOL CSerialComm::OpenSerialPort()
{
/*
GetCommTimeouts (m_hCom, &m_CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
m_CommTimeouts.ReadIntervalTimeout = MAXDWORD;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
m_CommTimeouts.ReadTotalTimeoutConstant = 0;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 10;
m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
*/
strcpy(cPortName,"COM3");
m_hCom = CreateFileA (cPortName ,// Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy
// if fail to open the port, return FALSE.
if ( m_hCom == INVALID_HANDLE_VALUE )
// Could not open the port.
return FALSE;
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (m_hCom, &PortDCB);
// Change the DCB structure settings.
// PortDCB.BaudRate = 9600; // Current baud
// PortDCB.fBinary = TRUE; // Binary mode; no EOF check
// PortDCB.fParity = TRUE; // Enable parity checking.
// PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
// PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
// PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
// PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
// PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
// PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
// PortDCB.fInX = FALSE; // No XON/XOFF in flow control
// PortDCB.fErrorChar = FALSE; // Disable error replacement.
// PortDCB.fNull = FALSE; // Disable null stripping.
// PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
// PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error.
// PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8
// PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
// PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// unsigned long inBuff = 1200;
// unsigned long outBuff = 1200;
// SetupComm(m_hCom,inBuff,outBuff);
// Configure the port according to the specifications of the DCB
// structure.
SetCommState(m_hCom,&PortDCB);
return true;
}
BOOL CSerialComm::Write(char *buffer)
{
DWORD dummy;
if(buffer == NULL)
return FALSE;
WriteFile( m_hCom,
buffer,
(DWORD)strlen(buffer),
&dummy,
0
);
return true;
}
BOOL CSerialComm::Read(char *buffer, unsigned int ByteCount)
{
DWORD dummy;
if((ByteCount==0) || (buffer == NULL))
return FALSE;
if(!ReadFile(m_hCom,buffer,(DWORD)ByteCount,&dummy,NULL))
return FALSE;
return TRUE;
}
BOOL CSerialComm::CloseSerialPort()
{
if(CloseHandle(m_hCom)) return TRUE ;
else return FALSE;
}
void main()
{
char buff[800];
for(int i=0;i<800;i++)
buff[i] = 0;
CSerialComm oSerialComm;
oSerialComm.OpenSerialPort();
oSerialComm.Write("ATDT0123456\r");
oSerialComm.Read(buff,510);
oSerialComm.CloseSerialPort();
printf("%s",buff);
} |
Partager