Linux serial port sniffer

How can I monitor serial port traffic?

socat is a tool to connect (nearly) everything to (nearly) everything, and tee can duplicate streams.
In your usecase you could connect your serial port /dev/ttyS0 to a PTY /tmp/ttyV0 , then point your application to the PTY, and have socat tee out Input and Output somewhere for you to observe.

Googling «socat serial port pty tee debug» will point you to several «standard procedure» examples, one being:

socat /dev/ttyS0,raw,echo=0 \ SYSTEM:'tee in.txt |socat - "PTY,link=/tmp/ttyV0,raw,echo=0,waitslave" |tee out.txt' 

The files in.txt and out.txt will then contain the captured data.

  • The socat syntax looks confusing at first, but in fact, it’s just 2 nested statements.
    A small price to pay to such a powerful, versatile tool.
  • If you need to setup your serial port, or send other ioctls, do so before calling socat, as socat cannot proxy them.
  • The single-purpose-tool interceptty from 2006 has slightly simpler syntax, but can only intercept TTYs (while proxying ioctls), and is probably not in your package manager. (Most Linux distros never added it to their repos)

I don’t think the serial driver has any tracing functionality that would allow you to watch packets. You can use strace to observe all the reads and writes from your application:

strace -s9999 -o myapp.strace -eread,write,ioctl ./myapp 

I found projects called Linux Serial Sniffer, jpnevulator, and Moni. The first two look like they do exactly what you want. The last one calls itself a monitor, but it actually looks like a standard serial communication program.

Источник

slsnif(1) — Linux man page

slsnif is a serial line sniffer software. It listens to the specified serial port and writes all data coming through it into either stdout (default) or a log file (if specified — see option -l).

slsnif operates either by creating a pseudo tty (pty) and linking it to the serial port, or by linking two serial ports together. In order to set up a debug session one has either to start slsnif on a given port first, note the pty it had opened (also available from file /tmp/slsnif_pty while slsnif is running), then configure the controlling software for the device being debugged to use this pty instead of the actual port, or, in case of two ports being used, simply start slsnif on two ports (see option -p) and watch the show 🙂

Читайте также:  Latest nvidia driver linux mint

Note 1 : Slsnif will try to match port settings on pty and port (or on 1st and 2nd ports) as soon as it detects first chunk of data coming from the «host side» (which is either pty or 2nd port). If it fails (in case of a braindead controlling software that doesn’t set the baudrate correctly), one has an ability to set baudrate by hand via option -s. Controlling software and slsnif should be set up to use the same baudrate, which in turn should match the baudrate used by the device connected to the port.

Note 2 : If controlling software is running under vmware (www.vmware.com) and pty is used, port in question should be disconnected from vmware before invoking slsnif, and connected to the opened pty after slsnif has been started.

Parameters

slsnif accepts the following parameters: (required) First (and only one in case of pty being used) serial port to listen to. (/dev/ttyS0, /dev/ttyS1, etc.)

Options

[-l ] ([—log ]) File to direct output to. Output is sent to stdout by default. [-i ] ([—in-tee ]) File to dump raw data from device to. Multiple ‘-i’ / ‘—in-tee’ arguments result in multiple copies of data in corresponding files. [-o ] ([—out-tee ]) File to dump raw data from host to. Multiple ‘-o’ / ‘—out-tee’ arguments result in multiple copies of data in corresponding files. [-s ] ([—speed ]) Baudrate to use. Valid options are:
50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200.
Defaults to 9600 baud. [-b] ([—bytes]) Print number of bytes transmitted on every read. [-p ] ([—port2 ]) Use real serial port instead of pty. Useful when one needs to log data going between two ports. [-n] ([—nolock]) Disable locking of the port. Normally not a good idea, but can be very useful if one doesn’t have permissions to write to lock directory (usually /var/lock/) [-t] ([—timestamp]) Print timestamp for every transmission. [-x] ([—hex]) Display hexadecimal ascii values. [-u] ([—unix98]) Use SYSV (Unix98) ptys instead of BSD ptys. [—color ] Color to use for normal output. [—timecolor ] Color to use for timestamp. [—bytescolor ] Color to use for number of bytes transmitted.
Valid values are:
black, red, green, yellow, blue, magenta,
cyan, white, brightblack, brightred,
brightgreen, brightyellow, brightblue,
brightmagenta, brightcyan, and brightwhite. [-h] ([—help]) Display help.

