at
at copied to clipboard
Examples needed
I new to the go language I would like a simple example for sending messages using at package.
import package
import "github.com/xlab/at"
func main(){
dev = &Device{
CommandPort: "/dev/ttyUSB1",
NotifyPort: ?,
}
if err = dev.Open(); err != nil {
return
//how to send message code
//how to check balance code
}
}
From at_test.go: How to send SMS:
package main
import (
"log"
"time"
"github.com/xlab/at"
"github.com/xlab/at/sms"
)
var dev *at.Device
// openDevice opens the hardcoded device paths for reading and writing,
// also inits this device with the default device profile.
func openDevice() (err error) {
dev = &at.Device{
CommandPort: "/dev/ttyUSB2", // or COM23 on windows
NotifyPort: "/dev/ttyUSB2", // or COM23 on windows
}
if err = dev.Open(); err != nil {
return
}
if err = dev.Init(at.DeviceE173()); err != nil {
return
}
return
}
func main() {
err := openDevice()
if err != nil {
log.Panicln(err)
}
defer dev.Close()
msg := sms.Message{
Text: "Your text message",
Type: sms.MessageTypes.Submit,
Encoding: sms.Encodings.UCS2,
Address: sms.PhoneNumber("+84902107791"), // Your phone number
VPFormat: sms.ValidityPeriodFormats.Relative,
VP: sms.ValidityPeriod(24 * time.Hour * 4),
}
n, octets, err := msg.PDU()
if err != nil {
log.Panicln(err)
}
err = dev.Commands.CMGS(n, octets)
if err != nil {
log.Panicln(err)
}
}
How to send USSD command (to check Balance):
package main
import (
"log"
"github.com/xlab/at"
"github.com/xlab/at/pdu"
)
var dev *at.Device
// openDevice opens the hardcoded device paths for reading and writing,
// also inits this device with the default device profile.
func openDevice() (err error) {
dev = &at.Device{
CommandPort: "/dev/ttyUSB2", // or COM23 on windows
NotifyPort: "/dev/ttyUSB2", // or COM23 on windows
}
if err = dev.Open(); err != nil {
return
}
if err = dev.Init(at.DeviceE173()); err != nil {
return
}
return
}
func main() {
err := openDevice()
if err != nil {
log.Panicln(err)
}
defer func() {
dev.Close()
}()
// Replace *101# by your USSD command
err = dev.Commands.CUSD(at.UssdResultReporting.Enable, pdu.Encode7Bit("*101#"), at.Encodings.Gsm7Bit)
if err != nil {
log.Panicln(err)
}
// wait for ussd reply
ussdReplyChan := dev.UssdReply()
ussdReply := <-ussdReplyChan
log.Println("USSD reply:", ussdReply.String())
}
does this lib over delivery report?
@vinhjaxt What is the best way to initiate multiple USB modems?
@vinhjaxt What is the best way to initiate multiple USB modems?
Hey! I will point in the direction but didn't test it myself.
You can create multiple configs like these to point to your different devices:
dev = &at.Device{
CommandPort: "/dev/ttyUSB2",
NotifyPort: "/dev/ttyUSB2",
}
dev2 = &at.Device{
CommandPort: "/dev/ANOTHER_ttyUSB",
NotifyPort: "/dev/ANOTHER_ttyUSB",
}
Then use dev.Open()
and dev.Init()
as usual (and same with dev2
).