Skip to content

Commit

Permalink
Gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmigan Casebolt committed Apr 20, 2011
1 parent b650c88 commit 0218f27
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 105 deletions.
18 changes: 9 additions & 9 deletions serial_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func OpenPort(name string, baud int) (rwc io.ReadWriteCloser, err os.Error) {
}

/*
r1, _, e = syscall.Syscall(syscall.SYS_IOCTL,
uintptr(f.Fd()),
uintptr(0x80045402), // IOSSIOSPEED
uintptr(unsafe.Pointer(&baud)));
if e != 0 || r1 != 0 {
s := fmt.Sprint("Baudrate syscall error:", e, r1)
f.Close()
return nil, SError{s}
}
r1, _, e = syscall.Syscall(syscall.SYS_IOCTL,
uintptr(f.Fd()),
uintptr(0x80045402), // IOSSIOSPEED
uintptr(unsafe.Pointer(&baud)));
if e != 0 || r1 != 0 {
s := fmt.Sprint("Baudrate syscall error:", e, r1)
f.Close()
return nil, SError{s}
}
*/

return f, nil
Expand Down
192 changes: 96 additions & 96 deletions serial_windows.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,105 @@
package serial

package serial

// #include <windows.h>
import "C"
import (
"os"
"io"
"fmt"
"unsafe"
)

type serialPort struct {
f *os.File
}

const EV_RXCHAR = 0x0001

func OpenPort(name string, baud int) (io.ReadWriteCloser, os.Error) {
f, err := os.Open(name, os.O_RDWR|os.O_NDELAY, 0666)
if err != nil {
return nil, err
}

type hp *C.HANDLE
var handle *C.HANDLE
fd := f.Fd()
handle = hp(unsafe.Pointer(&fd))
var params C.struct__DCB
params.DCBlength = C.DWORD(unsafe.Sizeof(params))
params.XonLim = 0
params.XoffLim = 0
params.BaudRate = C.DWORD(baud)
params.ByteSize = 8
params.StopBits = C.ONESTOPBIT
params.Parity = C.NOPARITY

import "C"
import (
"os"
"io"
"fmt"
"unsafe"
)

type serialPort struct {
f *os.File
}

const EV_RXCHAR = 0x0001

func OpenPort(name string, baud int) (io.ReadWriteCloser, os.Error) {
f, err := os.Open(name, os.O_RDWR|os.O_NDELAY, 0666)
if err != nil {
return nil, err
}

type hp *C.HANDLE
var handle *C.HANDLE
fd := f.Fd()
handle = hp(unsafe.Pointer(&fd))
var params C.struct__DCB
params.DCBlength = C.DWORD(unsafe.Sizeof(params))

params.XonLim = 0
params.XoffLim = 0

params.BaudRate = C.DWORD(baud)
params.ByteSize = 8
params.StopBits = C.ONESTOPBIT
params.Parity = C.NOPARITY

//fmt.Printf("%#v %v\n", params, params)

if ok, err := C.SetCommState(*handle, &params); ok == C.FALSE {
f.Close()
return nil, err
}


if ok, err := C.SetCommState(*handle, &params); ok == C.FALSE {
f.Close()
return nil, err
}

// Turn off buffers
if ok, err := C.SetupComm(*handle, 16, 16); ok == C.FALSE {
f.Close()
return nil, err
}
var timeouts C.struct__COMMTIMEOUTS
const DWORDMAX = 1<<32 - 1
timeouts.ReadIntervalTimeout = DWORDMAX
timeouts.ReadTotalTimeoutConstant = 0
if ok, err := C.SetCommTimeouts(*handle, &timeouts); ok == C.FALSE {
f.Close()
return nil, err
}
if ok, err := C.SetupComm(*handle, 16, 16); ok == C.FALSE {
f.Close()
return nil, err
}

var timeouts C.struct__COMMTIMEOUTS
const DWORDMAX = 1<<32 - 1
timeouts.ReadIntervalTimeout = DWORDMAX
timeouts.ReadTotalTimeoutConstant = 0
if ok, err := C.SetCommTimeouts(*handle, &timeouts); ok == C.FALSE {
f.Close()
return nil, err
}
//fmt.Printf("%#v\n", timeouts)

if ok, err := C.SetCommMask(*handle, EV_RXCHAR); ok == C.FALSE {
f.Close()
return nil, err
}

port := serialPort{f}

return &port, nil
}

func (p *serialPort) Close() os.Error {
return p.f.Close()
}

func (p *serialPort) Write(buf []byte) (int, os.Error) {
return p.f.Write(buf)
}

func (p *serialPort) Read(buf []byte) (int, os.Error) {
type hp *C.HANDLE
var handle *C.HANDLE
fd := p.f.Fd()
handle = hp(unsafe.Pointer(&fd))

var events C.DWORD
var overlapped *C.struct__OVERLAPPED

loop:
if ok, err := C.WaitCommEvent(*handle, &events, overlapped); ok == C.FALSE {
fmt.Printf("%v, 0x%04x\n", err, events)
return 0, err
}

if ok, err := C.SetCommMask(*handle, EV_RXCHAR); ok == C.FALSE {
f.Close()
return nil, err
}

port := serialPort{f}

return &port, nil
}

func (p *serialPort) Close() os.Error {
return p.f.Close()
}

func (p *serialPort) Write(buf []byte) (int, os.Error) {
return p.f.Write(buf)
}

func (p *serialPort) Read(buf []byte) (int, os.Error) {
type hp *C.HANDLE
var handle *C.HANDLE
fd := p.f.Fd()
handle = hp(unsafe.Pointer(&fd))

var events C.DWORD
var overlapped *C.struct__OVERLAPPED

loop:
if ok, err := C.WaitCommEvent(*handle, &events, overlapped); ok == C.FALSE {
fmt.Printf("%v, 0x%04x\n", err, events)
return 0, err
}
// There is a small race window here between returning from WaitCommEvent and reading from the file.
// If we receive data in this window, we will read that data, but the RXFLAG will still be set
// and next time the WaitCommEvent() will return but we will have already read the data. That's
// why we have the stupid goto loop on EOF.
n, err := p.f.Read(buf)
if err == os.EOF && n == 0 {
n, err := p.f.Read(buf)
if err == os.EOF && n == 0 {
//fmt.Printf("%v, %v, %v, 0x%04x\n", err, n, len(buf), events)
goto loop
}

return n, err
}
goto loop
}

return n, err
}

0 comments on commit 0218f27

Please sign in to comment.