1 // Copyright 2011 Aaron Jacobs. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package serial provides routines for interacting with serial ports. 16 // Currently it supports only OS X; see the readme file for details. 17 18 package serial 19 20 import ( 21 "io" 22 "math" 23 ) 24 25 // Valid parity values. 26 type ParityMode int 27 28 const ( 29 PARITY_NONE ParityMode = 0 30 PARITY_ODD ParityMode = 1 31 PARITY_EVEN ParityMode = 2 32 ) 33 34 var ( 35 // The list of standard baud-rates. 36 StandardBaudRates = map[uint]bool{ 37 50: true, 38 75: true, 39 110: true, 40 134: true, 41 150: true, 42 200: true, 43 300: true, 44 600: true, 45 1200: true, 46 1800: true, 47 2400: true, 48 4800: true, 49 7200: true, 50 9600: true, 51 14400: true, 52 19200: true, 53 28800: true, 54 38400: true, 55 57600: true, 56 76800: true, 57 115200: true, 58 230400: true, 59 } 60 ) 61 62 // IsStandardBaudRate checks whether the specified baud-rate is standard. 63 // 64 // Some operating systems may support non-standard baud-rates (OSX) via 65 // additional IOCTL. 66 func IsStandardBaudRate(baudRate uint) bool { return StandardBaudRates[baudRate] } 67 68 // OpenOptions is the struct containing all of the options necessary for 69 // opening a serial port. 70 type OpenOptions struct { 71 // The name of the port, e.g. "/dev/tty.usbserial-A8008HlV". 72 PortName string 73 74 // The baud rate for the port. 75 BaudRate uint 76 77 // The number of data bits per frame. Legal values are 5, 6, 7, and 8. 78 DataBits uint 79 80 // The number of stop bits per frame. Legal values are 1 and 2. 81 StopBits uint 82 83 // The type of parity bits to use for the connection. Currently parity errors 84 // are simply ignored; that is, bytes are delivered to the user no matter 85 // whether they were received with a parity error or not. 86 ParityMode ParityMode 87 88 // Enable RTS/CTS (hardware) flow control. 89 RTSCTSFlowControl bool 90 91 // An inter-character timeout value, in milliseconds, and a minimum number of 92 // bytes to block for on each read. A call to Read() that otherwise may block 93 // waiting for more data will return immediately if the specified amount of 94 // time elapses between successive bytes received from the device or if the 95 // minimum number of bytes has been exceeded. 96 // 97 // Note that the inter-character timeout value may be rounded to the nearest 98 // 100 ms on some systems, and that behavior is undefined if calls to Read 99 // supply a buffer whose length is less than the minimum read size. 100 // 101 // Behaviors for various settings for these values are described below. For 102 // more information, see the discussion of VMIN and VTIME here: 103 // 104 // http://www.unixwiz.net/techtips/termios-vmin-vtime.html 105 // 106 // InterCharacterTimeout = 0 and MinimumReadSize = 0 (the default): 107 // This arrangement is not legal; you must explicitly set at least one of 108 // these fields to a positive number. (If MinimumReadSize is zero then 109 // InterCharacterTimeout must be at least 100.) 110 // 111 // InterCharacterTimeout > 0 and MinimumReadSize = 0 112 // If data is already available on the read queue, it is transferred to 113 // the caller's buffer and the Read() call returns immediately. 114 // Otherwise, the call blocks until some data arrives or the 115 // InterCharacterTimeout milliseconds elapse from the start of the call. 116 // Note that in this configuration, InterCharacterTimeout must be at 117 // least 100 ms. 118 // 119 // InterCharacterTimeout > 0 and MinimumReadSize > 0 120 // Calls to Read() return when at least MinimumReadSize bytes are 121 // available or when InterCharacterTimeout milliseconds elapse between 122 // received bytes. The inter-character timer is not started until the 123 // first byte arrives. 124 // 125 // InterCharacterTimeout = 0 and MinimumReadSize > 0 126 // Calls to Read() return only when at least MinimumReadSize bytes are 127 // available. The inter-character timer is not used. 128 // 129 // For windows usage, these options (termios) do not conform well to the 130 // windows serial port / comms abstractions. Please see the code in 131 // open_windows setCommTimeouts function for full documentation. 132 // Summary: 133 // Setting MinimumReadSize > 0 will cause the serialPort to block until 134 // until data is available on the port. 135 // Setting IntercharacterTimeout > 0 and MinimumReadSize == 0 will cause 136 // the port to either wait until IntercharacterTimeout wait time is 137 // exceeded OR there is character data to return from the port. 138 // 139 140 InterCharacterTimeout uint 141 MinimumReadSize uint 142 143 // Use to enable RS485 mode -- probably only valid on some Linux platforms 144 Rs485Enable bool 145 146 // Set to true for logic level high during send 147 Rs485RtsHighDuringSend bool 148 149 // Set to true for logic level high after send 150 Rs485RtsHighAfterSend bool 151 152 // set to receive data during sending 153 Rs485RxDuringTx bool 154 155 // RTS delay before send 156 Rs485DelayRtsBeforeSend int 157 158 // RTS delay after send 159 Rs485DelayRtsAfterSend int 160 } 161 162 // Open creates an io.ReadWriteCloser based on the supplied options struct. 163 func Open(options OpenOptions) (io.ReadWriteCloser, error) { 164 // Redirect to the OS-specific function. 165 return openInternal(options) 166 } 167 168 // Rounds a float to the nearest integer. 169 func round(f float64) float64 { 170 return math.Floor(f + 0.5) 171 } 172