Читайте также:  Переключить линукс на русский

Rc File

The following options can be specified via rc-file ‘.slsnifrc’, which should be located in the user’s home directory: TOTALBYTES ON/OFF — corresponds to -b (—bytes). TIMESTAMP ON/OFF — corresponds to -t (—timestamp). DISPLAYHEX ON/OFF — corresponds to -x (—hex). NOLOCK ON/OFF — corresponds to -n (—nolock). SYSVPTY ON/OFF — corresponds to -u (—unix98). COLOR — corresponds to —color . TIMECOLOR — corresponds to —timecolor . BYTESCOLOR — corresponds to —bytescolor .

Options specified via command line always override the ones specified via rc-file.

Example

slsnif -n -l log.txt -s 2400 /dev/ttyS1
Started logging data into file ‘log.txt’.
Opened pty: /dev/ttyp0
Saved name of the pty opened into file ‘/tmp/slsnif_pty’.
Opened port: /dev/ttyS1
Baudrate is set to 2400 baud.

Note: pty slsnif has opened is /dev/ttyp0.

Signals

SIGHUP signal causes slsnif to move file pointer in all log files back to the beginning. This is useful for example when logfiles are rotated by logrotate. SIGUSR1 signal causes slsnif to re-synchronize settings between ttys at any time. Useful for debugging software that changes port settings in the middle of transmission.

Источник

Sniff serial port on Linux

I tried interceptty (copy at GitHub), and was successful in using it. First I ran it on the port of interest:

Then I connected the program-under-test to the pseudo-terminal /dev/pts/5 that interceptty created.

I tried to use slsnif, but I found that I got an error:

Failed to open a pty: No such file or directory 

This mailing list item indicates that slsnif only supports the «legacy» pseudo-terminals ( /dev/ttyp0 etc) which are probably not used on current Linux kernels.

Читайте также:  Linux cmd to reboot

Solution 3

Try using jpnevulator (debian packaged) or slsniff. Note that slsniff uses a deprecated terminal emulation model.

Sniff serial port on Linux (3 Solutions!!)

4 ways communicating with serial port in linux

Serial Port Sniffer

RS232 Communication (3) - Linux Serial Port Communication

FREE Serial Port Sniffer Easiest to Use! Works on RS-485, RS-232-, I2C, & others Sniffer for LabVIEW

Mike Holdsworth

(Last updated 2018-08-28.) http://stackoverflow.com/questions/1825585/how-to-determine-what-version-of-powershell-is-installed/1825807#1825807 Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (7 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly. My other accounts: iRosetta. [/]. Stack Overflow (SO). [/]. Server Fault (SF). [/]. Super User (SU). [/]. Meta Stack Overflow (MSO). [/]. Careers. [/]. Other My 15 minutes of fame on Super User Blog. Sample: Jump the shark. LinkedIn profile @PeterMortensen (Twitter) Google profile Quora profile GitHub profile Full jump page with other SOFU related, Stack Exchange sites, etc. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com).

Updated on September 17, 2022

Comments

If some answers provided answer to your question, you should accept it (meta.serverfault.com/questions/1033/…)

You can achieve this with standard tools using socat and tee . 1) socat -d -d pty,raw,echo=0 pty,raw,echo=0 . The output will give you two ports . N PTY is /dev/pts/27. N PTY is /dev/pts/28 . 2) sudo cat /dev/ttyS0 | tee /dev/pts/27 and in another terminal sudo cat /deb/pts/27 | tee /dev/ttyS0 . Finally 3) Connect your program to /dev/tty/28 . The two tee commands will dump both directions to the console and forward to/from the actual serial port. Note that the port settings like baudrate must be configured ahead of time.

You can save the tee stuff to a file, too: cat /dev/pts/27 | sudo tee /dev/ttyS0 serial-caps and xxd will help if it’s a binary protocol: cat /dev/pts/27 | sudo tee /dev/ttyS0 serial-caps | xxd .

@jtpereyda it looks like your 3rd step should be connect program to /dev/ttyS0. e.g. when I connect PUTTY there to /dev/ttyS0 it correctly shows me that output in step 2 while also communicating with the actual embedded board. BTW you have a typo /deb/pts/27 should be /dev/pts/27 . Also cat /deb/pts/27 | tee /dev/ttyS0 should be cat /dev/pts/27 | sudo tee /dev/ttyS0

Источник

Оцените статью
Adblock
detector