该代码使用 php-serial 项目进行串口通讯 ,完整代码打包下载:https://github.com/gonzalo123/gam-sms
相关的at指令集:
at+cpin?\r : checks if sim has the pin code. it answers +cpin: ready or +cpin: sim pin if we need to insert the pin number
at+cmgs=”[number]”\r[text]control-z : to send a sms (in php we have control-z with chr(26)). it returns ok or error
at+cmgf=1\r: set the device to operate in sms text mode (0=pdu mode and 1=text mode). returns ok.
at+cmgl=\”all”\r read all the sms stored in the device. we also can use “rec unread” instead of “all”.
at+cmgd=[id]\r: deletes a sms from the device
1.PHP代码
<?php
class Sms_Dummy implements Sms_Interface
{
public function deviceOpen()
{
}
public function deviceClose()
{
}
public function sendMessage($msg)
{
}
public function readPort()
{
return array("OK", array());
}
private $_validOutputs = array();
public function setValidOutputs($validOutputs)
{
$this->_validOutputs = $validOutputs;
}
}2. 测试类
<?php
require_once('Sms.php');
require_once('Sms/Interface.php');
require_once('Sms/Dummy.php');
$pin = 1234;
$serial = new Sms_Dummy;
if (Sms::factory($serial)->insertPin($pin)
->sendSMS(555987654, "test Hi")) {
echo "SMS sent\n";
} else {
echo "SMS not Sent\n";
}3. 完整类
require_once('Sms.php');
require_once('Sms/Interface.php');
require_once('Sms/Serial.php');
$pin = 1234;
try {
$serial = new Sms_Serial;
$serial->deviceSet("/dev/ttyS0");
$serial->confBaudRate(9600);
$serial->confParity('none');
$serial->confCharacterLength(8);
$sms = Sms::factory($serial)->insertPin($pin);
if ($sms->sendSMS(555987654, "test Hi")) {
echo "SMS sent\n";
} else {
echo "Sent Error\n";
}
// Now read inbox
foreach ($sms->readInbox() as $in) {
echo"tlfn: {$in['tlfn']} date: {$in['date']} {$in['hour']}\n{$in['msg']}\n";
// now delete sms
if ($sms->deleteSms($in['id'])) {
echo "SMS Deleted\n";
}
}
} catch (Exception $e) {
switch ($e->getCode()) {
case Sms::EXCEPTION_NO_PIN:
echo "PIN Not set\n";
break;
case Sms::EXCEPTION_PIN_ERROR:
echo "PIN Incorrect\n";
break;
case Sms::EXCEPTION_SERVICE_NOT_IMPLEMENTED:
echo "Service Not implemented\n";
break;
default:
echo $e->getMessage();
}
}4. PHP代码
<?php
require_once('Sms.php');
require_once('Sms/Interface.php');
require_once('Sms/Http.php');
$serialEternetConverterIP = '192.168.1.10';
$serialEternetConverterPort = 1113;
$pin = 1234;
try {
$sms = Sms::factory(new Sms_Http($serialEternetConverterIP, $serialEternetConverterPort));
$sms->insertPin($pin);
if ($sms->sendSMS(555987654, "test Hi")) {
echo "SMS Sent\n";
} else {
echo "Sent Error\n";
}
// Now read inbox
foreach ($sms->readInbox() as $in) {
echo"tlfn: {$in['tlfn']} date: {$in['date']} {$in['hour']}\n{$in['msg']}\n";
// now delete sms
if ($sms->deleteSms($in['id'])) {
echo "SMS Deleted\n";
}
}
} catch (Exception $e) {
switch ($e->getCode()) {
case Sms::EXCEPTION_NO_PIN:
echo "PIN Not set\n";
break;
case Sms::EXCEPTION_PIN_ERROR:
echo "PIN Incorrect\n";
break;
case Sms::EXCEPTION_SERVICE_NOT_IMPLEMENTED:
echo "Service Not implemented\n";
break;
default:
echo $e->getMessage();
}
}以上就是PHP 通过 GSM Modem 收发短信的内容,更多相关内容请关注PHP中文网(www.php.cn)!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号