EM7455 and atinout

I have a Xiaomi Mi Router 3G with OpenWrt 22.03.5 firmware and an EM7455 modem. I also installed packages to support QMI protocol and packages to send at-commands.
When I send commands via atinout, they are often sent incorrectly and the modem responds with "ERROR". How can I fix this problem?
I have written scripts to make the modem work, but they don't work because of the frequent sending of incorrect commands. On another router I used ModemManager interface type and at-commands were always correct.

When the connection is lost

#!/bin/sh
modemPort=$(ls /dev/ttyUSB* | tail -n 1)
echo "$modemPort" | grep -E -o -q -i "ttyUSB" || exit 0

ifdown EM7455

status=$(echo "at!gstatus?" | atinout - $modemPort -)
hexGoodBS="08199120"

if echo "$status" | grep -E -o -q -i "Normal Service"; then
	if ! echo "$status" | grep -E -o -q -i "Cell ID:\s*$hexGoodBS\s*\(\d+\)"; then
		echo "at!band=09" | atinout - $modemPort -
	fi
elif echo "$status" | grep -E -o -q -i "Attach Needed"; then
	if echo "at!band?" | atinout - $modemPort - | grep -E -o -q -i "Band_3"; then
		if echo "$status" | grep -E -o -q -i "Cell ID:\s*$hexGoodBS\s*\(\d+\)"; then
			echo "at!band=09" | atinout - $modemPort -
			sleep 3
			echo "at!band=03" | atinout - $modemPort -
		else
			echo "at!band=09" | atinout - $modemPort -
		fi
	fi
elif echo "$status" | grep -E -o -q -i "(No (Cell|band)|PLMN Search)"; then
	if ! echo "at!band?" | atinout - $modemPort - | grep -E -o -q -i "LTE ALL"; then
		echo "at!band=09" | atinout - $modemPort - #LTE ALL
	fi
fi

ifup EM7455

Every night at 3:00 AM

#!/bin/sh
modemPort=$(ls /dev/ttyUSB* | tail -n 1)
echo "$modemPort" | grep -E -o -q -i "ttyUSB" || exit 0

ifdown EM7455

if ! echo "at!band?" | atinout - $modemPort - | grep -E -o -q -i "Band_3"; then
	echo "at!band=03" | atinout - $modemPort -
	sleep 5
	status=$(echo "at!gstatus?" | atinout - $modemPort -)
	hexGoodBS="08199120"
	if ! echo "$status" | grep -E -o -q -i "Cell ID:\s*$hexGoodBS\s*\(\d+\)"; then
		echo "at!band=09" | atinout - $modemPort -
	fi
fi

ifup EM7455

28779084
28779085
28779193

Do you have any other program installed that is using the same ttyUSBx port?
You can try using sms_tool to send AT commands instead of atinout and also you can obtain some information from the modem using QMI instead of AT command.
As an example, you can get Cell Id and Band information by running uqmi -d /dev/cdc-wdm0 --get-cell-location-info

I guess not. Here is the list of packages:

atinout
base-files
busybox
ca-bundle
cgi-io
dnsmasq
dropbear
firewall4
fstools
fwtool
getrandom
hostapd-common
htop
iptables-mod-ipopt
iptables-zz-legacy
iw
iwinfo
jansson4
jshn
jsonfilter
kernel
kmod-cfg80211
kmod-crypto-aead
kmod-crypto-ccm
kmod-crypto-cmac
kmod-crypto-crc32c
kmod-crypto-ctr
kmod-crypto-gcm
kmod-crypto-gf128
kmod-crypto-ghash
kmod-crypto-hash
kmod-crypto-hmac
kmod-crypto-kpp
kmod-crypto-lib-chacha20
kmod-crypto-lib-chacha20poly1305
kmod-crypto-lib-curve25519
kmod-crypto-lib-poly1305
kmod-crypto-manager
kmod-crypto-null
kmod-crypto-rng
kmod-crypto-seqiv
kmod-crypto-sha256
kmod-gpio-button-hotplug
kmod-ifb
kmod-ipt-core
kmod-ipt-ipopt
kmod-leds-gpio
kmod-lib-crc-ccitt
kmod-lib-crc32c
kmod-mac80211
kmod-mii
kmod-mt76-core
kmod-mt7603
kmod-mt76x02-common
kmod-mt76x2
kmod-mt76x2-common
kmod-nf-conntrack
kmod-nf-conntrack6
kmod-nf-flow
kmod-nf-ipt
kmod-nf-log
kmod-nf-log6
kmod-nf-nat
kmod-nf-reject
kmod-nf-reject6
kmod-nfnetlink
kmod-nft-core
kmod-nft-fib
kmod-nft-nat
kmod-nft-offload
kmod-nls-base
kmod-ppp
kmod-pppoe
kmod-pppox
kmod-sched-cake
kmod-sched-core
kmod-slhc
kmod-udptunnel4
kmod-udptunnel6
kmod-usb-core
kmod-usb-ledtrig-usbport
kmod-usb-net
kmod-usb-net-cdc-ether
kmod-usb-net-qmi-wwan
kmod-usb-net-rndis
kmod-usb-serial
kmod-usb-serial-option
kmod-usb-serial-qualcomm
kmod-usb-serial-wwan
kmod-usb-wdm
kmod-usb-xhci-hcd
kmod-usb-xhci-mtk
kmod-usb3
kmod-wireguard
libblobmsg-json20220515
libc
libgcc1
libip4tc2
libip6tc2
libiptext0
libiptext6-0
libiwinfo-data
libiwinfo-lua
libiwinfo20210430
libjson-c5
libjson-script20220515
liblua5.1.5
liblucihttp-lua
liblucihttp0
libmnl0
libncurses6
libnftnl11
libnl-tiny1
libpthread
librt
libubox20220515
libubus-lua
libubus20220601
libuci-lua
libuci20130104
libuclient20201210
libucode20220812
libusb-1.0-0
libustream-wolfssl20201210
libwolfssl5.5.4.ee39414e
libxtables12
logd
lua
luci
luci-app-atinout
luci-app-firewall
luci-app-opkg
luci-app-pingcontrol
luci-app-sqm
luci-app-watchcat
luci-app-wireguard
luci-base
luci-compat
luci-i18n-pingcontrol-en
luci-i18n-sqm-en
luci-i18n-watchcat-en
luci-i18n-wireguard-en
luci-lib-base
luci-lib-ip
luci-lib-jsonc
luci-lib-nixio
luci-mod-admin-full
luci-mod-network
luci-mod-status
luci-mod-system
luci-proto-ipv6
luci-proto-ppp
luci-proto-qmi
luci-proto-wireguard
luci-ssl
luci-theme-bootstrap
mtd
nano
netifd
nftables-json
odhcp6c
odhcpd-ipv6only
openwrt-keyring
opkg
pingcontrol
ppp
ppp-mod-pppoe
procd
procd-seccomp
procd-ujail
px5g-wolfssl
rpcd
rpcd-mod-file
rpcd-mod-iwinfo
rpcd-mod-luci
rpcd-mod-rrdns
sqm-scripts
tc-tiny
terminfo
ubi-utils
uboot-envtools
ubox
ubus
ubusd
uci
uclient-fetch
ucode
ucode-mod-fs
ucode-mod-ubus
ucode-mod-uci
uhttpd
uhttpd-mod-ubus
uqmi
urandom-seed
urngd
usb-modeswitch
usign
watchcat
wireguard-tools
wireless-regdb
wpad-basic-wolfssl
wwan
xtables-legacy

