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

How to work with UART? #41

Closed
eleimt opened this issue Dec 2, 2024 · 4 comments
Closed

How to work with UART? #41

eleimt opened this issue Dec 2, 2024 · 4 comments

Comments

@eleimt
Copy link

eleimt commented Dec 2, 2024

Describe the bug
No UART

To Reproduce
Steps to reproduce the behavior:

  1. Run program (https://github.com/periph/conn/blob/main/uart/uartreg/example_test.go#L46)
package main

import (
	"fmt"
	"log"
	"periph.io/x/conn/v3/driver/driverreg"
	"periph.io/x/conn/v3/uart"
	"periph.io/x/conn/v3/uart/uartreg"
	"strings"
)

func main() {
	// Make sure periph is initialized.
	// TODO: Use host.Init(). It is not used in this example to prevent circular
	// go package import.
	if _, err := driverreg.Init(); err != nil {
		log.Fatal(err)
	}

	// Enumerate all UART ports available and the corresponding pins.
	fmt.Print("UART ports available:\n")
	for _, ref := range uartreg.All() {
		fmt.Printf("- %s\n", ref.Name)
		if ref.Number != -1 {
			fmt.Printf("  %d\n", ref.Number)
		}
		if len(ref.Aliases) != 0 {
			fmt.Printf("  %s\n", strings.Join(ref.Aliases, " "))
		}

		b, err := ref.Open()
		if err != nil {
			fmt.Printf("  Failed to open: %v", err)
		}
		if p, ok := b.(uart.Pins); ok {
			fmt.Printf("  RX : %s", p.RX())
			fmt.Printf("  TX : %s", p.TX())
			fmt.Printf("  RTS: %s", p.RTS())
			fmt.Printf("  CTS: %s", p.CTS())
		}
		if err := b.Close(); err != nil {
			fmt.Printf("  Failed to close: %v", err)
		}
	}
}
  1. Run it.
  2. See only "UART ports available:"

Expected behavior
UART list

Platform (please complete the following information):

Additional context
Also I use minicom 2.8 (minicom -D /dev/ttyS0), it's work.

@eleimt
Copy link
Author

eleimt commented Dec 3, 2024

@mattetti

@gsexton
Copy link
Contributor

gsexton commented Dec 9, 2024

@eleimt The normal architecture would be for a "periph.io/host" package to be included, and that "host" would as part of it's init locate all of the UARTs and register them with "uartreg".

I did a grep of the periph.io/host package and find no files referencing uartreg()

I remembered wondering about this a while ago, and here's the thread on it:

periph/host#18

@eleimt
Copy link
Author

eleimt commented Dec 11, 2024

@eleimt The normal architecture would be for a "periph.io/host" package to be included, and that "host" would as part of it's init locate all of the UARTs and register them with "uartreg".

I did a grep of the periph.io/host package and find no files referencing uartreg()

I remembered wondering about this a while ago, and here's the thread on it:

periph/host#18

I not understand how this using. Give me example please. Thanks.

@gsexton
Copy link
Contributor

gsexton commented Jan 5, 2025

@eleimt

Here's a basic approach using a different library. The serial library for periph.io/conn is not implemented.

package main

import (
	"errors"
	"fmt"
	"log"
	"time"
	//    "periph.io/x/conn/v3/gpio"
	"periph.io/x/conn/v3/display"
	"periph.io/x/conn/v3/display/displaytest"
	"periph.io/x/devices/v3/serlcd"
	// "periph.io/x/devices/v3/mow"
	"go.bug.st/serial"
)

func main() {
	fmt.Println("serlcd")
	mode := &serial.Mode{
		BaudRate: 9600,
		Parity:   serial.NoParity,
		DataBits: 8,
		StopBits: serial.OneStopBit,
	}
	port, err := serial.Open("/dev/ttyUSB0", mode)
	if err != nil {
		log.Fatal(err)
	}
	time.Sleep(time.Second)
	mow := serlcd.NewSerLCD(port, 4, 20)
	time.Sleep(time.Second)
	defer mow.Halt()
	mow.Clear()
	// mow.Contrast(40)
	time.Sleep(time.Second)
	mow.Display(true)package main

import (
	"errors"
	"fmt"
	"log"
	"time"
	"periph.io/x/conn/v3/display"
	"periph.io/x/conn/v3/display/displaytest"
	"periph.io/x/devices/v3/serlcd"
	"go.bug.st/serial"
)

func main() {
	fmt.Println("serlcd")
	mode := &serial.Mode{
		BaudRate: 9600,
		Parity:   serial.NoParity,
		DataBits: 8,
		StopBits: serial.OneStopBit,
	}
	port, err := serial.Open("/dev/ttyUSB0", mode)
	if err != nil {
		log.Fatal(err)
	}
	time.Sleep(time.Second)
	mow := serlcd.NewSerLCD(port, 4, 20)
	time.Sleep(time.Second)
	defer mow.Halt()
	mow.Clear()
	// mow.Contrast(40)
	time.Sleep(time.Second)
	mow.Display(true)
	time.Sleep(time.Second)
	fmt.Println(mow.String())
	n, err := mow.WriteString("abcdefg")
	time.Sleep(time.Second)
	fmt.Println(mow.String())
	n, err := mow.WriteString("abcdefg")
}

@gsexton gsexton closed this as completed Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants