2021-07-12-Iot2040和Iot2050 修改串口参数

IOT2040 串口表示#

Linux将所有UARTs表示为ttyS,其中 x = 0, 1 对应内部设备,2, 3对应外部设备

修改指令#

IOT2040

1
2
3
4
5
6
7
8
9
10
指令:switchserialmode

Usage: switchserialmode DEVICE [MODE [-t|--terminate]]

DEVICE The device for which you want to switch the mode.
MODE The mode you want to use: rs232, rs485, or rs422.
If omitted, the current mode will be printed.

Optional arguments:
-t, --terminate Terminate the RS422 or RS485 bus.

例子:switchserialmode /dev/ttyS2 RS485 –terminate

IOT2050

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
指令:switchserialmode
It's used to set external serial port mode.
Usage:
switchserialmode [ttyuart [options]] | [cp210x [options]] | [-m,--mode MODE]

Example:
switchserialmode ttyuart -h
switchserialmode cp210x -h
switchserialmode -m,--mode <rs232 | rs485 | rs422> [-t,--terminate]
-t,--terminate: Terminate the rs422 or rs485 bus.

指令:switchserialmode ttyuart -h

It's to operate tty serial device.
-h,--help: display help information.
-D,--device: specified device, like '/dev/ttyS1' etc.
-m,--mode mode: set serial work mode, the mode can be set 'rs232' or 'rs485' or 'rs422'.
-l,--logic level: set RTS-pin logic level when sending in rs485 mode, logic can be set '0' or '1'.
-d,--display: display the current mode of ttyuart

例子:

  1. 查看:switchserialmode ttyuart -D /dev/ttyS2 -d
  2. 设置:switchserialmode ttyuart -D /dev/ttyS2 -m RS485 -t

设置开机启动#

IOT2040#

  1. 创建文件:vi /etc/init.d/changetty.sh
  2. chmod 755 /etc/init.d/changetty.sh
  3. 修改文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /bin/sh
# /etc/init.d/changetty.sh

### BEGIN INIT INFO
# Provides: changetty
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: changetty
### END INIT INFO

echo '/etc/init.d/changetty.sh'
switchserialmode /dev/ttyS2 RS485 --terminate
exit 0


  1. 将启动脚本链接到 /ect/rcX.d
1
2
3
4
ln -s /etc/init.d/changetty.sh  /etc/rc2.d/S20changetty
ln -s /etc/init.d/changetty.sh /etc/rc3.d/S20changetty
ln -s /etc/init.d/changetty.sh /etc/rc4.d/S20changetty
ln -s /etc/init.d/changetty.sh /etc/rc5.d/S20changetty
  1. 执行并替换文件
1
2
3
4
5
6
switchserialmode /dev/ttyS2 RS485 --terminate
switchserialmode /dev/ttyS2 RS422 --terminate
switchserialmode /dev/ttyS2 RS232

sed -i s/.*switchserialmode.*/'switchserialmode \/dev\/ttyS2 RS485 --terminate'/ /etc/init.d/changetty.sh

IOT2050#

  1. 在/root 目录中创建 changetty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#! /usr/bin/python3

import sys
import subprocess
import json
from collections import OrderedDict


class Process:
def __init__(self):
self.configureFile = '/etc/board-configuration.json'
self.config = self.getConfig()

def getConfig(self):
with open(self.configureFile, 'r') as f:
config = json.load(f, object_pairs_hook=OrderedDict)
return config

def saveConfig(self, jsonSrc):
with open(self.configureFile, 'w') as f:
json.dump(jsonSrc, f, indent=4, separators=(',', ': '))

def setBasicBoard(self, mode, terminateStatus):
command = 'switchserialmode ttyuart -D /dev/ttyS2 -m ' + mode
subprocess.call(command, shell=True)
self.config['User_configuration']['External_Serial_Init_Mode'] = mode
if terminateStatus == 'on' or terminateStatus == 'off':
self.config['User_configuration']['External_Serial_Terminate'] = terminateStatus
self.saveConfig(self.config)


def main(argv):
try:
process = Process()

print(argv[1]) # mode
print(argv[2]) # terminateStatus
process.setBasicBoard(argv[1], argv[2])
except:
pass
finally:
print('end world')
return ''


if __name__ == '__main__':
main(sys.argv)
  1. 在NodeRed 中调用脚本
1
2
3
4
python changetty.py RS485 'on'
python changetty.py RS422 'on'
python changetty.py RS232 'on'

作者

lxmuyu

发布于

2022-02-17

更新于

2022-02-17

许可协议