CN112416521A - 一种基于linux tty子系统的串口虚拟化的实现方法 - Google Patents
一种基于linux tty子系统的串口虚拟化的实现方法 Download PDFInfo
- Publication number
- CN112416521A CN112416521A CN202011328809.9A CN202011328809A CN112416521A CN 112416521 A CN112416521 A CN 112416521A CN 202011328809 A CN202011328809 A CN 202011328809A CN 112416521 A CN112416521 A CN 112416521A
- Authority
- CN
- China
- Prior art keywords
- serial port
- port
- uart
- data
- serial
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 21
- 230000006870 function Effects 0.000 claims description 39
- 238000012545 processing Methods 0.000 claims description 9
- 239000000523 sample Substances 0.000 claims description 9
- 238000001514 detection method Methods 0.000 claims description 5
- 230000005540 biological transmission Effects 0.000 claims description 3
- 238000012360 testing method Methods 0.000 claims description 3
- 238000010586 diagram Methods 0.000 description 2
- 238000005516 engineering process Methods 0.000 description 2
- 230000008569 process Effects 0.000 description 2
- 238000006243 chemical reaction Methods 0.000 description 1
- 238000004891 communication Methods 0.000 description 1
- 230000006872 improvement Effects 0.000 description 1
- 238000013508 migration Methods 0.000 description 1
- 230000005012 migration Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000000926 separation method Methods 0.000 description 1
- 238000004088 simulation Methods 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/455—Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
- G06F9/45533—Hypervisors; Virtual machine monitors
- G06F9/45558—Hypervisor-specific management and integration aspects
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/4401—Bootstrapping
- G06F9/4411—Configuring for operating with peripheral devices; Loading of device drivers
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/455—Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
- G06F9/45533—Hypervisors; Virtual machine monitors
- G06F9/45558—Hypervisor-specific management and integration aspects
- G06F2009/45562—Creating, deleting, cloning virtual machine instances
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/455—Emulation; Interpretation; Software simulation, e.g. virtualisation or emulation of application or operating system execution engines
- G06F9/45533—Hypervisors; Virtual machine monitors
- G06F9/45558—Hypervisor-specific management and integration aspects
- G06F2009/45591—Monitoring or debugging support
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Computer Security & Cryptography (AREA)
- Information Transfer Systems (AREA)
Abstract
本发明提供一种基于linux tty子系统的串口虚拟化的实现方法,属于软件设计领域,本发明定义虚拟串口设备和串口驱动,创建串口平台设备和平台驱动,在platform driver的probe接口中,完成虚拟串口的注册。在串口驱动的初始化函数中,调用uart_register_driver注册串口驱动。保证传统使用串口应用程序得正常运行。
Description
技术领域
本发明涉及软件设计领域,尤其涉及一种基于linux tty子系统的串口虚拟化的实现方法。
背景技术
随着近年多核系统、集群、网格、云计算的广泛部署,虚拟化技术在商业应用上的优势日益体现,不仅降低了IT成本,而且还增强了系统安全性和可靠性,虚拟化的概念也逐渐深入到人们日常的工作与生活中。虚拟化技术大部分使用网络通信,导致使用串口等特殊接口的传统应用的在实际迁移过程中遇到无法使用物理接口的问题。因此在系统下创建虚拟串口,应用程序操作虚拟串口进行数据收发操作,并将收发的数据通过网络进行转发,实现虚拟中的串口虚拟化。
tty一词源于Teletypes,或Teletypewriters,它是最早出现的一种终端设备,类似电传打字机,由Teletype公司生产。最初tty是指连接到Unix系统上的物理或者虚拟终端。终端是一种字符型设备,通常使用tty来统称各种类型的终端设备。随着时间的推移,当通过串行口能够建立起终端连接后,这个名字也用来指任何的串口设备。它还有多种类,例如串口(ttySn、ttySACn、ttyOn)、USB到串口的转换器(ttyUSBn),还有需要特殊处理才能正常工作的调制解调器(比如传统的WinModem类设备)等。tty虚拟设备支持虚拟控制台,它能通过键盘及网络连接或者通过xterm会话登录到计算机上。为了支持这些tty设备,Linux实现了一个叫做tty的子系统
发明内容
基于以上技术问题,本发明提供了一种基于linux tty子系统的串口虚拟化的实现方法。
本发明的技术方案是:
一种基于linux tty子系统的串口虚拟化的实现方法,定义虚拟串口设备和串口驱动,创建串口平台设备和平台驱动,在platform driver的probe接口中,完成虚拟串口的注册;在串口驱动的初始化函数中,调用uart_register_driver注册串口驱动。
进一步的,
定义虚拟串口设备
在串口驱动中,定义数据结构virtual_uart_port,其中包含uart_port结构体;同时定义rx_enable_flag和tx_enable_flag两个变量用于控制收发使能;定义工作队列用于模拟中断处理函数,完成数据发送操作。
定义串口驱动
定义virtual_uart_driver结构体,MAX_VIRTUAL_UART为控制器驱动支持的串口个数,dev_name为该虚拟串口对应字符设备文件名称的前缀。
创建串口平台设备
定义两个platform device结构体,而传递的参数index为串口的序号;platformdevice的中成员name为“virtual_uart_dev”,根据该名称完成与platform driver的匹配及探测功能。
创建串口平台驱动
定义的platform driver的结构体中支持probe、remove接口;该platform driver的name为“virtual_uart_dev”,通过该名称可进行platform device与platform driver的匹配检测。
probe接口实现的功能如下:
1)为uart_port申请内存,并设置uart_port的ops、fifosize、type、line等值,使用attr_group创建uart port私有的属性文件,这里用该变量该变量创建属性文件uart_receive_buff,用于模拟串口接收数据。
2)初始化工作队列及其回调函数virtual_uart_flush_to_port,该接口主要是模拟串口发送中断的功能,在真实的串口控制器中,则是申请串口中断,在串口中断的处理函数中进行数据的发送,而我们这个工作队列则是模拟串口中断函数的功能;
3)调用uart_add_one_port,将串口设备注册到串口驱动。
定义虚拟串口操作接口
1)tx_empty接口用于测试发送缓存是否为空;
2)stop_tx用于停止发送操作,start_tx用于启动发送操作;
3)stop_rx用于停止接收操作,startup接口主要是控制串口收发使能;
4)set_termios则主要是设置termios相关参数。
模拟串口接收功能
在tty port对应的device中,定义模拟串口接收数据的属性文件,定义如下:当用户向uart_receive_buff中写数据时,在该属性文件的store接口中会将写入的数据发送到tty_port的接收缓存中,并通过调用tty_flip_buffer_push接口,将tty_port接收缓存中的数据通过线路规程的receive_buff接口将数据刷新到tty_struct的接收缓存中,并wakeup读等待队列中sleep的读线程,即将数据发送到串口上的读线程中。
定义串口网络转发功能
定义虚拟串口网络转发函数,串口发送数据时,工作队列不断检测发送缓冲区是否为空,若不为空则通过网络TCP/UDP方式将数据转发出去;
接收到远端网络发送过来的数据后,将数据写入uart_receive_buff属性文件,应用程序可以在打开串口后,读取接收到的数据。
本发明可以创建虚拟串口,保证传统使用串口应用程序得正常运行。
附图说明
图1是本发明的所用的tty子系统内部结构意图;
图2为该虚拟串口读写时数据流向和函数调用图。
具体实施方式
为使本发明实施例的目的、技术方案和优点更加清楚,下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本发明一部分实施例,而不是全部的实施例,基于本发明中的实施例,本领域普通技术人员在没有做出创造性劳动的前提下所获得的所有其他实施例,都属于本发明保护的范围。
本发明的所用的tty子系统内部结构如图1所示,其中串口驱动对接外部的串口设备,线路规程主要对输入输出数据进行处理,以协议转换的方式格式化从用户或硬件设备收到的数据。线路规程把串口驱动和tty驱动结合在一起,策略的分离使得串口驱动和tty驱动不需要关注数据语法处理,tty驱动可以被相同的硬件复用,而只需更改线路规程。tty驱动用来处理各种终端设备。用户空间程序通过tty驱动来读写串口设备。
本发明主要功能包括以下几点:定义虚拟串口设备和串口驱动,创建串口平台设备和平台驱动,在platform driver的probe接口中,完成虚拟串口的注册。在串口驱动的初始化函数中,调用uart_register_driver注册串口驱动。
1、定义虚拟串口设备
在串口驱动中,定义数据结构virtual_uart_port,其中包含uart_port结构体。同时定义rx_enable_flag和tx_enable_flag两个变量用于控制收发使能。定义工作队列用于模拟中断处理函数,完成数据发送操作。
2、定义串口驱动
定义virtual_uart_driver结构体,MAX_VIRTUAL_UART为控制器驱动支持的串口个数,dev_name为该虚拟串口对应字符设备文件名称的前缀。
3、创建串口平台设备
定义两个platform device结构体,而传递的参数index为串口的序号。platformdevice的中成员name为“virtual_uart_dev”,根据该名称可完成与platform driver的匹配及探测功能。
4、创建串口平台驱动
定义的platform driver的结构体中支持probe、remove接口。该platform driver的name为“virtual_uart_dev”,通过该名称可进行platform device与platform driver的匹配检测。
probe接口主要实现的功能如下:
1)为uart_port申请内存,并设置uart_port的ops、fifosize、type、line等值,使用attr_group创建uart port私有的属性文件,这里用该变量该变量创建属性文件uart_receive_buff,用于模拟串口接收数据。
2)初始化工作队列及其回调函数virtual_uart_flush_to_port,该接口主要是模拟串口发送中断的功能,在真实的串口控制器中,则是申请串口中断,在串口中断的处理函数中进行数据的发送,而我们这个工作队列则是模拟串口中断函数的功能;
3)调用uart_add_one_port,将串口设备注册到串口驱动。
5、定义虚拟串口操作接口
1)tx_empty接口用于测试发送缓存是否为空;
2)stop_tx用于停止发送操作,start_tx用于启动发送操作;
3)stop_rx用于停止接收操作,startup接口主要是控制串口收发使能;
4)set_termios则主要是设置termios相关参数(包括字节宽度、波特率等参数的设置)。
6、模拟串口接收功能
在tty port对应的device中,定义模拟串口接收数据的属性文件,定义如下:当用户向uart_receive_buff中写数据时,在该属性文件的store接口中会将写入的数据发送到tty_port的接收缓存中,并通过调用tty_flip_buffer_push接口,将tty_port接收缓存中的数据通过线路规程的receive_buff接口将数据刷新到tty_struct的接收缓存中,并wakeup读等待队列中sleep的读线程,即将数据发送到串口上的读线程中,可以操作打开虚拟串口并读取串口接收到的数据。
/*借助注册至sysfs下的属性文件模拟接收,因此当对属性文件uart_receive_buff进行写操作时会进入该函数的处理中*/
7、定义串口网络转发功能
定义虚拟串口网络转发函数,串口发送数据时,工作队列不断检测发送缓冲区是否为空,若不为空则通过网络TCP/UDP方式将数据转发出去。
接收到远端网络发送过来的数据后,将数据写入uart_receive_buff属性文件,应用程序可以在打开串口后,读取接收到的数据。
以上所述仅为本发明的较佳实施例,仅用于说明本发明的技术方案,并非用于限定本发明的保护范围。凡在本发明的精神和原则之内所做的任何修改、等同替换、改进等,均包含在本发明的保护范围内。
Claims (9)
1.一种基于linux tty子系统的串口虚拟化的实现方法,其特征在于,
定义虚拟串口设备和串口驱动,创建串口平台设备和平台驱动,在platform driver的probe接口中,完成虚拟串口的注册;在串口驱动的初始化函数中,调用uart_register_driver注册串口驱动。
2.根据权利要求1所述的方法,其特征在于,
定义虚拟串口设备
在串口驱动中,定义数据结构virtual_uart_port,其中包含uart_port结构体;同时定义rx_enable_flag和tx_enable_flag两个变量用于控制收发使能;定义工作队列用于模拟中断处理函数,完成数据发送操作。
3.根据权利要求1所述的方法,其特征在于,
定义串口驱动
定义virtual_uart_driver结构体,MAX_VIRTUAL_UART为控制器驱动支持的串口个数,dev_name为该虚拟串口对应字符设备文件名称的前缀。
4.根据权利要求1所述的方法,其特征在于,
创建串口平台设备
定义两个platform device结构体,而传递的参数index为串口的序号;platformdevice的中成员name为“virtual_uart_dev”,根据该名称完成与platform driver的匹配及探测功能。
5.根据权利要求1所述的方法,其特征在于,
创建串口平台驱动
定义的platform driver的结构体中支持probe、remove接口;该platform driver的name为“virtual_uart_dev”,通过该名称可进行platform device与platform driver的匹配检测。
6.根据权利要求5所述的方法,其特征在于,
probe接口实现的功能如下:
1)为uart_port申请内存,并设置uart_port的ops、fifosize、type、line等值,使用attr_group创建uart port私有的属性文件,这里用该变量该变量创建属性文件uart_receive_buff,用于模拟串口接收数据。
2)初始化工作队列及其回调函数virtual_uart_flush_to_port,该接口主要是模拟串口发送中断的功能,在真实的串口控制器中,则是申请串口中断,在串口中断的处理函数中进行数据的发送,而我们这个工作队列则是模拟串口中断函数的功能;
3)调用uart_add_one_port,将串口设备注册到串口驱动。
7.根据权利要求1所述的方法,其特征在于,
定义虚拟串口操作接口
1)tx_empty接口用于测试发送缓存是否为空;
2)stop_tx用于停止发送操作,start_tx用于启动发送操作;
3)stop_rx用于停止接收操作,startup接口主要是控制串口收发使能;
4)set_termios则主要是设置termios相关参数。
8.根据权利要求7所述的方法,其特征在于,
模拟串口接收功能
在tty port对应的device中,定义模拟串口接收数据的属性文件,定义如下:当用户向uart_receive_buff中写数据时,在该属性文件的store接口中会将写入的数据发送到tty_port的接收缓存中,并通过调用tty_flip_buffer_push接口,将tty_port接收缓存中的数据通过线路规程的receive_buff接口将数据刷新到tty_struct的接收缓存中,并wakeup读等待队列中sleep的读线程,即将数据发送到串口上的读线程中。
9.根据权利要求5所述的方法,其特征在于,
定义串口网络转发功能
定义虚拟串口网络转发函数,串口发送数据时,工作队列不断检测发送缓冲区是否为空,若不为空则通过网络TCP/UDP方式将数据转发出去;
接收到远端网络发送过来的数据后,将数据写入uart_receive_buff属性文件,应用程序可以在打开串口后,读取接收到的数据。
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011328809.9A CN112416521A (zh) | 2020-11-24 | 2020-11-24 | 一种基于linux tty子系统的串口虚拟化的实现方法 |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011328809.9A CN112416521A (zh) | 2020-11-24 | 2020-11-24 | 一种基于linux tty子系统的串口虚拟化的实现方法 |
Publications (1)
Publication Number | Publication Date |
---|---|
CN112416521A true CN112416521A (zh) | 2021-02-26 |
Family
ID=74777521
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202011328809.9A Pending CN112416521A (zh) | 2020-11-24 | 2020-11-24 | 一种基于linux tty子系统的串口虚拟化的实现方法 |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112416521A (zh) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114827117A (zh) * | 2022-04-12 | 2022-07-29 | 西安超越申泰信息科技有限公司 | 一种基于串口虚拟化的远程访问实现方法 |
CN117971525A (zh) * | 2024-04-01 | 2024-05-03 | 江苏小牛电子科技有限公司 | 一种串口多路通信方法 |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20100293232A1 (en) * | 2009-05-12 | 2010-11-18 | At&T Intellectual Property I, Lp | Unified Messaging Accessibility Architecture |
CN102546347A (zh) * | 2010-12-17 | 2012-07-04 | 中兴通讯股份有限公司 | 基于proc技术的虚拟串口热插拔的实现方法及系统 |
CN103294509A (zh) * | 2013-05-17 | 2013-09-11 | 深圳市亿威尔信息技术股份有限公司 | 用于VxWorks操作系统的驱动多串口设备的方法 |
-
2020
- 2020-11-24 CN CN202011328809.9A patent/CN112416521A/zh active Pending
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20100293232A1 (en) * | 2009-05-12 | 2010-11-18 | At&T Intellectual Property I, Lp | Unified Messaging Accessibility Architecture |
CN102546347A (zh) * | 2010-12-17 | 2012-07-04 | 中兴通讯股份有限公司 | 基于proc技术的虚拟串口热插拔的实现方法及系统 |
CN103294509A (zh) * | 2013-05-17 | 2013-09-11 | 深圳市亿威尔信息技术股份有限公司 | 用于VxWorks操作系统的驱动多串口设备的方法 |
Non-Patent Citations (1)
Title |
---|
码农家园: "linux虚拟串口控制器驱动开发及代码实现(uart driver)", 《码农家园》 * |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114827117A (zh) * | 2022-04-12 | 2022-07-29 | 西安超越申泰信息科技有限公司 | 一种基于串口虚拟化的远程访问实现方法 |
CN117971525A (zh) * | 2024-04-01 | 2024-05-03 | 江苏小牛电子科技有限公司 | 一种串口多路通信方法 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US6012113A (en) | Method for connecting a plurality of communication applications with an actual communication port by emulating a plurality of virtual modems | |
US6519645B2 (en) | Method and apparatus for providing configuration information using a queued direct input-output device | |
US6347341B1 (en) | Computer program product used for exchange and transfer of data having a siga vector and utilizing a queued direct input-output device | |
CA1310728C (en) | Data conferencing arrangement | |
US6345327B1 (en) | Queuing method and apparatus for providing direct data processing access using a queued direct input-output device | |
WO2022213832A1 (zh) | 计算机设备、虚拟化加速设备、远程控制方法及存储介质 | |
US20050209842A1 (en) | Remote USB port system and method | |
CN204759403U (zh) | 一种基于数据透传实现设备内部模块升级的系统 | |
JPH086880A (ja) | 通信提供方法および装置 | |
CN116886751B (zh) | 一种异构设备的高速通信方法、装置和异构通信系统 | |
CN112416521A (zh) | 一种基于linux tty子系统的串口虚拟化的实现方法 | |
US6397350B1 (en) | Method of providing direct data processing access using a queued direct input-output device | |
US6345241B1 (en) | Method and apparatus for simulation of data in a virtual environment using a queued direct input-output device | |
WO2021120960A1 (zh) | Linux系统控制台的USB串口实现方法及相关产品 | |
US8589954B2 (en) | Method and program for selective suspension of USB network device | |
US6401145B1 (en) | Method of transferring data using an interface element and a queued direct input-output device | |
JP2001519060A (ja) | コントローラレスモデム | |
US6321350B1 (en) | Method and apparatus for error detection using a queued direct Input-Output device | |
WO2012079401A1 (zh) | 一种虚拟串口系统及其通信方法 | |
US6341321B1 (en) | Method and apparatus for providing concurrent patch using a queued direct input-output device | |
US6345324B1 (en) | Apparatus for transferring data using an interface element and a queued direct input-output device | |
US6339803B1 (en) | Computer program product used for exchange and transfer of data having a queuing mechanism and utilizing a queued direct input-output device | |
US9087031B2 (en) | Method and program for selective suspension of USB device | |
US6339801B1 (en) | Method for determining appropriate devices for processing of data requests using a queued direct input/output device by issuing a special command specifying the devices can process data | |
US6345329B1 (en) | Method and apparatus for exchanging data using a queued direct input-output device |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
RJ01 | Rejection of invention patent application after publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20210226 |