...

Source file src/github.com/jacobsa/go-serial/serial/integration_test.go

Documentation: github.com/jacobsa/go-serial/serial

     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  // Integration tests for the serial package.
    16  
    17  package serial
    18  
    19  import (
    20  	"errors"
    21  	"io"
    22  )
    23  
    24  import "testing"
    25  import "time"
    26  
    27  const (
    28  	DEVICE = "/dev/tty.usbserial-A8008HlV"
    29  )
    30  
    31  //////////////////////////////////////////////////////
    32  // Helpers
    33  //////////////////////////////////////////////////////
    34  
    35  // Read at least n bytes from an io.Reader, making sure not to block if it
    36  // takes too long.
    37  func readWithTimeout(r io.Reader, n int) ([]byte, error) {
    38  	buf := make([]byte, n)
    39  	done := make(chan error)
    40  	readAndCallBack := func() {
    41  		_, err := io.ReadAtLeast(r, buf, n)
    42  		done <- err
    43  	}
    44  
    45  	go readAndCallBack()
    46  
    47  	timeout := make(chan bool)
    48  	sleepAndCallBack := func() { time.Sleep(2e9); timeout <- true }
    49  	go sleepAndCallBack()
    50  
    51  	select {
    52  	case err := <-done:
    53  		return buf, err
    54  	case <-timeout:
    55  		return nil, errors.New("Timed out.")
    56  	}
    57  
    58  	return nil, errors.New("Can't get here.")
    59  }
    60  
    61  //////////////////////////////////////////////////////
    62  // Tests
    63  //////////////////////////////////////////////////////
    64  
    65  // The device is assumed to be running the increment_and_echo program from the
    66  // hardware directory.
    67  func TestIncrementAndEcho(t *testing.T) {
    68  	// Open the port.
    69  	var options OpenOptions
    70  	options.PortName = DEVICE
    71  	options.BaudRate = 19200
    72  	options.DataBits = 8
    73  	options.StopBits = 1
    74  	options.MinimumReadSize = 4
    75  
    76  	circuit, err := Open(options)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	defer circuit.Close()
    82  
    83  	// Pause for a few seconds to deal with the Arduino's annoying startup delay.
    84  	time.Sleep(3e9)
    85  
    86  	// Write some bytes.
    87  	b := []byte{0x00, 0x17, 0xFE, 0xFF}
    88  
    89  	n, err := circuit.Write(b)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	if n != 4 {
    95  		t.Fatal("Expected 4 bytes written, got ", n)
    96  	}
    97  
    98  	// Check the response.
    99  	b, err = readWithTimeout(circuit, 4)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	if b[0] != 0x01 {
   105  		t.Error("Expected 0x01, got ", b[0])
   106  	}
   107  	if b[1] != 0x18 {
   108  		t.Error("Expected 0x18, got ", b[1])
   109  	}
   110  	if b[2] != 0xFF {
   111  		t.Error("Expected 0xFF, got ", b[2])
   112  	}
   113  	if b[3] != 0x00 {
   114  		t.Error("Expected 0x00, got ", b[3])
   115  	}
   116  }
   117  

View as plain text