一、CMUX 介绍
CMUX,即串口多路复用,是一种用于在串行通信中实现多路复用的协议。它允许在单个串行连接上同时传输多个数据流,从而解决了多个数据流同时发送的问题。具体来说,CMUX协议利用一个底层物理串口,向上层系统提供多个逻辑串口,每个逻辑串口对应着一个数据链路连接(DLC)。这样,就可以在一条串行接口上同时存在多个会话,如语音、FAX、数据、SMS、GPRS、USSD等。当正在进行传真、数据或GPRS呼叫时,这种多路复用特别有利,因为控制模块或使用SMS服务可以通过附加信道完成,而不会干扰数据流,也无需访问第二个UART。
CMUX协议包通常包括地址字段、控制字段、数据字段和校验字段。地址字段指定数据包的目标设备或通道;控制字段包含用于管理多路复用连接的控制信息,如连接建立、断开等;数据字段是实际传输的数据内容;校验字段用于校验数据包的完整性,确保数据在传输过程中没有损坏。
总的来说CMUX( Connection Multiplexing ),即连接(串口)多路复用,其功能主要在一个真实的物理通道上虚拟多个通道,每个虚拟通道上的连接和数据通讯可独立进行。串口的多路复用器模式,就是使一个串行接口能够将数据传输到四个不同的客户应用程序。CMUX 的设计需求来自于蜂窝模块:蜂窝模块作为一种基础通信模块,设计时就有电话,短信,上网等需求,但通常模块上的片上外设资源比较少,于是 GSMMUX 应运而生。GSM0707/GSM0710 协议提供了解决问题的方法,在同一个串口上同时传输 AT, PPP, MODEM 等数据而互不干扰,也就是可以传输电话,上网,短信等数据。GSMMUX 可以解决在一个串口上与蜂窝模块通信传输多种数据的场景,那 CMUX 就同样可以解决在两个 CMUX 上只使用一个物理串口传输多种 APP 数据的使用场景。
RT-Thread 关于CMUX的介绍
CMUX 是一种类似于传输层的协议,用户使用时无法感知该层;数据传输依赖一个真实串口传输,cmux 层负责解析数据用以分发到不同的 virtual uart ;从而实现一个真实串口虚拟出多个 UART 的目的。
CMUX 在应用场景中多用于 UART, 如有必要也可以支持 SPI 方式。
RT-Thread软件包CMUX
支持所有基于 GSM0707 / GSM0710 协议的蜂窝模块
1.兼容 PPP_DEVICE 软件包,实现 CMUX+PPP 场景,在一个串口上同时传输 AT 命令与 PPP 数据;解决PPP 通信独占一个物理串口的问题。
2.CMUX 的 GSM 功能已经在 Luat Air720, SIM7600, SIM800C 模块测试通过。
支持通过一个真实串口虚拟出多个串口。
1.CMUX 协议限制,理论上一个物理串口最多可以虚拟出 63 个虚拟串口。
2.虚拟出的串口使用与真实串口一致,拥有 open,write,read 操作,设置 rx_indicate 接收回调。
3.两方的 MCU 接收时要注意 DLCI ( Data Link Connection Identifier ) 数据链路连接接标识,相同的 DLCI 才可以互相正常通信。
二、CMUX 帧结构
多路复用有三种操作模式:基本模式、带错误恢复功能的高级模式、不带错误恢复功能的高级模式(具体可以参见GSM 07.10协议)。不同的GPRS模块也支持不同的模式。
其中,帧类型可分控制帧和信息帧。
CMUX双方通过控制帧协商用以构建、拆除虚拟链路,控制帧又为以下几种:
SABM:建立DLC
UA:响应SABM帧或DISC帧
DM:链路未成功建立时,对收到的DISC命令的响应
DISC:通知对端拆除链接,在DLC0发送DISC帧,等于退出MUX功能
信息帧:
UIH\ UI \ I:这三种是信息帧,也就是携带要传输数据的帧。
三、Linux 内核配置
要在Linux下使用模块的CMUX功能,需要在内核中开启相应的支持,开启方法见下图。
搜索 N_GSM 确定位置并查看是否开启。
N_GSM是否,则需要开启如下功能
此时再查看N_GSM 则变为y
内核编译,下载到开发板。
官方测试代码:
//gsmmux.h 文件
#ifndef _LINUX_GSMMUX_H
#define _LINUX_GSMMUX_H
struct gsm_config
{
unsigned int adaption;
unsigned int encapsulation;
unsigned int initiator;
unsigned int t1;
unsigned int t2;
unsigned int t3;
unsigned int n2;
unsigned int mru;
unsigned int mtu;
unsigned int k;
unsigned int i;
unsigned int unused[8]; /* Padding for expansion without
breaking stuff */
};
#define GSMIOC_GETCONF _IOR('G', 0, struct gsm_config)
#define GSMIOC_SETCONF _IOW('G', 1, struct gsm_config)
struct gsm_netconfig {
unsigned int adaption; /* Adaption to use in network mode */
unsigned short protocol;/* Protocol to use - only ETH_P_IP supported */
unsigned short unused2;
char if_name[IFNAMSIZ]; /* interface name format string */
__u8 unused[28]; /* For future use */
};
#define GSMIOC_ENABLE_NET _IOW('G', 2, struct gsm_netconfig)
#define GSMIOC_DISABLE_NET _IO('G', 3)
#endif
//cmux.c文件
/**
* Cmux
* Enables GSM 0710 multiplex using n_gsm
*
* Copyright (C) 2013 - Rtone - Nicolas Le Manchet <nicolaslm@rtone.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <net/if.h>
#include <linux/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <signal.h>
/**
* gsmmux.h provides n_gsm line dicipline structures and functions.
* It should be kept in sync with your kernel release.
*/
#include "gsmmux.h"
/* n_gsm ioctl */
#ifndef N_GSM0710
# define N_GSM0710 21
#endif
/* attach a line discipline ioctl */
#ifndef TIOCSETD
# define TIOCSETD 0x5423
#endif
/* serial port of the modem */
#define SERIAL_PORT "/dev/ttyS1"
/* line speed */
#define LINE_SPEED B115200
/* maximum transfert unit (MTU), value in bytes */
#define MTU 512
/**
* whether or not to create virtual TTYs for the multiplex
* 0 : do not create
* 1 : create
*/
#define CREATE_NODES 1
/* number of virtual TTYs to create (most modems can handle up to 4) */
#define NUM_NODES 4
/* name of the virtual TTYs to create */
#define BASENAME_NODES "/dev/ttyGSM"
/* name of the driver, used to get the major number */
#define DRIVER_NAME "gsmtty"
/**
* whether or not to print debug messages to stderr
* 0 : debug off
* 1 : debug on
*/
#define DEBUG 1
/**
* whether or not to detach the program from the terminal
* 0 : do not daemonize
* 1 : daemonize
*/
#define DAEMONIZE 1
/* size of the reception buffer which gets data from the serial line */
#define SIZE_BUF 256
/**
* Prints debug messages to stderr if debug is wanted
*/
static void dbg(char *fmt, ...) {
va_list args;
if (DEBUG) {
fflush(NULL);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
fflush(NULL);
}
return;
}
/**
* Sends an AT command to the specified line and gets its result
* Returns 0 on success
* -1 on failure
*/
int send_at_command(int serial_fd, char *command) {
char buf[SIZE_BUF];
int r;
/* write the AT command to the serial line */
if (write(serial_fd, command, strlen(command)) <= 0)
err(EXIT_FAILURE, "Cannot write to %s", SERIAL_PORT);
/* wait a bit to allow the modem to rest */
sleep(1);
/* read the result of the command from the modem */
memset(buf, 0, sizeof(buf));
r = read(serial_fd, buf, sizeof(buf));
if (r == -1)
err(EXIT_FAILURE, "Cannot read %s", SERIAL_PORT);
/* if there is no result from the modem, return failure */
if (r == 0) {
dbg("%s\t: No response", command);
return -1;
}
/* if we have a result and want debug info, strip CR & LF out from the output */
if (DEBUG) {
int i;
char bufp[SIZE_BUF];
memcpy(bufp, buf, sizeof(buf));
for(i=0; i<strlen(bufp); i++) {
if (bufp[i] == '\r' || bufp[i] == '\n')
bufp[i] = ' ';
}
dbg("%s\t: %s", command, bufp);
}
/* if the output shows "OK" return success */
if (strstr(buf, "OK\r") != NULL) {
return 0;
}
return -1;
}
/**
* Function raised by signal catching
*/
void signal_callback_handler(int signum) {
return;
}
/**
* Gets the major number of the driver device
* Returns the major number on success
* -1 on failure
*/
int get_major(char *driver) {
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
char device[20];
int major = -1;
/* open /proc/devices file */
if ((fp = fopen("/proc/devices", "r")) == NULL)
err(EXIT_FAILURE, "Cannot open /proc/devices");
/* read the file line by line */
while ((major == -1) && (read = getline(&line, &len, fp)) != -1) {
/* if the driver name string is found in the line, try to get the major */
if (strstr(line, driver) != NULL) {
if (sscanf(line,"%d %s\n", &major, device) != 2)
major = -1;
}
/* free the line before getting a new one */
if (line) {
free(line);
line = NULL;
}
}
/* close /proc/devices file */
fclose(fp);
return major;
}
/**
* Creates nodes for the virtual TTYs
* Returns the number of nodes created
*/
int make_nodes(int major, char *basename, int number_nodes) {
int minor, created = 0;
dev_t device;
char node_name[15];
mode_t oldmask;
/* set a new mask to get 666 mode and stores the old one */
oldmask = umask(0);
for (minor=1; minor<number_nodes+1; minor++) {
/* append the minor number to the base name */
sprintf(node_name, "%s%d", basename, minor);
/* store a device info with major and minor */
device = makedev(major, minor);
/* create the actual character node */
if (mknod(node_name, S_IFCHR | 0666, device) != 0) {
warn("Cannot create %s", node_name);
} else {
created++;
dbg("Created %s", node_name);
}
}
/* revert the mask to the old one */
umask(oldmask);
return created;
}
/**
* Removes previously created TTY nodes
* Returns nothing, it doesn't really matter if it fails
*/
void remove_nodes(char *basename, int number_nodes) {
int node;
char node_name[15];
for (node=1; node<number_nodes+1; node++) {
/* append the minor number to the base name */
sprintf(node_name, "%s%d", basename, node);
/* unlink the actual character node */
dbg("Removing %s", node_name);
if (unlink(node_name) == -1)
warn("Cannot remove %s", node_name);
}
return;
}
int main(void) {
int serial_fd, major;
struct termios tio;
int ldisc = N_GSM0710;
struct gsm_config gsm;
char atcommand[40];
/* print global parameters */
dbg("SERIAL_PORT = %s", SERIAL_PORT);
/* open the serial port */
serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
if (serial_fd == -1)
err(EXIT_FAILURE, "Cannot open %s", SERIAL_PORT);
/* get the current attributes of the serial port */
if (tcgetattr(serial_fd, &tio) == -1)
err(EXIT_FAILURE, "Cannot get line attributes");
/* set the new attrbiutes */
tio.c_iflag = 0;
tio.c_oflag = 0;
tio.c_cflag = CS8 | CREAD | CLOCAL;
tio.c_cflag |= CRTSCTS;
tio.c_lflag = 0;
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
/* write the speed of the serial line */
if (cfsetospeed(&tio, LINE_SPEED) < 0 || cfsetispeed(&tio, LINE_SPEED) < 0)
err(EXIT_FAILURE, "Cannot set line speed");
/* write the attributes */
if (tcsetattr(serial_fd, TCSANOW, &tio) == -1)
err(EXIT_FAILURE, "Cannot set line attributes");
/**
* Send AT commands to put the modem in CMUX mode.
* This is vendor specific and should be changed
* to fit your modem needs.
* The following matches Quectel M95.
*/
if (send_at_command(serial_fd, "AT+IFC=2,2\r") == -1)
errx(EXIT_FAILURE, "AT+IFC=2,2: bad response");
if (send_at_command(serial_fd, "AT+GMM\r") == -1)
warnx("AT+GMM: bad response");
if (send_at_command(serial_fd, "AT\r") == -1)
warnx("AT: bad response");
if (send_at_command(serial_fd, "AT+IPR=115200&w\r") == -1)
errx(EXIT_FAILURE, "AT+IPR=115200&w: bad response");
sprintf(atcommand, "AT+CMUX=0,0,5,%d,10,3,30,10,2\r", MTU);
if (send_at_command(serial_fd, atcommand) == -1)
errx(EXIT_FAILURE, "Cannot enable modem CMUX");
/* use n_gsm line discipline */
sleep(2);
if (ioctl(serial_fd, TIOCSETD, &ldisc) < 0)
err(EXIT_FAILURE, "Cannot set line dicipline. Is 'n_gsm' module registred?");
/* get n_gsm configuration */
if (ioctl(serial_fd, GSMIOC_GETCONF, &gsm) < 0)
err(EXIT_FAILURE, "Cannot get GSM multiplex parameters");
/* set and write new attributes */
gsm.initiator = 1;
gsm.encapsulation = 0;
gsm.mru = MTU;
gsm.mtu = MTU;
gsm.t1 = 10;
gsm.n2 = 3;
gsm.t2 = 30;
gsm.t3 = 10;
if (ioctl(serial_fd, GSMIOC_SETCONF, &gsm) < 0)
err(EXIT_FAILURE, "Cannot set GSM multiplex parameters");
dbg("Line dicipline set");
/* create the virtual TTYs */
if (CREATE_NODES) {
int created;
if ((major = get_major(DRIVER_NAME)) < 0)
errx(EXIT_FAILURE, "Cannot get major number");
if ((created = make_nodes(major, BASENAME_NODES, NUM_NODES)) < NUM_NODES)
warnx("Cannot create all nodes, only %d/%d have been created.", created, NUM_NODES);
}
/* detach from the terminal if needed */
if (DAEMONIZE) {
dbg("Going to background");
if (daemon(0,0) != 0)
err(EXIT_FAILURE, "Cannot daemonize");
}
/* wait to keep the line discipline enabled, wake it up with a signal */
signal(SIGINT, signal_callback_handler);
signal(SIGTERM, signal_callback_handler);
pause();
/* remove the created virtual TTYs */
if (CREATE_NODES) {
remove_nodes(BASENAME_NODES, NUM_NODES);
}
/* close the serial line */
close(serial_fd);
return EXIT_SUCCESS;
}
//Makefile 文件
CC=gcc
CFLAGS=-Wall
all: cmux
program: cmux.c
clean:
rm -f cmux
run: cmux
./cmux
遇到的问题
./cmux: Cannot read /dev/ttyS2: Resource temporarily unavailable
解决如下,但是测试没成功…
I had the similar problem, after adding .c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG)
it works.
修改cmux.c 代码如下:
/**
* Cmux
* Enables GSM 0710 multiplex using n_gsm
*
* Copyright (C) 2013 - Rtone - Nicolas Le Manchet <nicolaslm@rtone.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <net/if.h>
#include <linux/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <signal.h>
#include <errno.h>
/**
* gsmmux.h provides n_gsm line dicipline structures and functions.
* It should be kept in sync with your kernel release.
*/
#include "gsmmux.h"
/* n_gsm ioctl */
#ifndef N_GSM0710
# define N_GSM0710 21
#endif
/* attach a line discipline ioctl */
#ifndef TIOCSETD
# define TIOCSETD 0x5423
#endif
/* serial port of the modem */
#define SERIAL_PORT "/dev/ttyS2"
/* line speed */
#define LINE_SPEED B115200
/* maximum transfert unit (MTU), value in bytes */
#define MTU 512
/**
* whether or not to create virtual TTYs for the multiplex
* 0 : do not create
* 1 : create
*/
#define CREATE_NODES 1
/* number of virtual TTYs to create (most modems can handle up to 4) */
#define NUM_NODES 4
/* name of the virtual TTYs to create */
#define BASENAME_NODES "/dev/ttyGSM"
/* name of the driver, used to get the major number */
#define DRIVER_NAME "gsmtty"
/**
* whether or not to print debug messages to stderr
* 0 : debug off
* 1 : debug on
*/
#define DEBUG 1
/**
* whether or not to detach the program from the terminal
* 0 : do not daemonize
* 1 : daemonize
*/
#define DAEMONIZE 1
/* size of the reception buffer which gets data from the serial line */
#define SIZE_BUF 256
/**
* Prints debug messages to stderr if debug is wanted
*/
static void dbg(char *fmt, ...) {
va_list args;
if (DEBUG) {
fflush(NULL);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
fflush(NULL);
}
return;
}
/**
* Sends an AT command to the specified line and gets its result
* Returns 0 on success
* -1 on failure
*/
int send_at_command(int serial_fd, char *command) {
char buf[SIZE_BUF];
int r;
/* write the AT command to the serial line */
if (write(serial_fd, command, strlen(command)) <= 0)
err(EXIT_FAILURE, "Cannot write to %s", SERIAL_PORT);
/* wait a bit to allow the modem to rest */
sleep(1);
/* read the result of the command from the modem */
memset(buf, 0, sizeof(buf));
r = read(serial_fd, buf, sizeof(buf));
if (r == -1)
err(EXIT_FAILURE, "Cannot read %s", SERIAL_PORT);
/* if there is no result from the modem, return failure */
if (r == 0) {
dbg("%s\t: No response", command);
return -1;
}
/* if we have a result and want debug info, strip CR & LF out from the output */
if (DEBUG) {
int i;
char bufp[SIZE_BUF];
memcpy(bufp, buf, sizeof(buf));
for(i=0; i<strlen(bufp); i++) {
if (bufp[i] == '\r' || bufp[i] == '\n')
bufp[i] = ' ';
}
dbg("%s\t: %s", command, bufp);
}
/* if the output shows "OK" return success */
if (strstr(buf, "OK\r") != NULL) {
return 0;
}
return -1;
}
/**
* Function raised by signal catching
*/
void signal_callback_handler(int signum) {
return;
}
/**
* Gets the major number of the driver device
* Returns the major number on success
* -1 on failure
*/
int get_major(char *driver) {
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
char device[20];
int major = -1;
/* open /proc/devices file */
if ((fp = fopen("/proc/devices", "r")) == NULL)
err(EXIT_FAILURE, "Cannot open /proc/devices");
/* read the file line by line */
while ((major == -1) && (read = getline(&line, &len, fp)) != -1) {
/* if the driver name string is found in the line, try to get the major */
if (strstr(line, driver) != NULL) {
if (sscanf(line,"%d %s\n", &major, device) != 2)
major = -1;
}
/* free the line before getting a new one */
if (line) {
free(line);
line = NULL;
}
}
/* close /proc/devices file */
fclose(fp);
return major;
}
/**
* Creates nodes for the virtual TTYs
* Returns the number of nodes created
*/
int make_nodes(int major, char *basename, int number_nodes) {
int minor, created = 0;
dev_t device;
char node_name[15];
mode_t oldmask;
/* set a new mask to get 666 mode and stores the old one */
oldmask = umask(0);
for (minor=1; minor<number_nodes+1; minor++) {
/* append the minor number to the base name */
sprintf(node_name, "%s%d", basename, minor);
/* store a device info with major and minor */
device = makedev(major, minor);
/* create the actual character node */
if (mknod(node_name, S_IFCHR | 0666, device) != 0) {
warn("Cannot create %s", node_name);
} else {
created++;
dbg("Created %s", node_name);
}
}
/* revert the mask to the old one */
umask(oldmask);
return created;
}
/**
* Removes previously created TTY nodes
* Returns nothing, it doesn't really matter if it fails
*/
void remove_nodes(char *basename, int number_nodes) {
int node;
char node_name[15];
for (node=1; node<number_nodes+1; node++) {
/* append the minor number to the base name */
sprintf(node_name, "%s%d", basename, node);
/* unlink the actual character node */
dbg("Removing %s", node_name);
if (unlink(node_name) == -1)
warn("Cannot remove %s", node_name);
}
return;
}
static int fd; /* 串口终端对应的文件描述符 */
static struct termios old_cfg; /* 用于保存终端的配置参数 */
typedef struct uart_hardware_cfg {
unsigned int baudrate; /* 波特率 */
unsigned char dbit; /* 数据位 */
char parity; /* 奇偶校验 */
unsigned char sbit; /* 停止位 */
}uart_cfg_t;
/**
** 串口配置
** 参数cfg指向一个uart_cfg_t结构体对象
**/
static int uart_cfg(const uart_cfg_t *cfg)
{
struct termios new_cfg = {0}; //将new_cfg对象清零
speed_t speed;
/* 设置为原始模式 */
cfmakeraw(&new_cfg);
/* 使能接收 */
new_cfg.c_cflag |= CREAD;
/* 设置波特率 */
switch (cfg->baudrate) {
case 1200: speed = B1200;
break;
case 1800: speed = B1800;
break;
case 2400: speed = B2400;
break;
case 4800: speed = B4800;
break;
case 9600: speed = B9600;
break;
case 19200: speed = B19200;
break;
case 38400: speed = B38400;
break;
case 57600: speed = B57600;
break;
case 115200: speed = B115200;
break;
case 230400: speed = B230400;
break;
case 460800: speed = B460800;
break;
case 500000: speed = B500000;
break;
default: //默认配置为115200
speed = B115200;
printf("default baud rate: 115200\n");
break;
}
if (0 > cfsetspeed(&new_cfg, speed)) {
fprintf(stderr, "cfsetspeed error: %s\n", strerror(errno));
return -1;
}
/* 设置数据位大小 */
new_cfg.c_cflag &= ~CSIZE; //将数据位相关的比特位清零
switch (cfg->dbit) {
case 5:
new_cfg.c_cflag |= CS5;
break;
case 6:
new_cfg.c_cflag |= CS6;
break;
case 7:
new_cfg.c_cflag |= CS7;
break;
case 8:
new_cfg.c_cflag |= CS8;
break;
default: //默认数据位大小为8
new_cfg.c_cflag |= CS8;
// printf("default data bit size: 8\n");
break;
}
/* 设置奇偶校验 */
switch (cfg->parity) {
case 'N': //无校验
new_cfg.c_cflag &= ~PARENB;
new_cfg.c_iflag &= ~INPCK;
break;
case 'O': //奇校验
new_cfg.c_cflag |= (PARODD | PARENB);
new_cfg.c_iflag |= INPCK;
break;
case 'E': //偶校验
new_cfg.c_cflag |= PARENB;
new_cfg.c_cflag &= ~PARODD; /* 清除PARODD标志,配置为偶校验 */
new_cfg.c_iflag |= INPCK;
break;
default: //默认配置为无校验
new_cfg.c_cflag &= ~PARENB;
new_cfg.c_iflag &= ~INPCK;
// printf("default parity: N\n");
break;
}
/* 设置停止位 */
switch (cfg->sbit) {
case 1: //1个停止位
new_cfg.c_cflag &= ~CSTOPB;
break;
case 2: //2个停止位
new_cfg.c_cflag |= CSTOPB;
break;
default: //默认配置为1个停止位
new_cfg.c_cflag &= ~CSTOPB;
// printf("default stop bit size: 1\n");
break;
}
/* 将MIN和TIME设置为0 */
new_cfg.c_cc[VTIME] = 0;
new_cfg.c_cc[VMIN] = 0;
/* 清空缓冲区 */
if (0 > tcflush(fd, TCIOFLUSH)) {
fprintf(stderr, "tcflush error: %s\n", strerror(errno));
return -1;
}
/* 写入配置、使配置生效 */
if (0 > tcsetattr(fd, TCSANOW, &new_cfg)) {
fprintf(stderr, "tcsetattr error: %s\n", strerror(errno));
return -1;
}
/* 配置OK 退出 */
return 0;
}
int main(void) {
int serial_fd, major;
struct termios tio;
int ldisc = N_GSM0710;
struct gsm_config gsm;
char atcommand[40];
uart_cfg_t cfg = {0};
/* print global parameters */
dbg("SERIAL_PORT = %s", SERIAL_PORT);
/* open the serial port */
serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
if (serial_fd == -1)
err(EXIT_FAILURE, "Cannot open %s", SERIAL_PORT);
fd=serial_fd;
/* 获取串口当前的配置参数 */
if (0 > tcgetattr(fd, &old_cfg)) {
fprintf(stderr, "tcgetattr error: %s\n", strerror(errno));
close(fd);
return -1;
}
/* 设置波特率 */
cfg.baudrate = 115200;
/* 串口配置 */
if (uart_cfg(&cfg)) {
tcsetattr(fd, TCSANOW, &old_cfg); //恢复到之前的配置
close(fd);
exit(EXIT_FAILURE);
}
/* get the current attributes of the serial port */
if (tcgetattr(serial_fd, &tio) == -1)
err(EXIT_FAILURE, "Cannot get line attributes");
// /* set the new attrbiutes */
// tio.c_iflag = 0;
// tio.c_oflag = 0;
// tio.c_cflag = CS8 | CREAD | CLOCAL;
// tio.c_cflag |= CRTSCTS;
// tio.c_lflag = 0;
// tio.c_cc[VMIN] = 1;
// tio.c_cc[VTIME] = 0;
/* write the speed of the serial line */
if (cfsetospeed(&tio, LINE_SPEED) < 0 || cfsetispeed(&tio, LINE_SPEED) < 0)
err(EXIT_FAILURE, "Cannot set line speed");
/* write the attributes */
if (tcsetattr(serial_fd, TCSANOW, &tio) == -1)
err(EXIT_FAILURE, "Cannot set line attributes");
/**
* Send AT commands to put the modem in CMUX mode.
* This is vendor specific and should be changed
* to fit your modem needs.
* The following matches Quectel M95.
*/
printf("0\r\n");
if (send_at_command(serial_fd, "AT\r\n") == -1)
errx(EXIT_FAILURE, "AT: bad response");
printf("1\r\n");
if (send_at_command(serial_fd, "AT+IFC=2,2\r\n") == -1)
errx(EXIT_FAILURE, "AT+IFC=2,2: bad response");
printf("2\r\n");
if (send_at_command(serial_fd, "AT+GMM\r\n") == -1)
warnx("AT+GMM: bad response");
printf("3\r\n");
if (send_at_command(serial_fd, "AT\r") == -1)
warnx("AT: bad response");
printf("4\r\n");
if (send_at_command(serial_fd, "AT+IPR=115200&w\r") == -1)
errx(EXIT_FAILURE, "AT+IPR=115200&w: bad response");
printf("5\r\n");
sprintf(atcommand, "AT+CMUX=0,0,5,%d,10,3,30,10,2\r", MTU);
if (send_at_command(serial_fd, atcommand) == -1)
errx(EXIT_FAILURE, "Cannot enable modem CMUX");
printf("6\r\n");
/* use n_gsm line discipline */
sleep(2);
if (ioctl(serial_fd, TIOCSETD, &ldisc) < 0)
err(EXIT_FAILURE, "Cannot set line dicipline. Is 'n_gsm' module registred?");
/* get n_gsm configuration */
if (ioctl(serial_fd, GSMIOC_GETCONF, &gsm) < 0)
err(EXIT_FAILURE, "Cannot get GSM multiplex parameters");
/* set and write new attributes */
gsm.initiator = 1;
gsm.encapsulation = 0;
gsm.mru = MTU;
gsm.mtu = MTU;
gsm.t1 = 10;
gsm.n2 = 3;
gsm.t2 = 30;
gsm.t3 = 10;
if (ioctl(serial_fd, GSMIOC_SETCONF, &gsm) < 0)
err(EXIT_FAILURE, "Cannot set GSM multiplex parameters");
dbg("Line dicipline set");
/* create the virtual TTYs */
if (CREATE_NODES) {
int created;
if ((major = get_major(DRIVER_NAME)) < 0)
errx(EXIT_FAILURE, "Cannot get major number");
if ((created = make_nodes(major, BASENAME_NODES, NUM_NODES)) < NUM_NODES)
warnx("Cannot create all nodes, only %d/%d have been created.", created, NUM_NODES);
}
/* detach from the terminal if needed */
if (DAEMONIZE) {
dbg("Going to background");
if (daemon(0,0) != 0)
err(EXIT_FAILURE, "Cannot daemonize");
}
/* wait to keep the line discipline enabled, wake it up with a signal */
signal(SIGINT, signal_callback_handler);
signal(SIGTERM, signal_callback_handler);
pause();
/* remove the created virtual TTYs */
if (CREATE_NODES) {
remove_nodes(BASENAME_NODES, NUM_NODES);
}
/* close the serial line */
close(serial_fd);
return EXIT_SUCCESS;
}
不在报上面的错误。
四、Air724UG启动CMUX模式
通常模块发送AT+CMUX命令来激活多路复用,该命令格式为:
参数定义:
参考:
https://oldask.openluat.com/article/1077
https://doc.openluat.com/wiki/6?wiki_page_id=557
https://github.com/Rtone/cmux/tree/master
https://www.kernel.org/doc/html/v5.9/driver-api/serial/n_gsm.html
https://docs.kernel.org/driver-api/tty/n_gsm.html
https://docs.kernel.org/6.2/driver-api/tty/n_gsm.html
https://blog.csdn.net/FILLMOREad/article/details/106416867
https://community.toradex.com/t/uart-dev-ttylp1-resource-temporarily-unavailable-on-read/5272/3