Drain consumes pending bytes on the port without blocking, then restores the normal read timeout. Call before Cmd / Get to clear stale or asynchronous bytes left over from a previous interaction.
()
| 121 | // the normal read timeout. Call before Cmd / Get to clear stale or |
| 122 | // asynchronous bytes left over from a previous interaction. |
| 123 | func (m *Mac) Drain() { |
| 124 | if err := m.port.SetReadTimeout(drainReadTimeout); err != nil { |
| 125 | return |
| 126 | } |
| 127 | defer func() { _ = m.port.SetReadTimeout(normalReadTimeout) }() |
| 128 | tmp := make([]byte, 256) |
| 129 | for { |
| 130 | n, err := m.port.Read(tmp) |
| 131 | if err != nil || n == 0 { |
| 132 | return |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // ReadResponse reads from the port until it sees a "\r\n" terminator or a |
| 138 | // read error. Returns the response without the terminator. A read that |