Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable read from port in Mac M1 #43

Open
sahilpaudel opened this issue Jan 28, 2024 · 12 comments
Open

Unable read from port in Mac M1 #43

sahilpaudel opened this issue Jan 28, 2024 · 12 comments

Comments

@sahilpaudel
Copy link

Hey there,

go version go1.21.6 darwin/arm64

`
import (
"fmt"
"github.com/albenik/go-serial/v2"
"log"
)

func main() {
ports, err := serial.GetPortsList()
if err != nil {
log.Fatal(err)
}
if len(ports) == 0 {
log.Fatal("No serial ports found!")
}

// Print the list of detected ports
for _, port := range ports {
	fmt.Printf("Found port: %v\n", port)
}

port, err := serial.Open(ports[0],
	serial.WithBaudrate(9600),
	serial.WithDataBits(8),
	serial.WithParity(serial.NoParity),
	serial.WithStopBits(serial.OneStopBit),
	serial.WithReadTimeout(2000),
	serial.WithWriteTimeout(2000),
	serial.WithHUPCL(false),
)
if err != nil {
	log.Fatal(err)
}

n, err := port.Write([]byte("sdadas dasdasd dasdasd dasdasda dasd asdas dasd asd as"))
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Sent %v bytes\n", n)

buff := make([]byte, 1024)
for {
	n, err := port.Read(buff)
	fmt.Printf("Length %v", err)
	fmt.Printf("Length %d", n)
	if n == 0 {
		fmt.Println("\nEOF")
		break
	}
	fmt.Printf("%v", string(buff[:n]))
}

}
`

The output of above code is

Found port: /dev/cu.Bluetooth-Incoming-Port Found port: /dev/cu.wlan-debug Found port: /dev/tty.Bluetooth-Incoming-Port Found port: /dev/tty.wlan-debug Sent 54 bytes Length <nil>Length 0 EOF

No matter what I send it gives length of read bytes as 0.
Can you please suggest what am I missing here.

@albenik
Copy link
Owner

albenik commented Jan 28, 2024

@sahilpaudel What data (what bytes exactly) you expected to read from the port by this code?

@sahilpaudel-pe
Copy link

for now I am sending this n, err := port.Write([]byte("sdadas dasdasd dasdasd dasdasda dasd asdas dasd asd as"))

@Aukstkalnis
Copy link

Why are you writing to port /dev/cu.Bluetooth-Incoming-Port ?

@sahilpaudel-pe
Copy link

When I list the port this is the port I am seeing.

@Aukstkalnis
Copy link

Well, if we believe the text in the name of the port, it says it is Bluetooth incoming port, So I don't think you could write to this port. Do you have any specific reason to write to this port?

@sahilpaudel-pe
Copy link

No I was trying out the library and how it works. Do I have to install some drivers to get a proper port because all I could see is
/dev/cu.Bluetooth-Incoming-Port
/dev/tty.Bluetooth-Incoming-Port
/dev/cu.wlan-debug
/dev/tty.wlan-debug

when I run Get All Port List

@Aukstkalnis
Copy link

OK. First of all you you should not use those ports. They are system related. If you want to work with serial communication you have to have a device that works with serial port. For example, you have GSM modem that works via UART and you control it via AT commands. For that you need FTDI/CP2102/(any other USB to serial converter) to connect to that device. Then you will use that port for controlling GSM modem. Or you have Micro controller (MCU) that has USB port and when you connect it to PC it creates virtual serial port and then you can open that port and send commands using this library. That is how you should use serial port.

@sahilpaudel-pe
Copy link

Thank you so much for this great insight is there a way I can mimic this using any library or commands in mac?

@Aukstkalnis
Copy link

I don't think you can mimic serial port opening/writing/reading/closing. If you want to test the library, then you have to have some real serial port device. What do you want to do with this library that you need to mimic serial port?

@sahilpaudel-pe
Copy link

I have a device that sends data stream and I need to write an application to consume that and process that.
I don't have the machine right now so was exploring this.

@sahilpaudel-pe
Copy link

And also I am getting invalid serial port: operating system error: inappropriate ioctl for device

`func main() {

portName := "/dev/ttys008"

port, err := serial.Open(portName)
if err != nil {
	log.Fatal(err)
}

if err := port.Reconfigure(
	serial.WithBaudrate(9600),
	serial.WithDataBits(8),
	serial.WithParity(serial.NoParity),
	serial.WithStopBits(serial.OneStopBit),
	serial.WithReadTimeout(1000),
	serial.WithWriteTimeout(1000),
); err != nil {
	log.Fatal(err)
}

if err != nil {
	log.Fatal("Error opening serial port: ", err)
}

defer port.Close()

}`

I tried the https://github.com/tarm/serial it was working with that.

@Aukstkalnis
Copy link

Does this serial port exist? Can you list serial ports with this library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants