Bash:从后台进程接收消息

Bash:从后台进程接收消息

cansend我正在通过发送数据vcan0,同时我正在收听vcan0使用该candump vcan0命令发送到 的消息

由于某种原因,当我通过脚本发送数据时,我不会收到任何消息,vcan0但是当我通过终端发送数据时,脚本会以某种方式接收数据。

cansend vcan0 004#0152FEE400000000 # trying to send data (doesnt work)
while true;
do
  msg_candump=$(candump vcan0) # read vcan0
  if [[ ${#msg_candump} > 1 ]]; then #received msg
    echo $msg_candump
  fi
done

答案1

您使用哈希字符#作为要发送的数据,但此字符保留用于在 shell 中进行注释

尝试一下:

( # Let run listener in background before trying to send to CAN
  while true;
  do
    msg_candump="$(candump vcan0 -T 1000)" # read CAN for 1 second
    if [[ ${#msg_candump} > 1 ]]; then #received msg
      echo "$msg_candump"
    fi
    sleep 0.025 # Give system a little breath
  done
) &
PID=$!

cansend vcan0 '004#0152FEE400000000' # <- quotes are matter 
sleep 5
kill -9 $PID

相关内容