~vibhavp/ubuntu/saucy/urg/merge-from-debian

« back to all changes in this revision

Viewing changes to src/cpp/connection/SerialDevice_win.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Albert Huang
  • Date: 2011-05-20 11:33:03 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110520113303-u8niofzwzcea0osk
Tags: 0.8.12-1
* New upstream release (closes: #624987)
* Add debian/watch file
* Bump standards-version to 3.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
  \author Satofumi KAMIMURA
6
6
 
7
 
  $Id: SerialDevice_win.cpp 1636 2010-01-21 12:50:34Z satofumi $
 
7
  $Id: SerialDevice_win.cpp 1811 2010-04-30 16:12:05Z satofumi $
8
8
*/
9
9
 
10
10
#include "DetectOS.h"
25
25
 
26
26
class RawSerialDevice
27
27
{
28
 
  string error_message_;
29
 
  HANDLE hCom_;
30
 
  int current_timeout_;
31
 
  string com_name_;
 
28
    string error_message_;
 
29
    HANDLE hCom_;
 
30
    int current_timeout_;
 
31
    string com_name_;
32
32
 
33
33
 
34
34
public:
35
 
  RawSerialDevice(void)
36
 
    : error_message_("no error."), hCom_(INVALID_HANDLE_VALUE),
37
 
      current_timeout_(0)
38
 
  {
39
 
  }
40
 
 
41
 
 
42
 
  const char* what(void)
43
 
  {
44
 
    return error_message_.c_str();
45
 
  }
46
 
 
47
 
 
48
 
  bool connect(const char* device, long baudrate)
49
 
  {
50
 
    /* COM �|�[�g���J�� */
51
 
    enum { NameLength = 11 };
52
 
    char adjusted_device[NameLength];
53
 
    snprintf(adjusted_device, NameLength, "\\\\.\\%s", device);
54
 
    hCom_ = CreateFileA(adjusted_device, GENERIC_READ | GENERIC_WRITE, 0,
55
 
                        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
56
 
 
57
 
    if (hCom_ == INVALID_HANDLE_VALUE) {
58
 
      error_message_ = string("open failed: ") + device;
59
 
      return false;
60
 
    }
61
 
    com_name_ = device;
62
 
 
63
 
    // �ʐM�T�C�Y�̍X�V
64
 
    SetupComm(hCom_, 4096 * 8, 4096);
65
 
 
66
 
    // �^�C���A�E�g�ݒ�
67
 
    setTimeout(current_timeout_);
68
 
 
69
 
    // �{�[���[�g�̕ύX
70
 
    bool ret = setBaudrate(baudrate);
71
 
    if (! ret) {
72
 
      error_message_ = "fail SerialDevice::setBaudrate()";
73
 
    }
74
 
 
75
 
    return true;
76
 
  }
77
 
 
78
 
 
79
 
  void setTimeout(int timeout)
80
 
  {
81
 
    COMMTIMEOUTS timeouts;
82
 
    GetCommTimeouts(hCom_, &timeouts);
83
 
 
84
 
    timeouts.ReadIntervalTimeout = (timeout == 0) ? MAXDWORD : 0;
85
 
    timeouts.ReadTotalTimeoutConstant = timeout;
86
 
    timeouts.ReadTotalTimeoutMultiplier = 0;
87
 
 
88
 
    SetCommTimeouts(hCom_, &timeouts);
89
 
  }
90
 
 
91
 
 
92
 
 
93
 
  void disconnect(void)
94
 
  {
95
 
    if (hCom_ != INVALID_HANDLE_VALUE) {
96
 
      CloseHandle(hCom_);
97
 
      hCom_ = INVALID_HANDLE_VALUE;
98
 
    }
99
 
  }
100
 
 
101
 
 
102
 
  bool isConnected(void)
103
 
  {
104
 
    return (hCom_ == INVALID_HANDLE_VALUE) ? false : true;
105
 
  }
106
 
 
107
 
 
108
 
  bool setBaudrate(long baudrate)
109
 
  {
110
 
    long baudrate_value;
111
 
    switch (baudrate) {
112
 
 
113
 
    case 4800:
114
 
      baudrate_value = CBR_4800;
115
 
      break;
116
 
 
117
 
    case 9600:
118
 
      baudrate_value = CBR_9600;
119
 
      break;
120
 
 
121
 
    case 19200:
122
 
      baudrate_value = CBR_19200;
123
 
      break;
124
 
 
125
 
    case 38400:
126
 
      baudrate_value = CBR_38400;
127
 
      break;
128
 
 
129
 
    case 57600:
130
 
      baudrate_value = CBR_57600;
131
 
      break;
132
 
 
133
 
    case 115200:
134
 
      baudrate_value = CBR_115200;
135
 
      break;
136
 
 
137
 
    default:
138
 
      baudrate_value = baudrate;
139
 
    }
140
 
 
141
 
    DCB dcb;
142
 
    GetCommState(hCom_, &dcb);
143
 
    dcb.BaudRate = baudrate_value;
144
 
    dcb.ByteSize = 8;
145
 
    dcb.Parity = NOPARITY;
146
 
    dcb.fParity = FALSE;
147
 
    dcb.StopBits = ONESTOPBIT;
148
 
    if (SetCommState(hCom_, &dcb) == 0) {
149
 
      flush();
150
 
      return false;
151
 
    } else {
152
 
      return true;
153
 
    }
154
 
  }
155
 
 
156
 
 
157
 
  int send(const char* data, size_t count)
158
 
  {
159
 
    if (count <= 0) {
160
 
      return 0;
161
 
    }
162
 
 
163
 
    DWORD n;
164
 
    WriteFile(hCom_, data, (DWORD)count, &n, NULL);
165
 
    return n;
166
 
  }
167
 
 
168
 
 
169
 
  int receive(char data[], int count, int timeout)
170
 
  {
171
 
    if (count <= 0) {
172
 
      return 0;
173
 
    }
174
 
 
175
 
    if (timeout != current_timeout_) {
176
 
      setTimeout(timeout);
177
 
      current_timeout_ = timeout;
178
 
    }
179
 
 
180
 
    DWORD n;
181
 
    ReadFile(hCom_, data, count, &n, NULL);
182
 
 
183
 
    return n;
184
 
  }
185
 
 
186
 
 
187
 
  void flush(void)
188
 
  {
189
 
    PurgeComm(hCom_,
190
 
              PURGE_RXABORT | PURGE_TXABORT | PURGE_RXCLEAR | PURGE_TXCLEAR);
191
 
  }
 
35
    RawSerialDevice(void)
 
36
        : error_message_("no error."), hCom_(INVALID_HANDLE_VALUE),
 
37
          current_timeout_(0)
 
38
    {
 
39
    }
 
40
 
 
41
 
 
42
    const char* what(void)
 
43
    {
 
44
        return error_message_.c_str();
 
45
    }
 
46
 
 
47
 
 
48
    bool connect(const char* device, long baudrate)
 
49
    {
 
50
        /* COM �|�[�g���J�� */
 
51
        enum { NameLength = 11 };
 
52
        char adjusted_device[NameLength];
 
53
        snprintf(adjusted_device, NameLength, "\\\\.\\%s", device);
 
54
        hCom_ = CreateFileA(adjusted_device, GENERIC_READ | GENERIC_WRITE, 0,
 
55
                            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 
56
 
 
57
        if (hCom_ == INVALID_HANDLE_VALUE) {
 
58
            error_message_ = string("open failed: ") + device;
 
59
            return false;
 
60
        }
 
61
        com_name_ = device;
 
62
 
 
63
        // �ʐM�T�C�Y�̍X�V
 
64
        SetupComm(hCom_, 4096 * 8, 4096);
 
65
 
 
66
        // �^�C���A�E�g�ݒ�
 
67
        setTimeout(current_timeout_);
 
68
 
 
69
        // �{�[���[�g�̕ύX
 
70
        bool ret = setBaudrate(baudrate);
 
71
        if (! ret) {
 
72
            error_message_ = "fail SerialDevice::setBaudrate()";
 
73
        }
 
74
 
 
75
        return true;
 
76
    }
 
77
 
 
78
 
 
79
    void setTimeout(int timeout)
 
80
    {
 
81
        COMMTIMEOUTS timeouts;
 
82
        GetCommTimeouts(hCom_, &timeouts);
 
83
 
 
84
        timeouts.ReadIntervalTimeout = (timeout == 0) ? MAXDWORD : 0;
 
85
        timeouts.ReadTotalTimeoutConstant = timeout;
 
86
        timeouts.ReadTotalTimeoutMultiplier = 0;
 
87
 
 
88
        SetCommTimeouts(hCom_, &timeouts);
 
89
    }
 
90
 
 
91
 
 
92
 
 
93
    void disconnect(void)
 
94
    {
 
95
        if (hCom_ != INVALID_HANDLE_VALUE) {
 
96
            CloseHandle(hCom_);
 
97
            hCom_ = INVALID_HANDLE_VALUE;
 
98
        }
 
99
    }
 
100
 
 
101
 
 
102
    bool isConnected(void)
 
103
    {
 
104
        return (hCom_ == INVALID_HANDLE_VALUE) ? false : true;
 
105
    }
 
106
 
 
107
 
 
108
    bool setBaudrate(long baudrate)
 
109
    {
 
110
        long baudrate_value;
 
111
        switch (baudrate) {
 
112
 
 
113
        case 4800:
 
114
            baudrate_value = CBR_4800;
 
115
            break;
 
116
 
 
117
        case 9600:
 
118
            baudrate_value = CBR_9600;
 
119
            break;
 
120
 
 
121
        case 19200:
 
122
            baudrate_value = CBR_19200;
 
123
            break;
 
124
 
 
125
        case 38400:
 
126
            baudrate_value = CBR_38400;
 
127
            break;
 
128
 
 
129
        case 57600:
 
130
            baudrate_value = CBR_57600;
 
131
            break;
 
132
 
 
133
        case 115200:
 
134
            baudrate_value = CBR_115200;
 
135
            break;
 
136
 
 
137
        default:
 
138
            baudrate_value = baudrate;
 
139
        }
 
140
 
 
141
        DCB dcb;
 
142
        GetCommState(hCom_, &dcb);
 
143
        dcb.BaudRate = baudrate_value;
 
144
        dcb.ByteSize = 8;
 
145
        dcb.Parity = NOPARITY;
 
146
        dcb.fParity = FALSE;
 
147
        dcb.StopBits = ONESTOPBIT;
 
148
        if (SetCommState(hCom_, &dcb) == 0) {
 
149
            flush();
 
150
            return false;
 
151
        } else {
 
152
            return true;
 
153
        }
 
154
    }
 
155
 
 
156
 
 
157
    int send(const char* data, size_t count)
 
158
    {
 
159
        if (count <= 0) {
 
160
            return 0;
 
161
        }
 
162
 
 
163
        DWORD n;
 
164
        WriteFile(hCom_, data, (DWORD)count, &n, NULL);
 
165
        return n;
 
166
    }
 
167
 
 
168
 
 
169
    int receive(char data[], int count, int timeout)
 
170
    {
 
171
        if (count <= 0) {
 
172
            return 0;
 
173
        }
 
174
 
 
175
        if (timeout != current_timeout_) {
 
176
            setTimeout(timeout);
 
177
            current_timeout_ = timeout;
 
178
        }
 
179
 
 
180
        DWORD n;
 
181
        ReadFile(hCom_, data, count, &n, NULL);
 
182
 
 
183
        return n;
 
184
    }
 
185
 
 
186
 
 
187
    void flush(void)
 
188
    {
 
189
        PurgeComm(hCom_,
 
190
                  PURGE_RXABORT | PURGE_TXABORT |
 
191
                  PURGE_RXCLEAR | PURGE_TXCLEAR);
 
192
    }
192
193
};