go-hackrf
go-hackrf copied to clipboard
Guide to using StartTX
Amazing wrapper! Love that you've done this.
I am confused on how to "feed" the StartTX with a constant stream of data. Is there an example anywhere at all?
Thanks!
How it works:
StartTX takes a callback that continuously fills its buffer with IQ (in-phase/quadrature) samples.
The callback is called repeatedly as HackRF needs more data for transmission.
You can replace the random pattern with any waveform, such as a sine wave, a constant signal, etc
This is the minimal example for feeding HackRF StartTX with a continuous stream. Replace the buffer fill code with your own signal as needed!
package main
import (
"log"
"math/rand"
"time"
"github.com/samuel/go-hackrf/hackrf"
)
func main() {
// Initialize HackRF driver and open device
if err := hackrf.Init(); err != nil {
log.Fatalf("hackrf.Init() failed: %v", err)
}
defer hackrf.Exit()
dev, err := hackrf.Open()
if err != nil {
log.Fatalf("hackrf.Open() failed: %v", err)
}
defer dev.Close()
// Set desired transmit parameters (example: 440 MHz, 10 MHz sample rate, gain 40)
if err := dev.SetFreq(440_000_000); err != nil {
log.Fatalf("SetFreq failed: %v", err)
}
if err := dev.SetSampleRate(10_000_000); err != nil {
log.Fatalf("SetSampleRate failed: %v", err)
}
if err := dev.SetTXVGAGain(40); err != nil {
log.Fatalf("SetTXVGAGain failed: %v", err)
}
if err := dev.SetAmpEnable(true); err != nil {
log.Fatalf("SetAmpEnable failed: %v", err)
}
log.Println("Starting HackRF transmission of a constant stream...")
// StartTX expects a callback function that fills buf with IQ samples continuously.
// buf is filled with int8 values, interleaved as I,Q,I,Q,...
// This example fills buf with random IQ values as a dummy constant stream.
err = dev.StartTX(func(buf []byte) error {
// Fill the buffer with a test pattern, e.g., a constant signal, noise, or waveform
for i := 0; i < len(buf); i += 2 {
buf[i] = int8(rand.Intn(256) - 128) // I sample: random noise
buf[i+1] = int8(rand.Intn(256) - 128) // Q sample: random noise
}
// If you want a constant tone, you could fill with a fixed value instead.
// Example for constant carrier:
// for i := 0; i < len(buf); i += 2 {
// buf[i] = 64 // I sample: constant amplitude
// buf[i+1] = 0 // Q sample: zero
// }
return nil
})
if err != nil {
log.Fatalf("StartTX failed: %v", err)
}
log.Println("HackRF transmission is live. Press Ctrl+C to stop.")
for {
time.Sleep(time.Second)
}
}