PHP dio_write() function close after sending - php

I am trying to send data to my serial port using the php_dio.dll extention below code manage to send "HELLO WORLD" to serial com port 11 (com11) and returned the bytes, now i cannot close the serial port after it received my data.
On my com11 i have an arduino mega attached.
PHP code
$portName = '\\\\.\COM11';
$baudRate = 9600;
$bits = 8;
$spotBit = 1;
header( 'Content-type: text/plain; charset=utf-8' );
?>
Serial Port Test
================
<?php
function echoFlush($string)
{
echo $string . "\n";
flush();
ob_flush();
}
if(!extension_loaded('dio'))
{
echoFlush( "PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php" );
exit;
}
try
{
//the serial port resource
$bbSerialPort;
echoFlush( "Connecting to serial port: {$portName}" );
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
$bbSerialPort = dio_open($portName, O_RDWR );
//we're on windows configure com port from command line
exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on");
}
else //'nix
{
$bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK );
dio_fcntl($bbSerialPort, F_SETFL, O_SYNC);
//we're on 'nix configure com from php direct io function
dio_tcsetattr($bbSerialPort, array(
'baud' => $baudRate,
'bits' => $bits,
'stop' => $spotBit,
'parity' => 0
));
}
if(!$bbSerialPort)
{
echoFlush( "Could not open Serial port {$portName} ");
exit;
}
// send data
$dataToSend = "HELLO WORLD!";
echoFlush( "Writing to serial port data: \"{$dataToSend}\"" );
$bytesSent = dio_write($bbSerialPort, $dataToSend );
echoFlush( "Sent: {$bytesSent} bytes" );
//date_default_timezone_set ("Europe/London");
$runForSeconds = new DateInterval("PT10S"); //10 seconds
$endTime = (new DateTime())->add($runForSeconds);
echoFlush( "Waiting for {$runForSeconds->format('%S')} seconds to recieve data on serial port" );
while (new DateTime() < $endTime) {
$data = dio_read($bbSerialPort, 256); //this is a blocking call
if ($data) {
echoFlush( "Data Recieved: ". $data );
}
}
echoFlush( "Closing Port" );
dio_close($bbSerialPort);
}
catch (Exception $e)
{
echoFlush( $e->getMessage() );
exit(1);
}
Arduino Code
char incomingByte;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
}

Related

Serial Port to PHP

I'm new to working with the serial port module to read data from devices and I'm trying to read data from an electronic balance through Nport W2150a device using php with the php_dio module, the code is outputing but the result is printing with unknow charset. What should I do to change that?
Output:
Code:
<?php
$portName = 'com1:1';
$baudRate = 9600;
$bits = 8;
$spotBit = 1;
header( 'Content-type: text/plain; charset=utf-8' );
?>
<?php
function echoFlush($string)
{
echo $string . "\n";
flush();
ob_flush();
}
if(!extension_loaded('dio'))
{
echoFlush( "PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php" );
exit;
}
try
{
$bbSerialPort;
echoFlush( "Connecting to serial port: {$portName}" );
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
$bbSerialPort = dio_open($portName, O_RDWR );
//we're on windows configure com port from command line
exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on");
}
else //'nix
{
$bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK );
dio_fcntl($bbSerialPort, F_SETFL, O_SYNC);
//we're on 'nix configure com from php direct io function
dio_tcsetattr($bbSerialPort, array(
'baud' => $baudRate,
'bits' => $bits,
'stop' => $spotBit,
'parity' => 0
));
}
if(!$bbSerialPort)
{
echoFlush( "Could not open Serial port {$portName} ");
exit;
}
// send data
$dataToSend = "HELLO WORLD!";
echoFlush( "Writing to serial port data: \"{$dataToSend}\"" );
$bytesSent = dio_write($bbSerialPort, $dataToSend );
echoFlush( "Sent: {$bytesSent} bytes" );
$runForSeconds = new DateInterval("PT10S");
$endTime = (new DateTime())->add($runForSeconds);
echoFlush( "Waiting for {$runForSeconds->format('%S')} seconds to recieve data on serial port" );
while (new DateTime() < $endTime) {
$data = dio_read($bbSerialPort, 256);
if ($data) {
echoFlush( "Data Recieved: ". $data );
}
}
echoFlush( "Closing Port" );
dio_close($bbSerialPort);
}
catch (Exception $e)
{
echoFlush( $e->getMessage() );
exit(1);
}
?>