The official repository only has the 'smstools3' package. Or do I need another package?
But I would like to know why 'atinout' doesn't work.

If sms-tool package is not available to you, you can try comgt, this is what OpenWrt uses to send AT commands to NCM modems. I do not have experience with atinout and cannot comment.

After I connected via picocom to /dev/ttyUSB2, all commands went through both picocom and atinout normally. But when I disconnect from picocom, atinout sends mostly nonsense again.

root@Xiaomi-Mi-Router-3G:~# echo at!gstatus? | atinout - /dev/ttyUSB2 -
at!b


ERROR
root@Xiaomi-Mi-Router-3G:~# echo at!gstatus? | atinout - /dev/ttyUSB2 -


ERROR
root@Xiaomi-Mi-Router-3G:~# echo at!gstatus? | atinout - /dev/ttyUSB2 -
at!gstatus?


!GSTATUS:

Current Time:  26655            Temperature: 33

Reset Counter: 1                Mode:        ONLINE

System mode:   LTE              PS state:    Not attached

LTE band:      No band          LTE bw:      1.4 MHz

LTE Rx chan:   0                LTE Tx chan: 4294967295

LTE CA state:  NOT ASSIGNED

EMM state:     Deregistered     Attach Needed

RRC state:     RRC Idle

IMS reg state: No Srv



PCC RxM RSSI:  0                RSRP (dBm):  0

PCC RxD RSSI:  0                RSRP (dBm):  0

Tx Power:      --               TAC:         FFFF (65535)

RSRQ (dB):     0.0              Cell ID:     FFFFFFFF (4294967295)

SINR (dB):





OK
root@Xiaomi-Mi-Router-3G:~# echo at!gstatus? | atinout - /dev/ttyUSB2 -


ERROR
root@Xiaomi-Mi-Router-3G:~# echo at!gstatus? | atinout - /dev/ttyUSB2 -
at!gren E cttacIrFat!gstatus?


ERROR
root@Xiaomi-Mi-Router-3G:~#

echo + cat don't work correctly either. Something very strange is going on...

root@Xiaomi-Mi-Router-3G:~# echo "at!gstatus?" > /dev/ttyUSB2
root@Xiaomi-Mi-Router-3G:~# cat /dev/ttyUSB2
at!gstatus?


!GSTATUS:

Current Time:  159971           Temperature: 40

Reset Counter: 1                Mode:        ONLINE

System mode:   LTE              PS state:    Attached

LTE band:      B3               LTE bw:      20 MHz

LTE Rx chan:   1700             LTE Tx chan: 19700

LTE CA state:  NOT ASSIGNED

EMM state:     Registered       Normal Service

RRC state:     RRC Connected

IMS reg state: No Srv



PCC RxM RSSI:  -79              RSRP (dBm):  -104

PCC RxD RSSI:  -79              RSRP (dBm):  -105

Tx Power:      17               TAC:         2DF3 (11763)

RSRQ (dB):     -7.6             Cell ID:     08199120 (135893280)

SINR (dB):     13.2





OK

at!g


ERROR

^C
root@Xiaomi-Mi-Router-3G:~# echo "at!gstatus?" > /dev/ttyUSB2
root@Xiaomi-Mi-Router-3G:~# cat /dev/ttyUSB2
at!gat!gstatus?


ERROR

Solution: execute the "stty -F /dev/ttyUSB2 raw -echo" command before sending at-commands.