本文整理汇总了Golang中github.com/tarm/serial.Port类的典型用法代码示例。如果您正苦于以下问题:Golang Port类的具体用法?Golang Port怎么用?Golang Port使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Port类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readSizedSerialPortLoop
func readSizedSerialPortLoop(bufSize int, port *serial.Port, readpipe chan []byte) error {
readBuf := make([]byte, 512)
var sumBuf = []byte{}
var renewBuf = []byte{}
sendBuf := make([]byte, 256)
for {
num, err := port.Read(readBuf)
if err == io.EOF {
continue
}
if err != nil {
return fmt.Errorf("cannnot open serial port: serialPort: %v, Error: %v", port, err)
}
if num > 0 {
log.Debugf("readBuf: %v, len: %v", readBuf, len(readBuf))
for index := range readBuf[:num] {
sumBuf = append(sumBuf, readBuf[index])
}
for len(sumBuf) >= bufSize {
sendBuf = sumBuf[:bufSize]
readpipe <- sendBuf
// Truncate sumBuf by Size
log.Debugf("sumBuf: %v, len: %v", sumBuf, len(sumBuf))
renewBuf = []byte{}
for index := bufSize; index < len(sumBuf); index++ {
renewBuf = append(renewBuf, sumBuf[index])
}
sumBuf = renewBuf
log.Debugf("renewed sumBuf: %v, len: %v / Size: %v", sumBuf, len(sumBuf), bufSize)
}
}
}
}
示例2: passToSerial
func passToSerial(serialPort *serial.Port, message ColorMessage) []byte {
// Write JSON Command
messageBytes, err := json.Marshal(message)
_, err = serialPort.Write(messageBytes)
if err != nil {
log.Println("Error: Write error", err)
return nil
}
// Read JSON Response
var outputBytes []byte
var readCount int
byteBuffer := make([]byte, 8)
for {
n, err := serialPort.Read(byteBuffer)
if err != nil {
log.Println("Error: Read error", err)
break
}
readCount++
outputBytes = append(outputBytes, byteBuffer[:n]...)
if bytes.Contains(byteBuffer[:n], []byte("\n")) {
// Stop at the termination of a JSON statement
break
} else if readCount > 15 {
// Prevent from read lasting forever
break
}
}
return outputBytes
}
示例3: writeSerial
func writeSerial(p serial.Port, s string) (err error) {
_, err = p.Write([]byte(s + "\n"))
if err != nil {
log.Fatal(err)
}
return err
}
示例4: readFreesizedSerialPortLoop
func readFreesizedSerialPortLoop(port *serial.Port, readpipe chan []byte) error {
readBuf := make([]byte, 256)
var sumBuf = []byte{}
readPointer := 0
defer port.Close()
for {
num, err := port.Read(readBuf)
if err == io.EOF {
// No more data comes
if readPointer > 0 {
log.Debugf("read data to send: %v", sumBuf)
readpipe <- sumBuf
readPointer = 0
sumBuf = []byte{}
log.Debugf("sumBuf cleared: %v", sumBuf)
}
continue
}
if err != nil {
return fmt.Errorf("cannnot open serial port: serialPort: %v, Error: %v", port, err)
}
if num > 0 {
readPointer += num
for index := range readBuf[:num] {
sumBuf = append(sumBuf, readBuf[index])
}
log.Debugf("read partial data: %v to sumBuf: %v", readBuf, sumBuf)
continue
}
}
}
示例5: verifyWrite
//------------------------------------------------------------------------------
// Purpose: To make sure that the xmodem calls were read by the device without
// clearing the data
//
// Param port: The serial port that we should utilize
//------------------------------------------------------------------------------
func verifyWrite(port *serial.Port) {
var err error
n, readBuff := 1, make([]byte, 5)
for n > 0 && err != io.EOF {
n, err = port.Read(readBuff)
}
}
示例6: readFromTelescope
func readFromTelescope(s *serial.Port, channel chan []byte) {
for {
buf := make([]byte, 1024)
bytesRead, _ := s.Read(buf)
fmt.Println(buf[:bytesRead])
channel <- buf[:bytesRead]
}
}
示例7: writeToTelescope
func writeToTelescope(s *serial.Port, channel chan []byte) {
for {
data := <-channel
fmt.Println(data)
s.Write(data)
s.Flush()
}
}
示例8: readSerial
func readSerial(p serial.Port) string {
if Verbose {
log.Printf("Reading from serial '%v'\n", p)
}
buf := make([]byte, 1024)
r, _ := p.Read(buf)
return strings.TrimSpace(string(buf[:r]))
}
示例9: writeSerial
func writeSerial(p serial.Port, s string) (err error) {
if Verbose {
log.Printf("Writing '%s' to serial '%v'\n", p, s)
}
_, err = p.Write([]byte(s + "\n"))
if err != nil {
log.Fatal(err)
}
return err
}
示例10: write
func write(s *serial.Port, msg []byte) {
_, err := s.Write(msg)
if err != nil {
log.Fatal(err)
}
_, err = s.Write([]byte{checksum(msg)})
if err != nil {
log.Fatal(err)
}
}
示例11: writeSerial
func writeSerial(s *serial.Port, input <-chan string) {
for {
message := <-input
message += "\n"
log.Println(message)
n, err := s.Write([]byte(message))
if err != nil {
log.Fatal(err)
}
log.Printf(string(n))
}
}
示例12: read
func read(s *serial.Port) ([]byte, error) {
buf := make([]byte, 15)
for numRead := 0; numRead < 15; {
n, err := s.Read(buf[numRead:])
if err != nil {
log.Fatal("COMMS, read: %s", err)
}
numRead = numRead + n
}
if checksum(buf[:14]) != buf[14] {
log.Printf("COMMS, read: Checksum doesn't match. Calculated: %x, received: %x", checksum(buf[:14]), buf[14])
return nil, errors.New("Non-matching checksum")
}
return buf[:14], nil
}
示例13: requestDatagram
func requestDatagram(s *serial.Port) error {
n, err := s.Write([]byte("DUMP\r"))
if err != nil {
return err
}
buf := make([]byte, 38) // Datagram length is 38 bytes incl. \n
n, err = s.Read(buf)
if err != nil {
return err
}
// check for correct size
if n != 38 {
return errors.New("received datagram with invalid size (must: 38, was: " + strconv.Itoa(n) + ")")
}
return processDatagram(buf)
}
示例14: sendCommand
func sendCommand(p *serial.Port, command string, waitForOk bool) string {
log.Println("--- SendCommand: ", command)
var status string = ""
p.Flush()
_, err := p.Write([]byte(command))
if err != nil {
log.Fatal(err)
}
buf := make([]byte, 32)
var loop int = 1
if waitForOk {
loop = 10
}
for i := 0; i < loop; i++ {
// ignoring error as EOF raises error on Linux
n, _ := p.Read(buf)
if n > 0 {
status = string(buf[:n])
log.Printf("SendCommand: rcvd %d bytes: %s\n", n, status)
if strings.HasSuffix(status, "OK\r\n") || strings.HasSuffix(status, "ERROR\r\n") {
break
}
}
}
return status
}
示例15: readSerial
func readSerial(s *serial.Port, output chan<- string) {
buf := make([]byte, 128)
re := regexp.MustCompile(commandRegex)
var message string
for {
n, err := s.Read(buf)
if err != nil {
log.Fatal(err)
}
message += string(buf[:n])
message = strings.Replace(message, "\r", "", -1)
message = strings.Replace(message, "\n", "", -1)
results := re.FindStringSubmatch(message)
if 0 < len(results) {
log.Println(results[0])
output <- results[0]
message = ""
}
}
}