can not read serial port using php or nodejs

I have tried to read serial port data sending with arduino by php or nodejs and I couldn't. I think my codes are correct. I think the problem is permission access reading serial port limited by windows or browser but i'm not sure. although the software of arduino can read data.
when I run my codes, arduio sends data over and over but nothing to show data.
can you help me?!
Here my php codes:
<?php
//-- settings --//
//brainboxes serial ports
$portName = "COM5";
$baudRate = 9600;
$bits = 8;
$spotBit = 1;
header( 'Content-type: text/plain; charset=utf-8' );
?>
Serial Port Test
================
<?php
function echoFlush($string)
{
echo $string . "\n";
flush();
ob_flush();
}
if(!extension_loaded('dio'))
{
echoFlush( "PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php" );
exit;
}
try
{
//the serial port resource
$bbSerialPort;
echoFlush( "Connecting to serial port: {$portName}" );
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
$bbSerialPort = dio_open($portName, O_RDWR );
//we're on windows configure com port from command line
exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on");
}
else //'nix
{
$bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK );
dio_fcntl($bbSerialPort, F_SETFL, O_SYNC);
//we're on 'nix configure com from php direct io function
dio_tcsetattr($bbSerialPort, array(
'baud' => $baudRate,
'bits' => $bits,
'stop' => $spotBit,
'parity' => 0
));
}
if(!$bbSerialPort)
{
echoFlush( "Could not open Serial port {$portName} ");
exit;
}
// send data
$dataToSend = "HELLO WORLD!";
echoFlush( "Writing to serial port data: \"{$dataToSend}\"" );
$bytesSent = dio_write($bbSerialPort, $dataToSend );
echoFlush( "Sent: {$bytesSent} bytes" );
//date_default_timezone_set ("Europe/London");
$runForSeconds = new DateInterval("PT10S"); //10 seconds
$endTime = (new DateTime())->add($runForSeconds);
echoFlush( "Waiting for {$runForSeconds->format('%S')} seconds to recieve data on serial port" );
while (new DateTime() < $endTime) {
$data = dio_read($bbSerialPort, 256); //this is a blocking call
if ($data) {
echoFlush( "Data Recieved: ". $data );
}
}
echoFlush( "Closing Port" );
dio_close($bbSerialPort);
}
catch (Exception $e)
{
echoFlush( $e->getMessage() );
exit(1);
}
?>
Error in php:
"Warning: dio_open(): cannot open file COM5 with flags 2 and permissions 0: Permission denied in C:\xampp\htdocs\serialreading\SerialPortTest.php on line 42
Could not open Serial port COM5"
Here my nodejs codes:
var express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server),
port = 8888;
//Server start
server.listen(port, () => console.log('on port' + port))
//user server
app.use(express.static(__dirname + '/public'));
io.on('connection', onConnection);
var connectedSocket = null;
function onConnection(socket){
connectedSocket = socket;
}
//Arduino to CMD
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const usbport = new SerialPort('COM5');
const parser = usbport.pipe(new Readline());
parser.on('data', function (data) {
io.emit('data', { data: data });
});
Error in nodejs:
"Could Not get"

Send sms through d-link GSM modem in PHP

