Example #1
0
func newHandle(c *Config) (handle syscall.Handle, err error) {
	handle, err = syscall.CreateFile(
		syscall.StringToUTF16Ptr(c.Address),
		syscall.GENERIC_READ|syscall.GENERIC_WRITE,
		0,   // mode
		nil, // security
		syscall.OPEN_EXISTING, // create mode
		0, // attributes
		0) // templates
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			syscall.CloseHandle(handle)
		}
	}()
	var dcb C.DCB
	dcb.BaudRate = C.DWORD(c.BaudRate)
	// Data bits
	if c.DataBits == 0 {
		dcb.ByteSize = 8
	} else {
		dcb.ByteSize = C.BYTE(c.DataBits)
	}
	// Stop bits
	switch c.StopBits {
	case 0, 1:
		// Default is one stop bit.
		dcb.StopBits = C.ONESTOPBIT
	case 2:
		dcb.StopBits = C.TWOSTOPBITS
	default:
		err = fmt.Errorf("serial: unsupported stop bits %v", c.StopBits)
		return
	}
	// Parity
	switch c.Parity {
	case "", "E":
		// Default parity mode is Even.
		dcb.Parity = C.EVENPARITY
	case "O":
		dcb.Parity = C.ODDPARITY
	case "N":
		dcb.Parity = C.NOPARITY
	default:
		err = fmt.Errorf("serial: unsupported parity %v", c.Parity)
		return
	}
	if C.SetCommState(C.HANDLE(handle), &dcb) == 0 {
		err = fmt.Errorf("serial: could not set device state: %v", syscall.GetLastError())
		return
	}
	return
}
Example #2
0
func (com *WinCom) Set(info *ComInfo) error {
	var dcb C.DCB
	dcb.BaudRate = C.DWORD(info.BaudRate)
	dcb.ByteSize = C.BYTE(info.DataBit)
	if info.Parity == NPBit {
		dcb.Parity = C.NOPARITY
	}
	if info.StopBits == SB1 {
		dcb.StopBits = C.ONESTOPBIT
	}
	ret := C.com_set(C.int(com.fd), &dcb)
	if ret < 0 {
		return &WinComErr{"Set Com", "fail"}
	}
	return nil
}
Example #3
0
func main() {
	handle, err := syscall.CreateFile(syscall.StringToUTF16Ptr(port),
		syscall.GENERIC_READ|syscall.GENERIC_WRITE,
		0,   // mode
		nil, // security
		syscall.OPEN_EXISTING, // no creating new
		0,
		0)
	if err != nil {
		fmt.Print(err)
		return
	}
	fmt.Printf("handle created %d\n", handle)
	defer syscall.CloseHandle(handle)

	var dcb C.DCB
	dcb.BaudRate = 9600
	dcb.ByteSize = 8
	dcb.StopBits = C.ONESTOPBIT
	dcb.Parity = C.NOPARITY
	if C.SetCommState(C.HANDLE(handle), &dcb) == 0 {
		fmt.Printf("set comm state error %v\n", syscall.GetLastError())
		return
	}
	fmt.Printf("set comm state succeed\n")

	var timeouts C.COMMTIMEOUTS
	// time-out between charactor for receiving (ms)
	timeouts.ReadIntervalTimeout = 1000
	timeouts.ReadTotalTimeoutMultiplier = 0
	timeouts.ReadTotalTimeoutConstant = 1000
	timeouts.WriteTotalTimeoutMultiplier = 0
	timeouts.WriteTotalTimeoutConstant = 1000
	if C.SetCommTimeouts(C.HANDLE(handle), &timeouts) == 0 {
		fmt.Printf("set comm timeouts error %v\n", syscall.GetLastError())
		return
	}
	fmt.Printf("set comm timeouts succeed\n")

	var n uint32
	data := []byte("abc")
	err = syscall.WriteFile(handle, data, &n, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("write file succeed\n")
	fmt.Printf("Press Enter when ready for reading...")
	reader := bufio.NewReader(os.Stdin)
	_, _ = reader.ReadString('\n')

	data = make([]byte, 512)
	err = syscall.ReadFile(handle, data, &n, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("received data %v:\n", n)
	fmt.Printf("%x\n", data[:n])
	fmt.Printf("closed\n")
}