|
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
int main(void)
{
int fd;
char sendmsg[] = "your car has been stolen! the position is:";
struct termios oldtio,newtio;
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if( -1 == fd )
{
perror("can't open the serial port!");
return -1;
}
if( tcgetaddr( fd,&oldtio ) != 0)
{
perror( "setup serial!" );
return -1;
}
bzero( &newtio,sizeof(newtio) );
newtio.c_cflag |= CLOCAL | CREAD;
cfsetispeed( &newtio,B115200 );
cfsetospeed( &newtio,B115200 );
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush( fd,TCIFLUSH );
if( ( tcsetattr( fd,TCSANOW,&newtio) ) )
{
perror("com set error!");
return -1;
}
write( fd,"AT+CMGF=1",8 );//设置文本格式
sleep(1);
write( fd,"AT+CMGS=+8613800451500\r",23 ); //设置哈尔滨短信服务中心
sleep(1);
write( fd,"AT+CMGS=861390451234\r",20 ); //设置接收个人号码
sleep(1);
write( fd,sendmsg,sizeof(sendmsg) );// 发送英文短信
sleep(1);
write( fd,"\x01a",1);//发送CTRL+Z的ascii码
sleep(3);
close(fd);
return 0; |
|