Linux Shell,如何通过串口发送命令并返回?

时间:2015-06-08 16:22:34

标签: linux shell serial-port

我希望发送重启'通过串口命令到PDU。我在交互模式中需要做的是:

#screen /dev/ttyS1
>reboot
>[Detach Screen]
#

如果我想在脚本中自动执行此任务,我应该可以使用shell中的一个命令重新启动PDU,如下所示:

#echo "reboot" >/dev/ttyS1

然而,它不起作用!我不知道为什么......你能帮助我吗?

PDU手动请求波特率为9600,这不是默认的波特率。我尝试了以下命令来设置波特率,但仍然没有锁定:

stty -F /dev/ttyS1 speed 9600 cs8 -cstopb -parenb 

这些输出在有或没有屏幕的情况下都不会改变:

 # stty -a -F /dev/ttyS1
    speed 9600 baud; rows 0; columns 0; line = 0;
    intr = ^C; quit = ^\; erase = ^?; kill = ^H; eof = ^D; eol = <undef>;
    eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z;     rprnt = ^R;
    werase = ^W; lnext = ^V; flush = ^O; min = 100; time = 2;
    -parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
    -ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon    -ixoff
    -iuclc -ixany -imaxbel -iutf8
    -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
    -isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
    echoctl echoke

1 个答案:

答案 0 :(得分:3)

以下是我们解决此问题的方法:

  1. 使用RAW设置配置串口:

    stty -F /dev/ttyS1 speed 9600 cs8 -cstopb -parenb raw
    
  2. 使用带有\ r:

    的echo发送命令
    echo -ne "reboot\r" > /dev/ttyS1
    
  3. 我认为以上可能包括拼写错误。如果上述方法无效,请尝试:

     echo -ne "reboot\n\r" > /dev/ttyS1
    
相关问题