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

Examples needed #9

Open
ambiflextrous opened this issue Jul 28, 2018 · 4 comments
Open

Examples needed #9

ambiflextrous opened this issue Jul 28, 2018 · 4 comments

Comments

@ambiflextrous
Copy link

ambiflextrous commented Jul 28, 2018

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
}
}
@ambiflextrous ambiflextrous changed the title Examples Examples needed Jul 28, 2018
@xlab xlab closed this as completed Jul 29, 2018
@xlab xlab reopened this Jul 29, 2018
@vinhjaxt
Copy link
Contributor

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())
}

@gemaalief
Copy link

does this lib over delivery report?

@zombozo12
Copy link

@vinhjaxt What is the best way to initiate multiple USB modems?

@xlab
Copy link
Owner

xlab commented Jun 14, 2022

@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).

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

5 participants