0

シリアル ポート経由で測定値を送信できるデジタル キャリパーがあります。このデータは次のようにフォーマットさ+123.45\rれており、デバイスからいつでも送信できます。

したがって、私のプログラムは、最初のデータ入力 (常に改行で終わる) を「リッスン」し、この入力の直後に読み値を閉じてから、自分の作業を続行する必要があります。

CoolTermPuttyなどの専用端末は、これらの予測不可能な入力を完全に処理します…しかし、Go でそれを行う方法がわかりません (cgo を使用しても)。

私のシステム

  • OS X 10.9.5
  • Go 1.4.2 ダーウィン/AMD64

デバイス

  • 4800 ボー
  • 7 データ ビット
  • 偶数パリティ
  • 2ストップビット
  • DTR

私の実際のコード

…うまくいきません…</p>

package main

import (
    "bufio"
    "fmt"
    "os"
    "syscall"
    "unsafe"
)

func main() {
    f, err := os.OpenFile("/dev/tty.PL2303-00003014", syscall.O_RDWR+syscall.O_NOCTTY+syscall.O_NDELAY+syscall.O_NONBLOCK, 0666)
    if err != nil {
        panic(err)
    }
    defer f.Close()

    // Init terminal for 4800 7E2.
    t := &syscall.Termios{
        // Enable parity checking, strip to 7 bits, CR to NL, outgoing software flow control.
        Iflag: syscall.INPCK + syscall.ISTRIP + syscall.ICRNL + syscall.IXON,
        // Set 4800 bauds, 7 data bits, even parity, 2 stop bits, DTR.
        Cflag:  syscall.B4800 + syscall.CS7 + syscall.PARENB + syscall.CSTOPB + syscall.TIOCM_DTR + syscall.CREAD + syscall.CLOCAL + syscall.CSIZE,
        Cc:     [20]uint8{syscall.VMIN: 1},
        Ispeed: syscall.B4800,
        Ospeed: syscall.B4800,
    }

    _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, uintptr(f.Fd()), uintptr(syscall.F_SETFL), uintptr(unsafe.Pointer(t)))
    if errno != 0 {
        panic("syscall error: " + errno.Error())
    }

    // Read until the first new line byte (\r is converted to \n with the ICRNL flag).
    fr := bufio.NewReader(f)
    line, err := fr.ReadBytes('\n')
    if err != nil {
        panic(err)
    }
    fmt.Println(string(line))
}
4

0 に答える 0