rs232 python_RS232 sniffing with Python

0f9fa28e67ca70a69dc980c6e0bdb758.png

Published on:15.04.2018

My motivation for monitoring RS232 with Python.

I had PC and LG TV screen communicating via RS232 cable.

PC was turning TV screen on and off via RS232 commands.

But TV screen was not acting normally, it was always in standby mode.

So, either PC was not sending RS232 commands or TV screen was not responding to them .

In order to diagnose the problem, I decided to spy on their communication .

half-duplex and full-duplex RS232

There are two types of communication in RS232 : half-duplex and full-duplex.

In half-duplex while one device is talking another one is listening, just like people communication should be.

In full-duplex both devices are talking and listening at the same time, people think that they can do this also, but it is not working in practice.

Hardware

The first step was to make special half duplex RS232 spy cable because I did not have software access on PC.

So the only solution was to sniff communication between PC and TV .

60238f602cc680382b6eb65cc21bb6c1.png

On the same website, there are also Schematics for full duplex RS232 spy cable.

Software

For redirecting, all communication data from the RS232 compliant serial port device into a text file Eltima RS232 Data Logger can be used.

The program is working fine, the only problem that I had was that there is no output on screen just to text file.

No real-time watching of communication data was possible, due to that I decided to make my own program for viewing RS232 communication data in real-time .

Python to the rescue

The best thing about Python program language is that there is a package for everything.

For access to the serial port in Python, there is pySerial package.

My Python program was reading communication data and showing it on screen in real-time with timestamps of reading, and time difference between the last read.

Python source code for RS232 sniffing:

import serial # pyserial

import datetime

def to_hex(input_str):

outpur_str = " ".join("{:02x}".format(ord(c)) for c in input_str)

# 4b 41 20 30 20 46 46 0d

return outpur_str

def main():

ser = serial.Serial(

port='COM1',

baudrate=9600,

parity=serial.PARITY_NONE,

stopbits=serial.STOPBITS_ONE,

bytesize=serial.EIGHTBITS

)

# open if not opened

if ser.isOpen() == False:

print "OPENING port."

ser.open()

# header

fmt = "{0:15}{1:35}{2}"

print fmt.format('TIME', 'RAW HEX', 'TIME DELTA')

# so to have first value

time_of_last_read = datetime.datetime.now()

while True:

data_to_read = ser.in_waiting

if data_to_read != 0: # read if tehre is new data

time_now = datetime.datetime.now()

data = ser.read(data_to_read)

hex_with_space = to_hex(data)

time_with_miliseconds = time_now.strftime("%H:%M:%S.%f")[:-3]

time_delta = time_now - time_of_last_read

print fmt.format(time_with_miliseconds, hex_with_space, time_delta)

# last time

time_of_last_read = time_now

ser.close()

if __name__ == '__main__':

main()

注意:本文来自Sasa Buklijas。本站无法对本文内容的真实性、完整性、及时性、原创性提供任何保证,请您自行验证核实并承担相关的风险与后果!

CoLaBug.com遵循[CC BY-SA 4.0]分享并保持客观立场,本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题,请通过[eMail]联系我们处理,如需商业授权请联系原作者/原网站。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值