串口自动登录实现程序自启动

本文实现通过串口自动登录Ubuntu系统,并自动运行程序

1. 自动登录

1. 写脚本autologin

#!/bin/bash
/bin/login -f username #你的用户名

保存到/usr/bin/下,并且用`chmod +x autologin`设置可执行权限

2. 配置systemd服务
修改系统中/lib/systemd/system/serial-getty\@.service 文件内容,修改ExecStart项,实现自动启动有两种方式:

  • ExecStart=-/sbin/agetty -a root 115200 %I $TERM 其中root可以替换为任何用户,这种方式实现自动登录。
  • ExecStart=-/sbin/agetty  -n -l /usr/bin/autologin 115200 %I $TERM` 这种方式通过执行autologin脚本来实现登录。
$ sudo vim /lib/systemd/system/serial-getty\@.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Serial Getty on %I
Documentation=man:agetty(8) man:systemd-getty-generator(8)
Documentation=http://0pointer.de/blog/projects/serial-console.html
BindsTo=dev-%i.device
After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service getty-pre.target
After=rc-local.service

# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
Before=getty.target
IgnoreOnIsolate=yes

# IgnoreOnIsolate causes issues with sulogin, if someone isolates
# rescue.target or starts rescue.service from multi-user.target or
# graphical.target.
Conflicts=rescue.service
Before=rescue.service

[Service]
# The '-o' option value tells agetty to replace 'login' arguments with an
# option to preserve environment (-p), followed by '--' for safety, and then
# the entered username.
#ExecStart=-/sbin/agetty -o '-p -- \\u' --keep-baud 115200,38400,9600 %I $TERM
#ExecStart=-/sbin/agetty -a root 115200 %I $TERM
ExecStart=-/sbin/agetty  -n -l /usr/bin/autologin 115200 %I $TERM
Type=idle
Restart=always
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes

[Install]
WantedBy=getty.target

配置服务器自动启动

$ sudo systemctl enable getty@tty1.service
Created symlink /etc/systemd/system/getty.target.wants/getty@tty1.service → /lib/systemd/system/getty@.service.

重启验证服务

Last login: Fri Aug 13 00:33:41 UTC 2021 from 192.168.31.150 on pts/2
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.19.95+ aarch64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation
HwHiAiUser@davinci-mini:~$

2. 程序自启动


通过在登录用户home目录下的.profile脚本添加程序启动命令来启动程序。

修改.profile,在文件尾部添加以下内容

 #注意此处加判断是防止程序重复启动,由于.profile中的内容,在每次登录都会执行,如果用户通过ssh等其他方式登录会导致程序重复启动。
    # $DISPLAY是可选项,如果是GUI的程序建议加上
    if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ]; then
         #此处添加你的启动程序的命令
         echo "run your application here!"
    fi

3. 重启,确实是不是自动登陆并执行了?

Last login: Fri Aug 13 00:33:41 UTC 2021 from 192.168.31.150 on pts/2
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.19.95+ aarch64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation
run your application here!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的Qt串口短接的自动收发检测的代码实现,供您参考: ```cpp #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> #include <QTimer> // 创建一个定时器对象 QTimer *timer = new QTimer(this); // 创建一个串口对象 QSerialPort *serial = new QSerialPort(this); // 定义定时器的时间间隔 const int interval = 1000; // 定义用于发送和接收数据的字符串 const QString sendStr = "test"; QString receiveStr; // 定义用于判断是否收到正确数据的标志位 bool receiveFlag = false; // 初始化串口 void initSerial() { // 获取可用的串口列表 QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts(); // 遍历串口列表,找到一个可用的串口 foreach (QSerialPortInfo info, infos) { if (info.description().contains("USB")) { // 判断串口描述中是否包含"USB" serial->setPort(info); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); if (serial->open(QIODevice::ReadWrite)) { qDebug() << "Serial port opened successfully!"; break; } } } } // 定时器的timeout信号的槽函数,用于发送和接收数据 void timerTimeout() { // 向串口发送数据 serial->write(sendStr.toUtf8()); // 等待一段时间,等待串口返回数据 if (serial->waitForReadyRead(interval)) { // 读取串口返回的数据 receiveStr = serial->readAll(); // 判断收到的数据是否正确 if (receiveStr == sendStr) { qDebug() << "Serial port communication is normal!"; receiveFlag = true; } } // 如果在规定的时间内未能收到正确数据,则说明串口通信存在问题 if (!receiveFlag) { qDebug() << "Serial port communication is abnormal!"; } // 清空标志位和接收字符串 receiveFlag = false; receiveStr.clear(); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 初始化串口对象 initSerial(); // 连接定时器的timeout信号和槽函数 QObject::connect(timer, &QTimer::timeout, timerTimeout); // 启动定时器 timer->start(interval); return a.exec(); } ``` 在这个例子中,我们创建了一个定时器对象和一个串口对象,并且设置了定时器的时间间隔、发送和接收数据的字符串、判断是否收到正确数据的标志位等变量。在程序执行时,我们先初始化串口对象,然后连接定时器的timeout信号和槽函数,最后启动定时器。在定时器的timeout信号的槽函数中,我们向串口发送数据,然后等待一段时间,等待串口返回数据。如果在规定的时间内收到了正确的数据,则说明串口通信正常,否则说明串口通信存在问题。 需要注意的是,这个例子中使用了Qt的信号和槽机制来实现定时器的timeout信号和槽函数的连接。同时,在进行串口通信时,也需要考虑波特率、数据位、停止位等参数的设置,以确保通信的正确性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值