AT Command

Discussion in 'PHP' started by amirsarb, Apr 18, 2007.

  1. #1
    hi
    I need to send "AT" Commands to a GSM Modem that is
    connected to COM1 but idon't know how to use AT command in php.

    best regards
     
    amirsarb, Apr 18, 2007 IP
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    I'm not sure if that exsist maybe you need to use other language like activex ?
     
    commandos, Apr 18, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    
    <?php
    class comport
    {
     	var $port  ;
     	
    	function comport( $portno )
    	{
    		$this->port = @fopen( sprintf( 'COM%d:', $portno ), 'w+' );
    		if( !$this->port )
    			die("Cannot open COM$portno, please check it is not in use");		
    	}
    	function write( $data )
    	{
    		return fwrite( $this->port, $data, strlen( $data ) );	
    	}
    	function _close( )
    	{
    		return fclose( $this->port );
    	}
    }
    /**
     I HAVE NO IDEA HOW THESE COMMANDS ARE SUPPOSED TO BE FORMATTED
     FIND OUT IF YOU NEED TO WRITE BINARY DATA OR DECIMAL OR WHATEVER
     by all means try a string first
    **/
    $at = "AT XXXXX";
    /**
     Open com port 1
    **/
    $com = new comport( 1 );
    /**
     Write to com port 1
    **/
    if( $com->write( $at ) )
    	echo "Wrote $at to COM1";
    /**
     Close com port 1
    **/
    $com->_close( );
    ?>
    
    PHP:
     
    krakjoe, Apr 18, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    I found this interesting, and would edit the above post if I could, I have no idea what phone you use, or if you'll ever come back, but I gotta old moto v220 sitting about at home, so I connected it upto a com port and heres what I came up with :

    
    <?php
    error_reporting( 0 );
    class v220
    {
    	var $portno;
    	var $phonebooks;
    	var $serialno;
    	var $revision;
    	var $charsets;
    	var $modeldesc;
    	
    	function v220( $portno )
    	{
    		$this->portno = $portno;
    		$testport = $this->command( "AT" );
    		
    		if( trim( $testport[0] ) != "OK" ):
    			die("Cannot send commands to com $this->portno\r\n");
    		endif;
    		
    		$this->phonebooks = $this->getpbs( );
    		
    		$this->serialno = $this->getserial( );
    		
    		$this->revision = $this->getrevision( );
    		
    		$this->charsets = $this->getcharsets( );
    		
    		$this->modeldesc = $this->getmodeldesc( );
    	}
    	function selectcharset( $charset )
    	{
    		if( in_array( $charset, $this->charsets ) ):
    			$data = $this->command( "AT+CSCS=$charset" );
    			return trim( $data[0] ) == "OK" ? true : false;
    		endif;
    	}
    	function command( $command )
    	{
    		if( !( $write = fopen( "COM$this->portno:", 'a' ) ) ):
    			print("Cannot open com $this->portno for writing\r\n");
    			return false;
    		else:
    			fwrite( $write, $command . "\r\n" );
    			fclose( $write );
    		endif;
    		
    		if( !($read = fopen("COM$this->portno:", 'r')) ):
    			print("Cannot open com $this->portno for reading\r\n");
    		else:
    			$data = null;
    			while( !feof( $read ) ) :
    				$data .= fgets( $read );
    				if( preg_match( "#(OK|ERROR)#si", $data, $code )): break; endif;
    			endwhile;
    			fclose( $read );
    		endif;	
    		
    		return array( $code[1], split("\n", $data ) );
    	}
    	function extended(  )
    	{
    		$data = $this->command( "AT+MODE=2" );
    		return trim( $data[0] ) == "OK" ? true : false;
    	}
    	function dextended( )
    	{
    		$data = $this->command( "AT+MODE=0" );
    		return trim( $data[0] ) == "OK" ? true : false;
    	}
    	function getserial( )
    	{
    		$data = $this->command( "AT+CGSN" );
    		if( trim( $data[0] ) == "OK" ):
    			$temp = split( ":", $data[1][1] );
    			return trim( $temp[1] );
    		endif;
    	}
    	function getcharsets( )
    	{
    		$data = $this->command( "AT+CSCS=?" );
    		if( trim( $data[0] ) == "OK" ):
    			$temp = split( ":", $data[1][1] );
    			preg_match_all( '#"(.*?[^"])"#s', $temp[1], $charsets );
    			return $charsets[1];
    		endif;
    	}
    	function getmodeldesc( )
    	{
    		$data = $this->command( "AT+CGMM" );
    		if( trim( $data[0] ) == "OK" ):
    			$temp = split( ":", $data[1][1] );
    			preg_match_all( '#"(.*?[^"])"#s', $temp[1], $mdesc );
    			return $mdesc[1];
    		endif;
    	}
    	function searchpbbyname( $name )
    	{
    		$entries = null;
    		$data = $this->command( "AT+MPBF=\"$name\"" );
    		if( trim( $data[0] ) == "OK" ):
    			foreach( $data[1] as $result )
    			{
    				preg_match_all( '#"(.*?[^"])"#s', $result, $details );
    				if( trim( $details[1][1] ) != "" ):
    					$entries[] = $details[1][0];
    				endif;
    			}	
    		endif;
    		return $entries;
    	}
    	function getrevision( )
    	{
    		$data = $this->command( "AT+CGMR" );
    		if( trim( $data[0] ) == "OK" ):
    			$temp = split( ":", $data[1][1] );
    			return preg_replace('#"#s', '', trim( $temp[1] ) );
    		endif;
    	}
    	function htmlout( $output = "debug.html" )
    	{
    		$debug = fopen( $output, "w+" );
    		foreach( $this as $k => $v )
    		{
    			switch( $k )
    			{
    				case "portno" :
    					fwrite( $debug, "<h2>Connected to COM$v</h2>\n" );
    				break;
    				
    				case "phonebooks" :
    					fwrite( $debug, "<h2>Phonebooks available</h2>\n" );
    					fwrite( $debug, "<ul>\n" );
    					foreach( $v as $phonebook )
    					{
    						fwrite( $debug, "<li>$phonebook</li>\n" );
    					}
    					fwrite( $debug, "</ul>\n" );
    				break;
    				
    				case "serialno" :
    					fwrite( $debug, "<h2>Serial : $v</h2>\n" );
    				break;
    				
    				case "revision" :
    					fwrite( $debug, "<h2>Revision : $v</h2>\n" );
    				break;
    				
    				case "charsets" :
    					fwrite( $debug, "<h2>Charsets available</h2>\n" );
    					fwrite( $debug, "<ul>\n" );
    					foreach( $v as $charset )
    					{
    						fwrite( $debug, "<li>$charset</li>\n" );
    					}
    					fwrite( $debug, "</ul>\n" );
    				break;
    				
    				case "modeldesc" :
    					fwrite( $debug, "<h2>Model description data</h2>\n" );
    					fwrite( $debug, "<ul>\n" );
    					foreach( $v as $mdesc )
    					{
    						fwrite( $debug, "<li>$mdesc</li>\n" );
    					}
    					fwrite( $debug, "</ul>\n" );
    				break;
    			}
    		}
    		fclose( $debug );
    	}
    	function getpbs( )
    	{
    		$data = $this->command( "AT+CPBS=?" );
    		if( trim( $data[0] ) == "OK" ):
    			preg_match_all( '#"([A-Z]{2})"#si', $data[1][1], $pbs );
    		else:
    			return false;
    		endif;
    		return $pbs[1]; 
    	}
    }
    $com = new v220( 3 );
    $com->htmlout( );
    ?>
    
    PHP:
    and that gives me in html

    Connected to COM3
    Phonebooks available

    * ME
    * SM
    * MT
    * ON
    * DC
    * MC
    * RC
    * AD
    * QD

    Serial : IMEI353872006091164
    Revision : R364_G_0B.D1.09R
    Charsets available

    * 8859-1
    * ASCII
    * GSM
    * UCS2
    * UTF8

    Model description data

    * GSM900
    * GSM1800
    * GSM1900
    * MODEL=V220

    I dunno what else you might do with it, but it's a working example, and gives you a generic command() method ..... enjoy ......
     
    krakjoe, Apr 20, 2007 IP
  5. moin

    moin Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello KrakJoe, I need ur help

    Could u find me a solution to this problem which is at this mentioned at this post,

    http://forums.digitalpoint.com/showthread.php?t=304960

    Quick help is needed
     
    moin, Apr 20, 2007 IP
  6. amirsarb

    amirsarb Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    sorry i forget to say i'm working with linux fedora
    i just need to know how to use AT command in linux
    at the final i shoud use AT Command in my php application in linux.
     
    amirsarb, Apr 20, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    change COM to /dev/ttsy0 or whatever it is on ur os.....got no linux locally so cant test it, but I know it'll work.......
     
    krakjoe, Apr 20, 2007 IP
  8. amirsarb

    amirsarb Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i changed COM to /dev/ttsy0 but fopen cann't do any thing
    on it.

    Error:
    Cannot open ttyS...

    i guess that ttyS is not a file like com port in windows.
     
    amirsarb, Apr 26, 2007 IP
  9. amirsarb

    amirsarb Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i changed ttyS permission and used your code, but i can't read
    from serial port with "fread" or "fputs".
     
    amirsarb, Apr 27, 2007 IP
  10. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #10
    That's right, the reason being fgets reads upto a new line properly on serial ports, fread doesn't it waits for data and the port hangs, it might not be ttsy0 that was a guess you need to know your modem device, and should not need to change it's permissions.

    COM ports and /dev/ttsy0 is the same thing, definately.
     
    krakjoe, Apr 27, 2007 IP