<commit_before>package serial

// #include <windows.h>
import "C"

import (
	"fmt"
	"syscall"
)

type port struct {
	handle syscall.Handle
}

// New allocates and returns a new serial port controller.
func New() Port {
	return &port{
		handle: syscall.InvalidHandle,
	}
}

// Open connects to the given serial port.
func (p *port) Open(c *Config) (err error) {
	handle, err := newHandle(c)
	if err != nil {
		return
	}
	// Read and write timeout
	if c.Timeout > 0 {
		timeout := C.DWORD(c.Timeout.Nanoseconds() / 1E6)
		var timeouts C.COMMTIMEOUTS
		// wait until a byte arrived or time out
		timeouts.ReadIntervalTimeout = C.MAXDWORD
		timeouts.ReadTotalTimeoutMultiplier = C.MAXDWORD
		timeouts.ReadTotalTimeoutConstant = timeout
		timeouts.WriteTotalTimeoutMultiplier = 0
		timeouts.WriteTotalTimeoutConstant = timeout
		if C.SetCommTimeouts(C.HANDLE(handle), &timeouts) == 0 {
			err = fmt.Errorf("serial: could not set timeout: %v", syscall.GetLastError())
			syscall.CloseHandle(handle)
			return
		}
	}
	p.handle = handle
	return
}

func (p *port) Close() (err error) {
	if p.handle == syscall.InvalidHandle {
		return
	}
	err = syscall.CloseHandle(p.handle)
	p.handle = syscall.InvalidHandle
	return
}

// Read reads from serial port.
// It is blocked until data received or timeout after p.timeout.
func (p *port) Read(b []byte) (n int, err error) {
	var done uint32
	if err = syscall.ReadFile(p.handle, b, &done, nil); err != nil {
		return
	}
	if done == 0 {
		err = ErrTimeout
		return
	}
	n = int(done)
	return
}

// Write writes data to the serial port.
func (p *port) Write(b []byte) (n int, err error) {
	var done uint32
	if err = syscall.WriteFile(p.handle, b, &done, nil); err != nil {
		return
	}
	n = int(done)
	return
}

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
}
<commit_msg>Set default baudrate in Windows<commit_after>package serial

// #include <windows.h>
import "C"

import (
	"fmt"
	"syscall"
)

type port struct {
	handle syscall.Handle
}

// New allocates and returns a new serial port controller.
func New() Port {
	return &port{
		handle: syscall.InvalidHandle,
	}
}

// Open connects to the given serial port.
func (p *port) Open(c *Config) (err error) {
	handle, err := newHandle(c)
	if err != nil {
		return
	}
	// Read and write timeout
	if c.Timeout > 0 {
		timeout := C.DWORD(c.Timeout.Nanoseconds() / 1E6)
		var timeouts C.COMMTIMEOUTS
		// wait until a byte arrived or time out
		timeouts.ReadIntervalTimeout = C.MAXDWORD
		timeouts.ReadTotalTimeoutMultiplier = C.MAXDWORD
		timeouts.ReadTotalTimeoutConstant = timeout
		timeouts.WriteTotalTimeoutMultiplier = 0
		timeouts.WriteTotalTimeoutConstant = timeout
		if C.SetCommTimeouts(C.HANDLE(handle), &timeouts) == 0 {
			err = fmt.Errorf("serial: could not set timeout: %v", syscall.GetLastError())
			syscall.CloseHandle(handle)
			return
		}
	}
	p.handle = handle
	return
}

func (p *port) Close() (err error) {
	if p.handle == syscall.InvalidHandle {
		return
	}
	err = syscall.CloseHandle(p.handle)
	p.handle = syscall.InvalidHandle
	return
}

// Read reads from serial port.
// It is blocked until data received or timeout after p.timeout.
func (p *port) Read(b []byte) (n int, err error) {
	var done uint32
	if err = syscall.ReadFile(p.handle, b, &done, nil); err != nil {
		return
	}
	if done == 0 {
		err = ErrTimeout
		return
	}
	n = int(done)
	return
}

// Write writes data to the serial port.
func (p *port) Write(b []byte) (n int, err error) {
	var done uint32
	if err = syscall.WriteFile(p.handle, b, &done, nil); err != nil {
		return
	}
	n = int(done)
	return
}

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
	if c.BaudRate == 0 {
		dcb.BaudRate = 19200
	} else {
		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
}

{'fraction_non_alphanumeric': 0.129349092725154, 'fraction_numerical': 0.007990677542866656, 'mean_word_length': 3.2700781805259416, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6228139', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2220, 'n_words': 626}