Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: chrome/browser/extensions/api/serial/serial_connection_win.cc

Issue 22804008: Adds Serial API to set data bits, parity, stop bits. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/serial/serial_connection.h" 5 #include "chrome/browser/extensions/api/serial/serial_connection.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include <string> 9 #include <string>
10 10
11 namespace extensions { 11 namespace extensions {
12 12
13 namespace {
14 int getBaudRate(int bitrate_) {
15 switch (bitrate_) {
16 case 110: return CBR_110;
17 case 300: return CBR_300;
18 case 600: return CBR_600;
19 case 1200: return CBR_1200;
20 case 2400: return CBR_2400;
21 case 4800: return CBR_4800;
22 case 9600: return CBR_9600;
23 case 14400: return CBR_14400;
24 case 19200: return CBR_19200;
25 case 38400: return CBR_38400;
26 case 57600: return CBR_57600;
27 case 115200: return CBR_115200;
28 case 128000: return CBR_128000;
29 case 256000: return CBR_256000;
30 default: return CBR_9600;
31 }
32 }
33
34 int getDataBit(serial::DataBit databit) {
35 switch (databit) {
36 case serial::DATA_BIT_SEVENBIT:
37 return 7;
38 case serial::DATA_BIT_EIGHTBIT:
39 return 8;
40 default:
41 return 8;
42 }
43 }
44
45 int getParity(serial::ParityBit parity) {
46 switch (parity) {
47 case serial::PARITY_BIT_EVENPARITY:
48 return EVENPARITY;
49 case serial::PARITY_BIT_NOPARITY:
50 return NOPARITY;
51 case serial::PARITY_BIT_ODDPARITY:
52 return SPACEPARITY;
53 default:
54 return NOPARITY;
55 }
56 }
57
58 int getStopBit(serial::StopBit stopbit) {
59 switch (stopbit) {
60 case serial::STOP_BIT_ONESTOPBIT:
61 return ONESTOPBIT;
62 case serial::STOP_BIT_TWOSTOPBIT:
63 return TWOSTOPBITS;
64 default:
65 return ONESTOPBIT;
66 }
67 }
68 } // namespace
69
13 bool SerialConnection::PostOpen() { 70 bool SerialConnection::PostOpen() {
14 // Set timeouts so that reads return immediately with whatever could be read 71 // Set timeouts so that reads return immediately with whatever could be read
15 // without blocking. 72 // without blocking.
16 COMMTIMEOUTS timeouts = { 0 }; 73 COMMTIMEOUTS timeouts = { 0 };
17 timeouts.ReadIntervalTimeout = MAXDWORD; 74 timeouts.ReadIntervalTimeout = MAXDWORD;
18 if (!::SetCommTimeouts(file_, &timeouts)) 75 if (!::SetCommTimeouts(file_, &timeouts))
19 return false; 76 return false;
20 77
21 DCB dcb = { 0 }; 78 DCB dcb = { 0 };
22 dcb.DCBlength = sizeof(dcb); 79 dcb.DCBlength = sizeof(dcb);
23 if (!GetCommState(file_, &dcb)) 80 if (!GetCommState(file_, &dcb))
24 return false; 81 return false;
25 82
26 if (bitrate_ >= 0) { 83 dcb.BaudRate = getBaudRate(bitrate_);
27 bool speed_found = true; 84 dcb.ByteSize = getDataBit(databit_);
28 DWORD speed = CBR_9600; 85 dcb.Parity = getParity(parity_);
29 switch (bitrate_) { 86 dcb.StopBits = getStopBit(stopbit_);
30 case 110: speed = CBR_110; break; 87
31 case 300: speed = CBR_300; break;
32 case 600: speed = CBR_600; break;
33 case 1200: speed = CBR_1200; break;
34 case 2400: speed = CBR_2400; break;
35 case 4800: speed = CBR_4800; break;
36 case 9600: speed = CBR_9600; break;
37 case 14400: speed = CBR_14400; break;
38 case 19200: speed = CBR_19200; break;
39 case 38400: speed = CBR_38400; break;
40 case 57600: speed = CBR_57600; break;
41 case 115200: speed = CBR_115200; break;
42 case 128000: speed = CBR_128000; break;
43 case 256000: speed = CBR_256000; break;
44 default: speed_found = false; break;
45 }
46 if (speed_found)
47 dcb.BaudRate = speed;
48 }
49 dcb.ByteSize = 8;
50 dcb.StopBits = ONESTOPBIT;
51 dcb.Parity = NOPARITY;
52 if (!SetCommState(file_, &dcb)) 88 if (!SetCommState(file_, &dcb))
53 return false; 89 return false;
54 90
55 return true; 91 return true;
56 } 92 }
57 93
58 bool SerialConnection::GetControlSignals(ControlSignals &control_signals) { 94 bool SerialConnection::GetControlSignals(ControlSignals &control_signals) {
59 DWORD dwModemStatus; 95 DWORD dwModemStatus;
60 if (!GetCommModemStatus(file_, &dwModemStatus)) 96 if (!GetCommModemStatus(file_, &dwModemStatus))
61 return false; 97 return false;
(...skipping 19 matching lines...) Expand all
81 const std::string &port_name) { 117 const std::string &port_name) {
82 // For COM numbers less than 9, CreateFile is called with a string such as 118 // For COM numbers less than 9, CreateFile is called with a string such as
83 // "COM1". For numbers greater than 9, a prefix of "\\\\.\\" must be added. 119 // "COM1". For numbers greater than 9, a prefix of "\\\\.\\" must be added.
84 if (port_name.length() > std::string("COM9").length()) 120 if (port_name.length() > std::string("COM9").length())
85 return std::string("\\\\.\\").append(port_name); 121 return std::string("\\\\.\\").append(port_name);
86 122
87 return port_name; 123 return port_name;
88 } 124 }
89 125
90 } // namespace extensions 126 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698