I am trying to send sms via D-Link GSM modem. I am new on this. My code shows Unable to setup COM port I did not add any additional file, library or any modification in php.ini file in xampp. just installed software provided by modem. My operating system is windows 10. I am mentioning my code, though I got it from internet.
$mobile_number=$_POST['mobile_number'];
$messages=$_POST['messages'];
$gsm_send_sms = new gsm_send_sms();
$gsm_send_sms->debug = false;
$gsm_send_sms->port = 'COM3';
$gsm_send_sms->baud = 115200;
$gsm_send_sms->init();
$status = $gsm_send_sms->send($mobile_number,$messages);
if ($status) {
echo "<body bgcolor='lightgreen'>";
echo "<br><br><br>";
echo "<h3 align='center'>Message Successfully Send... </h3>\n";
echo "</body>";
} else {
echo "<body bgcolor='lightgreen'>";
echo "<br><br><br>";
echo "<h3 align='center'>Message not sent Successfully... </h3>\n";
echo "</body>";
}
$gsm_send_sms->close();
//Send SMS via serial SMS modem
class gsm_send_sms {
public $port = 'COM3';
public $baud = 115200;
public $debug = false;
private $fp;
private $buffer;
//Setup COM port
public function init() {
$this->debugmsg("Setting up port: \"{$this->port} # \"{$this->baud}\" baud");
exec("MODE {$this->port}: BAUD={$this->baud} PARITY=N DATA=8 STOP=1", $output, $retval);
if ($retval != 0) {
throw new Exception('Unable to setup COM port, check it is correct');
}
$this->debugmsg(implode("\n", $output));
$this->debugmsg("Opening port");
//Open COM port
$this->fp = fopen($this->port . ':', 'r+');
//Check port opened
if (!$this->fp) {
throw new Exception("Unable to open port \"{$this->port}\"");
}
$this->debugmsg("Port opened");
$this->debugmsg("Checking for response from modem");
//Check modem connected
fputs($this->fp, "AT\r");
//Wait for ok
$status = $this->wait_reply("OK\r\n", 5);
if (!$status) {
throw new Exception('Did not receive responce from modem');
}
$this->debugmsg('Modem connected');
//Set modem to SMS text mode
$this->debugmsg('Setting text mode');
fputs($this->fp, "AT+CMGF=1\r");
$status = $this->wait_reply("OK\r\n", 5);
if (!$status) {
throw new Exception('Unable to set text mode');
}
$this->debugmsg('Text mode set');
}
//Wait for reply from modem
private function wait_reply($expected_result, $timeout) {
$this->debugmsg("Waiting {$timeout} seconds for expected result");
//Clear buffer
$this->buffer = '';
//Set timeout
$timeoutat = time() + $timeout;
//Loop until timeout reached (or expected result found)
do {
$this->debugmsg('Now: ' . time() . ", Timeout at: {$timeoutat}");
$buffer = fread($this->fp, 1024);
$this->buffer .= $buffer;
usleep(200000);//0.2 sec
$this->debugmsg("Received: {$buffer}");
//Check if received expected responce
if (preg_match('/'.preg_quote($expected_result, '/').'$/', $this->buffer)) {
$this->debugmsg('Found match');
return true;
//break;
} else if (preg_match('/\+CMS ERROR\:\ \d{1,3}\r\n$/', $this->buffer)) {
return false;
}
} while ($timeoutat > time());
$this->debugmsg('Timed out');
return false;
}
//Print debug messages
private function debugmsg($message) {
if ($this->debug == true) {
$message = preg_replace("%[^\040-\176\n\t]%", '', $message);
echo $message . "\n";
}
}
//Close port
public function close() {
$this->debugmsg('Closing port');
fclose($this->fp);
}
//Send message
public function send($tel, $message) {
//Filter tel
$tel = preg_replace("%[^0-9\+]%", '', $tel);
//Filter message text
$message = preg_replace("%[^\040-\176\r\n\t]%", '', $message);
$this->debugmsg("Sending message \"{$message}\" to \"{$tel}\"");
//Start sending of message
fputs($this->fp, "AT+CMGS=\"{$tel}\"\r");
//Wait for confirmation
$status = $this->wait_reply("\r\n> ", 5);
if (!$status) {
//throw new Exception('Did not receive confirmation from modem');
$this->debugmsg('Did not receive confirmation from modem');
return false;
}
//Send message text
fputs($this->fp, $message);
//Send message finished indicator
fputs($this->fp, chr(26));
//Wait for confirmation
$status = $this->wait_reply("OK\r\n", 180);
if (!$status) {
//throw new Exception('Did not receive confirmation of message sent');
$this->debugmsg('Did not receive confirmation of message sent');
return false;
}
$this->debugmsg("Message Send Successfully...");
return true;
}
}
It's Look like You are using a D-Link USB GSM Modem.
in this case Modem COM port doesn't show in Device Manager Automatically.
First you must start D-Link Connection Manager SoftWare and then you can find your modem under MODEM category in Device Manager.
R-Click on it and Open Properties window.
You can find COM port on it!

Unable to setup COM port, check it is correct

