Avoid calling GetModemStatusBits on Android. Should fix #1

This commit is contained in:
Torsten Harenberg
2025-02-03 15:37:45 +01:00
parent 255137345f
commit 71d2cfb652
3 changed files with 37 additions and 17 deletions

14
cts.go Normal file
View File

@@ -0,0 +1,14 @@
//go:build !android
package main
import "github.com/albenik/go-serial/v2"
// cts returns the clear-to-send status of a device. On Android it will return true as GetModemStatusBits() fails there.
func cts(p *serial.Port) (bool, error) {
r, err := p.GetModemStatusBits()
if err != nil {
return false, err
}
return r.CTS, nil
}

9
cts_android.go Normal file
View File

@@ -0,0 +1,9 @@
//go:build android
package main
import "github.com/albenik/go-serial/v2"
func cts(_ *serial.Port) (bool, error) {
return true, nil
}

31
ptc.go
View File

@@ -761,12 +761,15 @@ func (p *Modem) _write(cmd string) error {
defer p.mux.device.Unlock() defer p.mux.device.Unlock()
out := cmd + "\r" out := cmd + "\r"
for { for {
status, err := p.device.GetModemStatusBits()
// check if the serial line is clear-to-send
status, err := cts(p.device)
if err != nil { if err != nil {
writeDebug("GetModemStatusBits failed. cmd: "+cmd+" Error: "+err.Error(), 1) writeDebug("GetModemStatusBits failed. cmd: "+cmd+" Error: "+err.Error(), 1)
return err return err
} }
if status.CTS {
if status {
for { for {
sent, err := p.device.Write([]byte(out)) sent, err := p.device.Write([]byte(out))
if err == nil { if err == nil {
@@ -788,13 +791,13 @@ func (p *Modem) _write(cmd string) error {
// Although these functions are small, I prefer to keep their functionality // Although these functions are small, I prefer to keep their functionality
// separate. They follow the steps in the SCS documentation // separate. They follow the steps in the SCS documentation
// helper function: de-hexlify and write to debug channel // printhex: helper function: de-hexlify and write to debug channel
func printhex(s string) { func printhex(s string) {
t, _ := hex.DecodeString(s) t, _ := hex.DecodeString(s)
writeDebug(string(t), 3) writeDebug(string(t), 3)
} }
// helper function: "unstuff" a string // unstuff: helper function: "unstuff" a string
func unstuff(s string) string { func unstuff(s string) string {
//Expect: the string contains aa aa at the beginning, that should NOT be //Expect: the string contains aa aa at the beginning, that should NOT be
//stuffed //stuffed
@@ -806,7 +809,7 @@ func unstuff(s string) string {
return re return re
} }
// helper function: "stuff" a string: replaces every #170 with #170#0 // stuff: helper function: "stuff" a string: replaces every #170 with #170#0
func stuff(s string) string { func stuff(s string) string {
//Expect: the string contains aa aa at the beginning, that should NOT be //Expect: the string contains aa aa at the beginning, that should NOT be
//stuffed //stuffed
@@ -822,14 +825,14 @@ func stuff(s string) string {
return re return re
} }
// helper function: calculates the CCITT-CRC16 checksum // checksum: helper function: calculates the CCITT-CRC16 checksum
func checksum(s string) uint16 { func checksum(s string) uint16 {
tochecksum, _ := hex.DecodeString(s[4:]) tochecksum, _ := hex.DecodeString(s[4:])
chksum := bits.ReverseBytes16(crc16.ChecksumCCITT([]byte(tochecksum))) chksum := bits.ReverseBytes16(crc16.ChecksumCCITT([]byte(tochecksum)))
return chksum return chksum
} }
// helper fuction: check the checksum by comparing // checkcrc: helper fuction: check the checksum by comparing
func checkcrc(s []byte) bool { func checkcrc(s []byte) bool {
tochecksum := s[2 : len(s)-2] tochecksum := s[2 : len(s)-2]
chksum := bits.ReverseBytes16(crc16.ChecksumCCITT(tochecksum)) chksum := bits.ReverseBytes16(crc16.ChecksumCCITT(tochecksum))
@@ -837,7 +840,7 @@ func checkcrc(s []byte) bool {
return (binary.BigEndian.Uint16(pksum) == chksum) return (binary.BigEndian.Uint16(pksum) == chksum)
} }
// super helper fuction: convert an ordinary WA8DED message into a CRC-Hostmode message // docrc: super helper fuction: convert an ordinary WA8DED message into a CRC-Hostmode message
func docrc(msg string) string { func docrc(msg string) string {
// step 1: add a #170170 // step 1: add a #170170
msg = fmt.Sprintf("%02x%02x%s", 170, 170, msg) msg = fmt.Sprintf("%02x%02x%s", 170, 170, msg)
@@ -850,9 +853,7 @@ func docrc(msg string) string {
} }
// Write channel to serial connection (NOT thread safe) // writeChannel: Write channel to serial connection (NOT thread safe)- If used, make sure to mutex!
//
// If used, make shure to lock/unlock p.mux.pactor mutex!
func (p *Modem) writeChannel(msg string, ch int, isCommand bool) error { func (p *Modem) writeChannel(msg string, ch int, isCommand bool) error {
if err := p.checkSerialDevice(); err != nil { if err := p.checkSerialDevice(); err != nil {
writeDebug(err.Error(), 1) writeDebug(err.Error(), 1)
@@ -905,18 +906,14 @@ func (p *Modem) writeChannel(msg string, ch int, isCommand bool) error {
return nil return nil
} }
// Read from serial connection (thread safe) // read: Read from serial connection (thread safe)
//
// No other read/write operation allowed during this time
func (p *Modem) read(chunkSize int) (int, []byte, error) { func (p *Modem) read(chunkSize int) (int, []byte, error) {
p.mux.pactor.Lock() p.mux.pactor.Lock()
defer p.mux.pactor.Unlock() defer p.mux.pactor.Unlock()
return p._read(chunkSize) return p._read(chunkSize)
} }
// Read from serial connection (NOT thread safe) // _read: Read from serial connection (NOT thread safe). To be used from read
//
// If used, make shure to lock/unlock p.mux.pactor mutex!
func (p *Modem) _read(chunkSize int) (int, []byte, error) { func (p *Modem) _read(chunkSize int) (int, []byte, error) {
if err := p.checkSerialDevice(); err != nil { if err := p.checkSerialDevice(); err != nil {
writeDebug(err.Error(), 1) writeDebug(err.Error(), 1)