- 论坛徽章:
- 0
|
GSM模块单独在电脑终端测试是没有问题的,但是拿到context A8开发板下就是读取不了串口数据,用的GSM模块是明基M23的。写的程序如下size返回的是-1,recv是空的,搞了几天,就是不知道哪里错了,请大神们帮我检查检查错误,感激不尽....
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
int main()
{
int fd;
char ch;
int size = 0;
unsigned char recv[500];
char *end = "\xd\0";
char *cmd = "AT+CMGR=10";
struct termios options;
fd = open("/dev/s3c2410_serial2", O_RDWR | O_NONBLOCK);
if(fd < 0)
printf("error");
tcgetattr(fd, &options);
options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|IGNCR|ICRNL|IXON);
options.c_cflag &= ~PARENB; //无奇偶校验位
options.c_cflag &= ~CSTOPB; //停止位为1位
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; //数据位为8位
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
cfsetispeed(&options, B115200);//uart in speed
cfsetospeed(&options, B115200);//uart out speed
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
write(fd, cmd, strlen(cmd));
write(fd, end, strlen(end));
sleep(3);
size = read(fd, recv, sizeof(recv));
printf("%d\n", size);
recv[size] = '\0';
printf("%s", recv);
close(fd);
return 0;
} |
|