I am sending sms using Nokia usb Device and PHP script but getting error
many answer are there on stack but no answer has solution of this problem
Fatal error: Uncaught Exception: Unable to setup COM port, check it is correct in C:\xampp\htdocs\sms-prac\send-sms.php:103
Stack trace:
#0 C:\xampp\htdocs\sms-prac\send-sms.php(36): gsm_send_sms->init()
#1 {main}
thrown in C:\xampp\htdocs\sms-prac\send-sms.php on line 103
My gsm_send_sms Class Code
//Send SMS via serial SMS modem
class gsm_send_sms {
public $port = 'COM1';
public $baud = 115200;
public $debug = false;
private $fp;
private $buffer;
//Setup COM port
public function init() {
$this->debugmsg("Setting up port: \"{$this->port} # \"{$this->baud}\" baud");
exec("MODE {$this->port}: BAUD={$this->baud} PARITY=N DATA=8 STOP=1", $output, $retval);
if ($retval != 0) {
throw new Exception('Unable to setup COM port, check it is correct');
}
$this->debugmsg(implode("\n", $output));
$this->debugmsg("Opening port");
//Open COM port
$this->fp = fopen($this->port . ':', 'r+');
//Check port opened
if (!$this->fp) {
throw new Exception("Unable to open port \"{$this->port}\"");
}
$this->debugmsg("Port opened");
$this->debugmsg("Checking for responce from modem");
//Check modem connected
fputs($this->fp, "AT\r");
//Wait for ok
$status = $this->wait_reply("OK\r\n", 5);
if (!$status) {
throw new Exception('Did not receive responce from modem');
}
$this->debugmsg('Modem connected');
//Set modem to SMS text mode
$this->debugmsg('Setting text mode');
fputs($this->fp, "AT+CMGF=1\r");
$status = $this->wait_reply("OK\r\n", 5);
if (!$status) {
throw new Exception('Unable to set text mode');
}
$this->debugmsg('Text mode set');
}
//Wait for reply from modem
private function wait_reply($expected_result, $timeout) {
$this->debugmsg("Waiting {$timeout} seconds for expected result");
//Clear buffer
$this->buffer = '';
//Set timeout
$timeoutat = time() + $timeout;
//Loop until timeout reached (or expected result found)
do {
$this->debugmsg('Now: ' . time() . ", Timeout at: {$timeoutat}");
$buffer = fread($this->fp, 1024);
$this->buffer .= $buffer;
usleep(200000);//0.2 sec
$this->debugmsg("Received: {$buffer}");
//Check if received expected responce
if (preg_match('/'.preg_quote($expected_result, '/').'$/', $this->buffer)) {
$this->debugmsg('Found match');
return true;
//break;
} else if (preg_match('/\+CMS ERROR\:\ \d{1,3}\r\n$/', $this->buffer)) {
return false;
}
} while ($timeoutat > time());
$this->debugmsg('Timed out');
return false;
}
//Print debug messages
private function debugmsg($message) {
if ($this->debug == true) {
$message = preg_replace("%[^\040-\176\n\t]%", '', $message);
echo $message . "\n";
}
}
//Close port
public function close() {
$this->debugmsg('Closing port');
fclose($this->fp);
}
//Send message
public function send($tel, $message) {
//Filter tel
$tel = preg_replace("%[^0-9\+]%", '', $tel);
//Filter message text
$message = preg_replace("%[^\040-\176\r\n\t]%", '', $message);
$this->debugmsg("Sending message \"{$message}\" to \"{$tel}\"");
//Start sending of message
fputs($this->fp, "AT+CMGS=\"{$tel}\"\r");
//Wait for confirmation
$status = $this->wait_reply("\r\n> ", 5);
if (!$status) {
//throw new Exception('Did not receive confirmation from modem');
$this->debugmsg('Did not receive confirmation from modem');
return false;
}
//Send message text
fputs($this->fp, $message);
//Send message finished indicator
fputs($this->fp, chr(26));
//Wait for confirmation
$status = $this->wait_reply("OK\r\n", 180);
if (!$status) {
//throw new Exception('Did not receive confirmation of messgage sent');
$this->debugmsg('Did not receive confirmation of messgage sent');
return false;
}
$this->debugmsg("Message sent");
return true;
}
}
My PHP Code which i am using to send SMS
<?php
//SMS via GSM Modem - A PHP class to send SMS messages via a GSM modem attached to the computers serial port.
//Windows only (tested on XP with PHP 5.2.6)
//Tested with the EZ863 (Telit GE863) GSM modem
//Requires that PHP has permission to access "COM" system device, and system "mode" command
ini_set('max_execution_time', 300);
error_reporting(E_ALL);
//Example
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "MembersManagmentSystem";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Member.memberId, Member.memberContactMobileNumber, SMSRecipient.smsRecipientReceiverId, SMSRecipient.smsInfoId, SMSInfo.smsInfoText, SMSInfo.`smsInfoSaveDateTime` FROM Member, SMSRecipient,SMSInfo WHERE SMSRecipient.`smsRecipientReceiverId` = Member.`memberId` AND SMSRecipient.`smsInfoId`= SMSInfo.`smsInfoId` AND SMSRecipient.smsRecipientSentDateTime IS NULL AND Member.`memberContactMobileNumber` IS NOT NULL AND SMSInfo.`smsInfoSaveDateTime` > DATE_SUB(NOW(), INTERVAL 1 DAY)";
$result = $conn->query($sql);
echo "<pre>";
$gsm_send_sms = new gsm_send_sms();
$gsm_send_sms->debug = false;
$gsm_send_sms->port = 'COM3';
$gsm_send_sms->baud = 115200;
$gsm_send_sms->init();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
print_r($row);
$status = $gsm_send_sms->send($row['memberContactMobileNumber'], $row['smsInfoText']);
if ($status) {
$sql = "UPDATE SMSRecipient SET smsRecipientSentDateTime = now() WHERE smsInfoId = ".$row['smsInfoId']." and smsRecipientReceiverId = ".$row['memberId'];
if ($conn->query($sql) === TRUE) {
echo "Message sent\n";
}
} else {
echo "Message not sent\n";
}
}
//exit();
} else {
echo "0 results";
}
$gsm_send_sms->close();
$conn->close();
?>
on change $gsm_send_sms->debug = false; to true, get
Array
(
[0] =>
[1] => Status for device COM3:
[2] => -----------------------
[3] => Baud: 115200
[4] => Parity: None
[5] => Data Bits: 8
[6] => Stop Bits: 1
[7] => Timeout: OFF
[8] => XON/XOFF: OFF
[9] => CTS handshaking: OFF
[10] => DSR handshaking: OFF
[11] => DSR sensitivity: OFF
[12] => DTR circuit: ON
[13] => RTS circuit: ON
[14] =>
)
After Debug above Code I found that when i first time connect my device and run it php executing this line as like as infinity loop and before this line all debugmsg and other code running fine please help me
$buffer = fread($this->fp, 8192);

