Skip to content
112 changes: 78 additions & 34 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,57 @@ go-serial
This is a package that allows you to read from and write to serial ports in Go.


CPU Support(MIPS only)
----------

This fork works only with MIPS cpu, most openwrt routers equipped with MIPS CPU.


OS support
----------

Currently this package works only on OS X, Linux and Windows. It could probably be ported
to other Unix-like platforms simply by updating a few constants; get in touch if
you are interested in helping and have hardware to test with.

The master works with OpenWrt With the following change
````go
// #include <stdio.h>
// #include <stdlib.h>
// #include <linux/termios.h>
//
// int main(int argc, const char **argv) {
// printf("TCSETS2 = 0x%08X\n", TCSETS2);
// printf("BOTHER = 0x%08X\n", BOTHER);
// printf("NCCS = %d\n", NCCS);
// return 0;
// }
//
const (
kTCSETS2 = 0x402C542B
kBOTHER = 0x1000
kNCCS = 19
)
following works with PC linux
TCSETS2 = 0x402C542B
BOTHER = 0x00001000
NCCS = 19



````

Installation
------------

Simply use `go get`:

go get github.com/jacobsa/go-serial/serial
go get github.com/philipgreat/go-serial/serial

To update later:

go get -u github.com/jacobsa/go-serial/serial
go get -u github.com/philipgreat/go-serial/serial



Use
Expand All @@ -30,38 +63,49 @@ Use
Set up a `serial.OpenOptions` struct, then call `serial.Open`. For example:

````go
import "fmt"
import "log"
import "github.com/jacobsa/go-serial/serial"

...

// Set up options.
options := serial.OpenOptions{
PortName: "/dev/tty.usbserial-A8008HlV",
BaudRate: 19200,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 4,
}

// Open the port.
port, err := serial.Open(options)
if err != nil {
log.Fatalf("serial.Open: %v", err)
}

// Make sure to close it later.
defer port.Close()

// Write 4 bytes to the port.
b := []byte{0x00, 0x01, 0x02, 0x03}
n, err := port.Write(b)
if err != nil {
log.Fatalf("port.Write: %v", err)
}

fmt.Println("Wrote", n, "bytes.")
package main

//env GOOS=linux GOARCH=mips go build -ldflags "-s -w" mem.go
import (
"fmt"
"log"

"github.com/philipgreat/go-serial/serial"
)

func main() {
// Below is an example of using our PrintMemUsage() function
// Print our starting memory usage (should be around 0mb)
options := serial.OpenOptions{
PortName: "/dev/ttyUSB0",
BaudRate: 38400,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 4,
}
port, err := serial.Open(options)
if err != nil {
log.Fatalf("serial.Open: %v", err)
}
defer port.Close()
b := []byte{0x28, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x00, 0x80, 0x05, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1F, 0x00, 0x08, 0x12, 0x21, 0x00, 0x00, 0x64, 0x00, 0x64, 0x00,
0x64, 0x00, 0x64, 0x00, 0x64, 0x00, 0xFC, 0x63}
n, err := port.Write(b)
if err != nil {
log.Fatalf("port.Write: %v", err)
}

fmt.Println("Wrote", n, "bytes.")
buf := make([]byte, 128)
n, err = port.Read(buf)
if err != nil {
log.Fatalf("port.Write: %v", err)
}

}

````

See the documentation for the `OpenOptions` struct in `serial.go` for more
Expand Down
37 changes: 34 additions & 3 deletions serial/open_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,43 @@ import (
// return 0;
// }
//
/*386
amd64
amd64p32
arm
armbe
arm64
arm64be
ppc64
ppc64le
mips
mipsle
mips64
mips64le
mips64p32
mips64p32le
ppc
s390
s390x
sparc
sparc64
and reference
https://github.com/mojo-runtime/lib-linux/blob/6dbfa74d17beda9be9c6e3b595c76f8df3cbb077/c/struct-termios.h
*/


const (
kTCSETS2 = 0x402C542B
kBOTHER = 0x1000
kNCCS = 19
kTCSETS2 = unix.TCSETS2 //0x8030542B
kBOTHER = unix.BOTHER//0x00001000
kNCCS = 23 // 23 is the value fix for MIPS. most of the OpenWrt routers installed with MIPS cpu.
)
/*
TCSETS2 = 0x8030542B

BOTHER = 0x00001000

NCCS = 23
*/
//
// Types from asm-generic/termbits.h
//
Expand Down