n_gsm 是一种tty设备上的线路规程(line discipline),来实现3gpp MUX协议
n_gsm 实现方法如下:
1. kernel配置文件中 打开 CONFIG_N_GSM=y 编译内核
2. cat /proc/device | grep gsmtty 查看gsmtty
主节点号 3. 建立虚拟串口
例如:(例子中主节点号为251)
busybox mknod /dev/ttygsm0 c 251 0
busybox mknod /dev/ttygsm1 c 251 1
busybox mknod /dev/ttygsm2 c 251 2
busybox mknod /dev/ttygsm3 c 251 3
busybox mknod /dev/ttygsm4 c 251 4
4. 打开串口,进行属性设置
5 用CMUX设置modem mux模式
6.用TIOCSETD ioctl设置串口线路规程 (line discipline )
下面是示例代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define N_GSM0710 21
#define DEFAULT_SPEED B115200
#define SERIAL_PORT "/dev/ttyO3"
int main()
{
int fd;
int ldisc = N_GSM0710;
int status;
struct gsm_config c;
struct termios Opt;
//struct termios configuration;
fd = open(SERIAL_PORT, O_RDWR);
printf("\ntry to set 115200\n");
tcgetattr(fd, &Opt);
//Opt.c_lflag = 0;
Opt.c_iflag = 0;
Opt.c_cflag=7346;
Opt.c_lflag=0;
Opt.c_oflag=0; // 4
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, B115200);
cfsetospeed(&Opt, B115200);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
printf("\ntcsetattr error");
}
tcflush(fd,TCIOFLUSH);
printf("\nterm set ok\n");
write(fd, "AT+SCMUX=3\r", 11);
sleep(3);
ioctl(fd, TIOCSETD, &ldisc);
ioctl(fd, GSMIOC_GETCONF, &c);
c.initiator = 1;
c.encapsulation = 0;
c.mru = 127;
c.mtu = 127;
ioctl(fd, GSMIOC_SETCONF, &c);
sleep(1);
daemon(0,0);
pause();
return 0;
}
精彩博文推荐阅读: