7 #include "PlusConfigure.h" 13 , SerialPortSpeed(9600)
34 CloseHandle(CommHandle);
50 CommHandle = CreateFile(this->PortName.c_str(),
51 GENERIC_READ | GENERIC_WRITE,
54 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
61 if (!SetupComm(CommHandle, 16384, 16384))
67 GetCommState(CommHandle, &dcb);
68 dcb.BaudRate = SerialPortSpeed;
69 dcb.Parity = NOPARITY;
71 dcb.StopBits = ONESTOPBIT;
72 if (!SetCommState(CommHandle, &dcb))
78 COMMTIMEOUTS timeouts;
79 GetCommTimeouts(CommHandle, &timeouts);
80 timeouts.ReadIntervalTimeout = 200;
81 timeouts.ReadTotalTimeoutConstant = MaxReplyTime;
82 timeouts.ReadTotalTimeoutMultiplier = 100;
83 timeouts.WriteTotalTimeoutConstant = MaxReplyTime;
84 timeouts.WriteTotalTimeoutMultiplier = 100;
85 if (!SetCommTimeouts(CommHandle, &timeouts))
93 LOG_ERROR(
"SerialLine::Open() is only implemented on Windows");
103 int numberOfBytesWrittenTotal = 0;
104 while (numberOfBytesToWrite > 0)
106 DWORD numberOfBytesWritten = 0;
107 if (WriteFile(CommHandle, &
data[numberOfBytesWrittenTotal], numberOfBytesToWrite, &numberOfBytesWritten, NULL) ==
FALSE)
109 if (GetLastError() == ERROR_OPERATION_ABORTED)
113 ClearCommError(CommHandle, &errors, NULL);
118 return numberOfBytesWritten;
121 else if (numberOfBytesWritten == 0)
124 return numberOfBytesWrittenTotal;
127 numberOfBytesToWrite -= numberOfBytesWritten;
128 numberOfBytesWrittenTotal += numberOfBytesWritten;
130 return numberOfBytesWrittenTotal;
132 LOG_ERROR(
"SerialLine::Write() is only implemented on Windows");
147 int numberOfBytesReadTotal = 0;
148 while (maxNumberOfBytesToRead > 0)
150 DWORD numberOfBytesRead;
151 if (ReadFile(CommHandle, &
data[numberOfBytesReadTotal], maxNumberOfBytesToRead, &numberOfBytesRead, NULL) ==
FALSE)
153 DWORD lastError = GetLastError();
154 if (lastError == ERROR_OPERATION_ABORTED)
158 ClearCommError(CommHandle, &errors, NULL);
163 return numberOfBytesReadTotal;
166 else if (numberOfBytesRead == 0)
169 return numberOfBytesReadTotal;
171 maxNumberOfBytesToRead -= numberOfBytesRead;
172 numberOfBytesReadTotal += numberOfBytesRead;
174 return numberOfBytesReadTotal;
176 LOG_ERROR(
"SerialLine::Read() is only implemented on Windows");
193 ClearCommError(CommHandle, &dwErrors, &comStat);
196 LOG_ERROR(
"SerialLine::ClearError() is only implemented on Windows");
204 SerialPortSpeed = speed;
210 MaxReplyTime = maxreply;
229 DWORD dwErrorFlags = 0;
231 ClearCommError(CommHandle, &dwErrorFlags, &comStat);
232 return ((
int) comStat.cbInQue);
234 LOG_ERROR(
"SerialLine::GetNumberOfBytesAvailableForReading() is only implemented on Windows");
243 DWORD dwFunc = CLRDTR;
248 if (EscapeCommFunction(CommHandle, dwFunc))
257 LOG_ERROR(
"SerialLine::SetDTR() is only implemented on Windows");
266 DWORD dwFunc = CLRRTS;
271 if (EscapeCommFunction(CommHandle, dwFunc))
280 LOG_ERROR(
"SerialLine::SetRTS() is only implemented on Windows");
290 if (!GetCommModemStatus(CommHandle, &dwStatus))
294 onOff = MS_DSR_ON & dwStatus;
297 LOG_ERROR(
"SerialLine::GetDSR() is only implemented on Windows");
307 if (!GetCommModemStatus(CommHandle, &dwStatus))
311 onOff = MS_CTS_ON & dwStatus;
314 LOG_ERROR(
"SerialLine::GetCTS() is only implemented on Windows");
PlusStatus SetRTS(bool onOff)
int Write(const BYTE *data, int numberOfBytesToWrite)
void SetMaxReplyTime(int maxreply)
int Read(BYTE *data, int maxNumberOfBytesToRead)
PlusStatus SetDTR(bool onOff)
PlusStatus GetCTS(bool &onOff)
#define INVALID_HANDLE_VALUE
unsigned int GetNumberOfBytesAvailableForReading() const
PlusStatus GetDSR(bool &onOff)
void SetSerialPortSpeed(DWORD speed)
bool IsHandleAlive() const
int GetMaxReplyTime() const