简体   繁体   English

嵌入式linux,如何在按下按钮时切换使用串行端口?

[英]embedded linux, how to switch the use of the serial port at the push of a button?

I am a business programmer with a few years of Linux administration experience. 我是具有几年Linux管理经验的业务程序员。 I'm starting out in embedded Linux. 我开始使用嵌入式Linux。 Yesterday, we were discussing a new device design and I was asked a few questions I had no answers for. 昨天,我们正在讨论一种新的设备设计,有人问我一些我没有答案的问题。

The engineers want to have some push buttons on an electronic board with a serial port on it. 工程师希望在带有串行端口的电子板上有一些按钮。 The OS is Linux. 操作系统是Linux。

Normally, when a user will connect to the serial port, a protocol will answer him instead of a Linux login prompt. 通常,当用户连接到串行端口时,协议会回答他,而不是Linux登录提示。 However, if he pushes a sequence of buttons on the device, a Linux prompt will answer him on the serial port instead. 但是,如果他按设备上的一系列按钮,则Linux提示符将在串行端口上回答他。

The Linux driver to handle the push button interrupt handling aside, how can you switch the basic use of the serial port like that? 除了用于处理按钮中断处理的Linux驱动程序之外,您如何才能像这样切换串行端口的基本用途? Does anyone have a URL reference on how to do this? 有没有人提供有关如何执行此操作的URL参考? (preferably with some sample code) (最好带有一些示例代码)

Note: I proposed providing a nice menu on login for a given user, but no can do. 注意:我建议为给定的用户提供一个不错的登录菜单,但是没有办法。

Thanks in advice for any suggestions. 感谢您的建议。

Best regards, Bert 最好的问候,伯特

The main problem is that the process that implements your device protocol is probably keeping the serial port open. 主要问题在于,实现设备协议的过程可能会使串行端口保持打开状态。

In this case you should probably: 在这种情况下,您应该:

  1. Wait for the button event 等待按钮事件

  2. Have the protocol process close the serial port - terminating that process completely might also do for you 让协议进程关闭串行端口-完全终止该进程也可能对您有用

  3. Launch a *getty process - or whatever your embedded target uses to present a login prompt on the serial port 启动*getty进程-或嵌入式目标用来在串行端口上显示登录提示的任何对象

  4. Restore the protocol process once you are done 完成后还原协议过程

EDIT: 编辑:

In the steps above I assume the more common case where the process that controls the serial port (eg pppd ) is not able to act as a getty substitute to provide a login prompt. 在上述步骤中,我假定了更常见的情况,即控制串行端口(例如pppd )的进程无法充当getty替代品来提供登录提示。 It is also typically not the same process that provides telnet/SSH/whatever logins. 它通常也不是提供telnet / SSH /任何登录名的同一进程。

That said, it's quite possible on a customised embedded Linux system for a process to do more than one thing. 就是说,在定制的嵌入式Linux系统上,一个进程可以做的事情不止一件事情。 In that case you have to configure or modify that process to switch operational modes when appropriate. 在那种情况下,您必须配置或修改该过程以在适当时切换操作模式。

Without more information about your embedded target it's impossible to provide a more specific answer. 没有有关嵌入式目标的更多信息,就不可能提供更具体的答案。

The getty process is usually started on serial ports to provide a login prompt by /sbin/init , which is configured in /etc/inittab . 通常在串行端口上启动getty进程,以通过/sbin/init提供登录提示,该提示在/etc/inittab配置。

init has the concept of "runlevels". init具有“运行级别”的概念。 Each runlevel defines a separate set of processes that init will keep running. 每个运行级别定义了单独的一套程序init将继续运行。 One elegant way to implement this would be to design your "protocol" process so that it is started by init , in the same way as getty . 实现此目的的一种优雅方法是设计您的“协议”过程,使其以与getty相同的方式由init启动。 You can then tell init to run your process in some runlevels, and getty in others, and have the pushbutton switch between runlevels. 然后,您可以告诉init在某些运行级别运行您的进程,在其他运行级别运行getty ,并在运行级别之间进行按钮切换。 For example, your /etc/inittab might include: 例如,您的/etc/inittab可能包括:

T0:2:respawn:/sbin/getty -L ttyS0 9600 vt100
P0:345:respawn:/sbin/protocol ttyS0 9600

This will run your protocol command on the first serial port in runlevels 3, 4 and 5; 这将在运行级别3、4和5的第一个串行端口上运行您的protocol命令。 but getty in runlevel 2. 但在运行级别2中却很getty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM