hbjorgo / ATLib

ATLib is a C# library that makes it easy to communicate with modems.
MIT License
49 stars 20 forks source link
at atcommand atcommands c-sharp gsm gsm-modem hayes modem sms

ATLib

CI Nuget Nuget

ATLib is a C# AT command library that abstracts away the commands and makes it easy to communicate with modems.

Hayes command set (commonly known as AT commands) is a command set frequently used in modems. Read more about it at Wikipedia.

Feedback is very much welcome and please request features πŸ™‚

HeboTech.GsmApi is a REST API wrapping this library.

Supported commands:

Events

Supported modems:

Other

Usage

Install as NuGet package

dotnet add package HeboTech.ATLib

Using a serial port to communicate with a modem is easy:

// Set up serial port
using SerialPort serialPort = new SerialPort(args[0], 9600, Parity.None, 8, StopBits.One)
{
    Handshake = Handshake.RequestToSend
};
serialPort.Open();

// Create AT channel
using AtChannel atChannel = AtChannel.Create(serialPort.BaseStream);

// Create the modem
using IModem modem = new Fona3G(atChannel);

// Open AT channel
atChannel.Open();

// Configure modem with required settings before PIN
var requiredSettingsBeforePin = await modem.SetRequiredSettingsBeforePinAsync();

// Get SIM status
var simStatus = await modem.GetSimStatusAsync();
Console.WriteLine($"SIM Status: {simStatus}");

if (simStatus == SimStatus.SIM_PIN)
{
    var simPinStatus = await modem.EnterSimPinAsync(new PersonalIdentificationNumber("<PIN>"));
    Console.WriteLine($"SIM PIN Status: {simPinStatus}");
}

// Configure modem with required settings after PIN
var requiredSettingsAfterPin = await modem.SetRequiredSettingsAfterPinAsync();

// Send SMS to the specified number
var smsReference = await modem.SendSmsAsync(new PhoneNumber("123456789"), "Hello ATLib!");
Console.WriteLine($"SMS Reference: {smsReference}");

Because it relies on a stream, you can even control a modem over a network! Either use a network attached modem, or forward a modem serial port to a network port.

For more examples, check out the TestConsole project in the code.