Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

unix - how to deal with the serial port in android mobile?

actually I know no thing about serial port. but I see an example of sending it an at-command:

echo -e "AT" > /dev/smd0

What is /dev/smd0 and why they sometimes replace it with /dev/ttyS0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

/dev/smd0 and /dev/ttyS0 are device files. Such files are virtual files that provide a file I/O operation interface for working with some underlying thing like for instance hardware resources like serial ports, hard disks and memory, or with non-hardware resources like process information, random number input, terminal screen output, etc.

Device files comes in two flavours, character and block. Serial ports are character devices, you can verify with c being the first character in output from ls -l:

$ ls -l /dev/ttyS0
crw-rw----. 1 root dialout 4, 64 Apr  7 00:25 /dev/ttyS0
$

/dev/ttyS0 is the device name used for serial ports on on linux desktop computers, corresponding to COM1 in DOS/Windows (in the very, very early days of linux /dev/cua was used, you might occasionally encounter references to that). For virtual USB serial interfaces to mobile phones /dev/ttyACM0 and /dev/ttyACM1 are used. Some other devices use /dev/ttyUSB0. For Android there are a few different device file names in use where /dev/smd0 is one of them. Your phone might use another one, so you have to check what you should use specifically for your phone.


The command echo -e "AT" > /dev/smd0 does not make sense. The -e option enables interpretation of backslash-escaped characters, but since the following string contains no such characters it is equivalent to just echo "AT" > /dev/smd0.

However, when sending AT commands to a modem, the command line should be terminated with only and nothing else. This is mandated by V.250.

So the proper command to send the command "AT" to the modem should be

echo -n -e "AT
" > /dev/smd0

But even when getting as far as sending the AT command correctly to the modem, you must read back the responses from the modem. Closing and (re-)opening the device file several times while doing this (which you will do by running a sequence of shell commands) is not a good way to go about, so I would recommend that you use my program atinout which is specifically written to be used for command line AT command communication:

$ echo AT | atinout - /dev/smd0 -
AT
OK
$

or

$ echo AT > input.txt
$ atinout input.txt /dev/smd0 output.txt
$ cat output.txt
AT
OK
$

This way you will get all modem communication performed correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...