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
276 views
in Technique[技术] by (71.8m points)

python - Sending what is received on one serial port to another serial port for logging

I'm currently working on a python script that loggs a serial connection between two devices without interfering/interrupting the connection between the two parties for Logging and troubleshooting purposes.

Heres my Github Project: https://github.com/Oberfeldwedler/serial_logging.git

Heres my the relevant part of the Code:


    ser1 = serial.Serial(
        port='/dev/ttyUSB0',
        baudrate = 9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=.1
    )

    ser2 = serial.Serial(
        port='/dev/ttyUSB1',
        baudrate = 9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=.1
    )


print ('Start logging')
try:
    while counter==0:
        if(datestr != time.strftime("%Y_%m_%d")):
            logf.close()
            datestr = time.strftime("%Y_%m_%d")
            timestr = time.strftime("%H_%M")
            logf = open('/media/pi/log/' + datestr + '_' + timestr + "_tracelog.txt" , "w" )

        x=ser1.readline()
        y=ser2.readline()
        xx=x.decode('UTF-8')
        yy=y.decode('UTF-8')
        if(xx!=""):
            logf.write('
' + time.strftime("%Y_%m_%d %H:%M:%S ") + str(xx))
            ser2.write(x)
            print (xx)
            print (x)
        if(yy!=""):
            logf.write('
' + time.strftime("%Y_%m_%d %H:%M:%S ") + str(yy))
            ser1.write(y)
            print (yy)
            print (y)
        if(xx=="" and yy==""):
            print ('No Data received')
except KeyboardInterrupt:
    os.system('sudo umount ' + speicher)
    print("Speichermedium ausgeworfen.")
    print("Logging erfolgreich beendet.")
    pass

if (speicher !=0):
    logf.close()
    os.system('sudo umount -l ' + speicher)
#os.system('sudo poweroff')

Short Explanation: The script has two serial connections and relays what it receives on one port directly to the other. The whole thing is for logging purposes, so i also write to a file.

I got it working. The only thing I'm not sure about is the part where I'm not interfering in the connection between the two parties or in other words none of the parties noticing that I'm logging.

So my questions are:

  • Do I have to worry about EOLs? I am using Putty to send and receive and it only replaces the current line, where the cursor is at. like_that.gif

  • Can I circumvent the "maybe problem" by handling bare bits/bytes? Would that be the superior solution?

In the example I'm printing x and xx so and this is what I get:

test
b'test
'
  • What does the b' do and how does it affect my Relay-Function?
  • The means new line? That's kind of strange.

I would love to try it out before putting that thing to use, but I want to reduce the Downtime to a minimum. And I would like to advance my knowledge before having to troubleshoot.

question from:https://stackoverflow.com/questions/65546057/sending-what-is-received-on-one-serial-port-to-another-serial-port-for-logging

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...