PHP Serial Port Access

I am trying to implement this code below, I have a wavecom gsm modem connected to COM6 http://imgur.com/QwkwfMc it works on 3rd party sms gateway software but it is very expensive.
I am getting these errors
Fatal error: Uncaught exception 'Exception' with message 'Unable to setup COM port, check it is correct' in C:\wamp\www\sms\index.php on line 53
( ! ) Exception: Unable to setup COM port, check it is correct in C:\wamp\www\sms\index.php on line 53
What do i need to do to make this work?
<?php
//SMS via GSM Modem - A PHP class to send SMS messages via a GSM modem attached to the computers serial port.
//Windows only (tested on XP with PHP 5.2.6)
//Tested with the EZ863 (Telit GE863) GSM modem
//Requires that PHP has permission to access "COM" system device, and system "mode" command
error_reporting(E_ALL);
//Example
$gsm_send_sms = new gsm_send_sms();
$gsm_send_sms->debug = false;
$gsm_send_sms->port = 'COM6';
$gsm_send_sms->baud = 9600;
$gsm_send_sms->init();
$status = $gsm_send_sms->send('+642102844012', 'testing 123');
if ($status) {
echo "Message sent\n";
} else {
echo "Message not sent\n";
}
$gsm_send_sms->close();
//Send SMS via serial SMS modem
class gsm_send_sms {
public $port = 'COM6';
public $baud = 9600;
public $debug = false;
private $fp;
private $buffer;
//Setup COM port
public function init() {
$this->debugmsg("Setting up port: \"{$this->port} # \"{$this->baud}\" baud");
exec("MODE {$this->port}: BAUD={$this->baud} PARITY=N DATA=8 STOP=1", $output, $retval);
if ($retval != 0) {
throw new Exception('Unable to setup COM port, check it is correct');
}
$this->debugmsg(implode("\n", $output));
$this->debugmsg("Opening port");
//Open COM port
$this->fp = fopen($this->port . ':', 'r+');
//Check port opened
if (!$this->fp) {
throw new Exception("Unable to open port \"{$this->port}\"");
}
$this->debugmsg("Port opened");
$this->debugmsg("Checking for responce from modem");
//Check modem connected
fputs($this->fp, "AT\r");
//Wait for ok
$status = $this->wait_reply("OK\r\n", 5);
if (!$status) {
throw new Exception('Did not receive responce from modem');
}
$this->debugmsg('Modem connected');
//Set modem to SMS text mode
$this->debugmsg('Setting text mode');
fputs($this->fp, "AT+CMGF=1\r");
$status = $this->wait_reply("OK\r\n", 5);
if (!$status) {
throw new Exception('Unable to set text mode');
}
$this->debugmsg('Text mode set');
}
//Wait for reply from modem
private function wait_reply($expected_result, $timeout) {
$this->debugmsg("Waiting {$timeout} seconds for expected result");
//Clear buffer
$this->buffer = '';
//Set timeout
$timeoutat = time() + $timeout;
//Loop until timeout reached (or expected result found)
do {
$this->debugmsg('Now: ' . time() . ", Timeout at: {$timeoutat}");
$buffer = fread($this->fp, 1024);
$this->buffer .= $buffer;
usleep(200000);//0.2 sec
$this->debugmsg("Received: {$buffer}");
//Check if received expected responce
if (preg_match('/'.preg_quote($expected_result, '/').'$/', $this->buffer)) {
$this->debugmsg('Found match');
return true;
//break;
} else if (preg_match('/\+CMS ERROR\:\ \d{1,3}\r\n$/', $this->buffer)) {
return false;
}
} while ($timeoutat > time());
$this->debugmsg('Timed out');
return false;
}
//Print debug messages
private function debugmsg($message) {
if ($this->debug == true) {
$message = preg_replace("%[^\040-\176\n\t]%", '', $message);
echo $message . "\n";
}
}
//Close port
public function close() {
$this->debugmsg('Closing port');
fclose($this->fp);
}
//Send message
public function send($tel, $message) {
//Filter tel
$tel = preg_replace("%[^0-9\+]%", '', $tel);
//Filter message text
$message = preg_replace("%[^\040-\176\r\n\t]%", '', $message);
$this->debugmsg("Sending message \"{$message}\" to \"{$tel}\"");
//Start sending of message
fputs($this->fp, "AT+CMGS=\"{$tel}\"\r");
//Wait for confirmation
$status = $this->wait_reply("\r\n> ", 5);
if (!$status) {
//throw new Exception('Did not receive confirmation from modem');
$this->debugmsg('Did not receive confirmation from modem');
return false;
}
//Send message text
fputs($this->fp, $message);
//Send message finished indicator
fputs($this->fp, chr(26));
//Wait for confirmation
$status = $this->wait_reply("OK\r\n", 180);
if (!$status) {
//throw new Exception('Did not receive confirmation of messgage sent');
$this->debugmsg('Did not receive confirmation of messgage sent');
return false;
}
$this->debugmsg("Message sent");
return true;
}
}
?>
Try this below code hope it helps,
Note : uses php_serial.class.php
Code :
<?php
require("php_serial.class.php");
$serial = new phpSerial;
// First we must specify the device. This works on both linux and windows (if
// your linux serial device is /dev/ttyS0 for COM1, etc)
$serial->deviceSet("/dev/ttyS0");
// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
// Then we need to open it
$serial->deviceOpen();
// To write into
$serial->sendMessage("AT+CMGF=1\n\r");
$serial->sendMessage("AT+cmgs=\"+912709345x23\"\n\r");
$serial->sendMessage("Hi, This is the sms that I sent to you using PHP script with serial Port communication with my phone as serial device!! GN - VKSALIAN \n\r");
$serial->sendMessage(chr(26));
//wait for modem to send message
sleep(7);
// Or to read from
$read = $serial->readPort();
echo $read;
// If you want to change the configuration, the device must be closed
$serial->deviceClose();
?>

Categories