串口调试例子:
程序先打印字符输出,等待有输入后打印输入内容结束。
通信速率115200位/秒
参考:http://maker.zlgmcu.com/forum.php?mod=viewthread&tid=391189&highlight=%E4%B8%B2%E5%8F%A3
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define CSCS "AT+CSCS="GSM"
"
#define CMGF "AT+CMGF=1
"
//#define CMGS "AT+CMGS="******"
"
int main (void)
{
//设置串口变量
int fd; //串口返回值
int len,ret; //串口收发数据,串口设置返回值
char buf[16]; //串口接收到的字符串
struct termios opt;
//初始化串口
fd = open("/dev/ttySP1",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd<0){
printf("open SPI device error
");
return -1;
}
//获取opt
if(tcgetattr(fd,&opt)<0){
return -1;
}
//设置波特率
cfsetispeed(&opt,B115200);
cfsetospeed(&opt,B115200);
opt.c_lflag &= ~(ECHO|ICANON|IEXTEN|ISIG);
opt.c_iflag &= ~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
opt.c_oflag &= ~(OPOST);
opt.c_cflag &= ~(CSIZE|PARENB);
opt.c_cflag |= CS8;
opt.c_cc[VMIN] = 255;
opt.c_cc[VTIME] = 150;
if(tcsetattr(fd,TCSANOW,&opt)<0){
return -1;
}
tcflush(fd,TCIFLUSH);
//设置手机卡参数
len = write(fd,CSCS,sizeof(CSCS));
printf(CSCS);
if(len<0){
printf("write cscs erro
");
return -1;
}
while(1){
len = read(fd,buf,sizeof(buf));
if(len > 0){
printf("发送短信返回:%s
",buf);
close(fd);
return 0;
}
}
}