Skip to content

can#

#mod
modprobe can
modprobe can_raw
modprobe mttcan

#install
sudo apt install can-utils

#查到当前can网络 can0 can1,包括收发包数量、是否有错误等等
ifconfig -a 

#关闭can设备;或使用ifconfig canX down
ip link set canX down 


#设置can
ip link set canX up type can bitrate 250000 restart-ms 500 [ loopback on ]

ip link set can0 up type can bitrate 1000000

# listen-only on: Puts the CAN controller into listen-only mode.
# loopback on: Enables loopback mode for testing (data transmitted is directly received).
# restart-ms TIME: Sets the restart-ms parameter for error recovery.
# bitrate BITRATE: Sets the bitrate for the CAN interface in bits per second (bps).

#开启can设备
ip link set canX up 

#显示can设备详细信息;
ip -details link show canX 


# candump
candump canX //接收can总线发来的数据;
candump canX,123:7FF (matches CAN ID 123 - including EFF and RTR frames)
candump canX,005:00f (matches CAN ID *5 - including EFF and RTR frames)
candump canX,123:7FF,111:7FF (matches CAN ID 123 and 111 - including EFF and RTR frames)
candump canX,123:C00007FF (matches CAN ID 123 - only SFF and non-RTR frames)


# cansend

cansend can0 006#11
cansend can0 006#R11 # 远程帧

#  <can_id>#{R|data}          for CAN 2.0 frames
#  <can_id>##<flags>{data}    for CAN FD frames

add vcan#

#!/bin/bash
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0

usart#

minicom 在 Linux 中使用 minicom 进行十六进制(hex)显示和发送数据,可以通过以下步骤实现:

1. 安装 minicom#

如果系统中没有安装 minicom,可以通过包管理器安装:

  • Debian/Ubuntu:
sudo apt-get install minicom
  • CentOS/RHEL:
sudo yum install minicom
  • Fedora:
sudo dnf install minicom

2. 配置 minicom#

启动 minicom 并进行配置:

minicom -s

在配置菜单中,选择 Serial port setup,然后配置串口设备、波特率、数据位、停止位、校验位等参数。配置完成后,保存并退出。

3. 启用十六进制显示#

  1. 启动 minicom
minicom
  1. 按下 Ctrl + A,然后按 O(字母 O,不是数字 0)进入配置菜单。

  2. 选择 Screen and keyboard(屏幕和键盘设置)。

  3. 找到 Display hex 选项,将其设置为 Yes

  4. 保存设置并退出配置菜单。

  5. 现在,接收到的数据将以十六进制格式显示,启用 Line wrap 或 Add Linefeed 选项,确保数据按行显示。。


关闭硬件流控制#

有的串口驱动不关闭硬件流控制,无法发出数据

  • 按下 Ctrl + A,然后按 O
  • Serial port setup -> Hardware Flow Control 设置为 NO;

minicom 中发送十六进制数据#

使用 echo 或 printf 命令将十六进制数据发送到串口设备。例如:

echo -n -e '\x01\x02\x03\x04' > /dev/ttyS0

# Install Minicom 
sudo apt install minicom

# Start Minicom:
sudo minicom -b BAUDRATE -D /dev/ttyPORT

# or use stty

# 串口 ttyS0 波特率为115200,8位数据位,1位停止位,偶数校验位
stty -F /dev/ttyS0 speed 115200 cs8 -cstopb parenb -parodd

# parenb:使终端进行奇偶校验,-parenb则是禁止校验;
# -parodd 偶数校验
#  parodd 奇数校验 


# send
echo "Hello, world!" | sudo tee /dev/ttyPORT

# recv
cat /dev/ttyPORT

# recv and show in hex format
cat /dev/ttyPORT | hexdump -C