Skip to content

Commit

Permalink
improve compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dcentelles committed Oct 8, 2024
1 parent 51e30aa commit c0093d2
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/cheggaaa/pb"
tty "github.com/jacobsa/go-serial/serial"
"golang.org/x/sys/unix"

"github.com/usedbytes/serial-flash/program"
"github.com/usedbytes/serial-flash/protocol"
Expand All @@ -27,6 +28,27 @@ func usage() error {
return fmt.Errorf("Usage: %s PORT FILE [BASE]", os.Args[0])
}

func setSerialAttributes(fd int) error {
// Obtener los atributos actuales del puerto serial
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil {
return fmt.Errorf("ioctl TCGETS failed: %v", err)
}

// Configurar baud rate, data bits, stop bits, etc.
termios.Cflag = unix.B921600 | unix.CS8 | unix.CLOCAL | unix.CREAD
termios.Iflag = unix.IGNPAR
termios.Oflag = 0
termios.Lflag = 0

// Aplicar los nuevos atributos
if err := unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil {
return fmt.Errorf("ioctl TCSETS failed: %v", err)
}

return nil
}

func run() error {
if len(os.Args) < 3 {
return usage()
Expand Down Expand Up @@ -81,24 +103,19 @@ func run() error {

rw = conn
} else {
options := tty.OpenOptions{
PortName: port,
BaudRate: 921600,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 1,
InterCharacterTimeout: 100,
fd, err := syscall.Open(port, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return fmt.Errorf("syscall.Open %s: %v", port, err)
}
defer syscall.Close(fd)

ser, err := tty.Open(options)
if err != nil {
return fmt.Errorf("tty.Open %s: %v", port, err)
if err := setSerialAttributes(fd); err != nil {
return fmt.Errorf("setSerialAttributes %s: %v", port, err)
}
defer ser.Close()

fmt.Println("Opened", port)

rw = ser
rw = os.NewFile(uintptr(fd), port)
}

prog := make(chan program.ProgressReport)
Expand Down

0 comments on commit c0093d2

Please sign in to comment.