00001
00002
00003
00004
00005
00006
00007
00008 #include "serialDevice.h"
00009 #include <stdio.h>
00010
00011
00012 SerialDevice::~SerialDevice(void) {
00013 raw_disconnect();
00014 }
00015
00016
00017 const char* SerialDevice::what(void) {
00018 return error_message.c_str();
00019 }
00020
00021
00022 SerialDevice::SerialDevice(int buffer_size)
00023 : ConnectionDevice(buffer_size), hComm(INVALID_HANDLE_VALUE),
00024 error_message("Connection device is not specified") {
00025 }
00026
00027
00028 int SerialDevice::raw_connect(const char* device, long baudrate) {
00029 char comPort[16];
00030 if (strlen(device) >= 16) {
00031 return DeviceOpenFail;
00032 }
00033 sprintf(comPort, "\\\\.\\%s", device);
00034 hComm = CreateFile(comPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
00035 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
00036 if (hComm == INVALID_HANDLE_VALUE) {
00037 LPVOID lpMsg;
00038 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00039 FORMAT_MESSAGE_FROM_SYSTEM |
00040 FORMAT_MESSAGE_IGNORE_INSERTS,
00041 NULL, GetLastError(),
00042 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00043 (LPTSTR)&lpMsg, 0, NULL);
00044 printf("port(%s) open failed: %s\n", device, (char *)lpMsg);
00045 LocalFree(lpMsg);
00046 return DeviceOpenFail;
00047 }
00048 return 0;
00049 }
00050
00051
00052 void SerialDevice::raw_disconnect(void) {
00053 CloseHandle(hComm);
00054 }
00055
00056
00057 bool SerialDevice::isConnected(void) {
00058 return (hComm != INVALID_HANDLE_VALUE) ? true : false;
00059 }
00060
00061
00062 int SerialDevice::raw_setBaudrate(long baudrate) {
00063 DCB dcb;
00064 GetCommState(hComm, &dcb);
00065 dcb.BaudRate = baudrate;
00066 dcb.ByteSize = 8;
00067 dcb.Parity = NOPARITY;
00068 dcb.fParity = FALSE;
00069 dcb.StopBits = ONESTOPBIT;
00070 SetCommState(hComm, &dcb);
00071
00072 return 0;
00073 }
00074
00075
00076 int SerialDevice::raw_send(const char* data, int len) {
00077 DWORD n;
00078 WriteFile(hComm, data, len, &n, NULL);
00079 return n;
00080 }
00081
00082
00083 void SerialDevice::raw_flush(void) {
00084 }
00085
00086
00087 void SerialDevice::raw_check(int require_size, long timeout) {
00088 if (require_size <= 0) {
00089 return;
00090 }
00091
00092 int read_size = require_size;
00093 if (timeout > 0) {
00094 COMMTIMEOUTS pcto;
00095 GetCommTimeouts(hComm, &pcto);
00096 pcto.ReadIntervalTimeout = 0;
00097 pcto.ReadTotalTimeoutConstant = timeout;
00098 pcto.ReadTotalTimeoutMultiplier = 2;
00099 SetCommTimeouts(hComm, &pcto);
00100
00101 } else {
00102 DWORD dwErrors;
00103 COMSTAT ComStat;
00104 ClearCommError(hComm, &dwErrors, &ComStat);
00105 read_size = ((int)ComStat.cbInQue > require_size)
00106 ? require_size : ComStat.cbInQue;
00107 }
00108 char buffer[BufferSize];
00109 DWORD n;
00110 ReadFile(hComm, buffer, read_size, &n, NULL);
00111 recv_buffer->put(buffer, n);
00112 }
00113
00114
00115 #if 0
00116 std::vector<std::string> SerialDevice::getComPorts(void) {
00117 std::vector<std::string> ports;
00118
00119 HKEY hkey;
00120 RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0,
00121 KEY_READ, &hkey);
00122
00123 enum { MaxLength = 32, };
00124 CHAR device[MaxLength];
00125 char name[MaxLength];
00126 DWORD ret = ERROR_SUCCESS;
00127 for (int i = 0; ret == ERROR_SUCCESS; ++i) {
00128 DWORD dl = MaxLength, nl = MaxLength;
00129 ret = RegEnumValue(hkey, i, device, &dl, NULL, NULL, (BYTE*)name, &nl);
00130 if (ret == ERROR_SUCCESS) {
00131 ports.push_back(std::string(name));
00132 }
00133 }
00134 RegCloseKey(hkey);
00135
00136 return ports;
00137 }
00138 #endif
00139