linux 中用n_gsm实现3gpp MUX协议

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 <stdio.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <linux/gsmmux.h>
#define N_GSM0710 21 /* GSM 0710 Mux */
#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;
/* open the serial port connected to the modem */
fd = open(SERIAL_PORT, O_RDWR);
/* configure the serial port : speed, flow control ... */
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");
/* send the AT commands to switch the modem to CMUX mode
and check that it's successful (should return OK) */
write(fd, "AT+SCMUX=3\r", 11);
/* experience showed that some modems need some time before
being able to answer to the first MUX packet so a delay
may be needed here in some case */
sleep(3);
/* use n_gsm line discipline */
ioctl(fd, TIOCSETD, &ldisc);
/* get n_gsm configuration */
ioctl(fd, GSMIOC_GETCONF, &c);
/* we are initiator and need encoding 0 (basic) */
c.initiator = 1;
c.encapsulation = 0;
/* our modem defaults to a maximum size of 127 bytes */
c.mru = 127;
c.mtu = 127;
/* set the new configuration */
ioctl(fd, GSMIOC_SETCONF, &c);
sleep(1);
/* and wait for ever to keep the line discipline enabled */
daemon(0,0);
pause();
return 0;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值