Beispiel #1
0
// Implementation of io.Writer interface.
func (p *Port) Write(b []byte) (int, error) {
	var c int32
	var start time.Time

	if Debug {
		start = time.Now()
	}

	buf, size := unsafe.Pointer(&b[0]), C.size_t(len(b))

	if p.writeDeadline.IsZero() {

		// no deadline
		c = C.sp_blocking_write(p.p, buf, size, 0)

	} else if millis := deadline2millis(p.writeDeadline); millis <= 0 {

		// call nonblocking write
		c = C.sp_nonblocking_write(p.p, buf, size)

	} else {

		// call blocking write
		c = C.sp_blocking_write(p.p, buf, size, C.uint(millis))

	}

	if Debug {
		log.Printf("write time: %d ns", time.Since(start).Nanoseconds())
	}

	n := int(c)

	// check for error
	if n < 0 {
		return 0, errmsg(c)
	} else if n != len(b) {
		return n, ErrTimeout
	}

	return n, nil
}
Beispiel #2
0
// Write as BlockingWrite but non-blocking and no timeout.
func Write(port Port, txBuf []byte) (int32, error) {
	var result int32 = SP_OK
	result = C.sp_nonblocking_write(port, (unsafe.Pointer(&txBuf[0])), C.size_t(len(txBuf)))
	return result, checkResult(result)
}