Пример #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
}
Пример #2
0
// BlockingWrite attempts to write the string or buffer supplied to the port
// blocks while writing, timeout is the number of mS to wait or set 0 to wait
// indefinitely.
func BlockingWrite(port Port, txBuf []byte, timeout uint16) (int32, error) {
	var result int32 = SP_OK
	result = C.sp_blocking_write(port, (unsafe.Pointer(&txBuf[0])), C.size_t(len(txBuf)), C.uint(timeout))
	return result, checkResult(result)
}