Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Read/Write serial
#1
Learning to read/write serial using back to back USB to serial boards (X-cross TX & RX) started writing to a port and monitoring the other using Putty. All looks good as expected but when I read and write to both ports (not using Putty at all) I get the quotes and the "b" from the write. ie. ser3.write( b'->this way') when I do text = ser4.read(100) then print it out I get b'->this way' quotes and apostrophe not sure why!?!?! Any help or clues greatly appreciated.

Please see result following code snippet.

import serial

def Serial_Port_Function(selection):
    print("Serial Port Function selection = " + selection )

    n = 9

    for counter in range (1, n+1):
    
        if 'hello' in selection:
            print ( "howdy!" )    
    
        elif '1' in selection:
            ser3.write( b"->Sent:ATZbdEFGhijkloputrew<<" )

        elif '2' in selection:
            ser4.write( b'->Sent:ZIGGYziggyZIGGYzoooo<<' )

        elif '3' in selection:

            ser3.write( b"->This way >>>>> Writen to Serial 3 >>>>>" )
            text = ser4.read(100)
            print( str(counter) + str(text) + '//The read from com4 ' + '\r\n' )

            ser4.write( b'->That way <<<<< Writen to Serial 4 <<<<<' )
            text = ser3.read(100)
            print( str(counter) + str(text) + '\\The read from com3 ' + '\r\n' )

        else:
            print ( 'nope' )

    return( selection )

# This is the Main Function -------------------------------------------------------###
print( "enter \n1. for output to com4\n2. for output to com3\n3. for bi-directional\n" )
input_text = input( '-->' )
print ( input_text )

if 'hello' in input_text:
    print ( input_text )
    
elif '1' in input_text:
    print ( input_text + ' output to com4' )
    input( "Open Serial 4 with Putty press enter when ready" )
    ser3 = serial.Serial("com3", timeout=0.07)  # open serial port3
    print(ser3.name)         # check which port was really used

elif '2' in input_text:
    print ( input_text + " output to com3" )
    input( "Open Serial 3 with Putty press enter when ready" )
    ser4 = serial.Serial("com4", timeout=0.07)  # open serial port4
    print(ser4.name)         # check which port was really used

elif '3' in input_text:
    print ( input_text + ' is bi-directional' )
    input( "Ensure All Putty ports are closed, press enter when ready" )

    ser3 = serial.Serial("com3", timeout=0.1)  # open serial port3
    print(ser3.name)         # check which port was really used

    ser4 = serial.Serial("com4", timeout=0.1)  # open serial port4
    print(ser4.name)         # check which port was really used

#    ser3.close()
#    ser4.close()

else:
    print ( 'nope' )

ret_val = Serial_Port_Function( input_text )

print( "Returned value =>",ret_val )

#quit()
[icode][/icode]
What I get for output is:

1b'->This way >>>>> Writen to Serial 3 >>>>>'// The read from com4

1b'->That way <<<<< Writen to Serial 4 <<<<<'\The read from com3

2b' etc., etc., etc.

Why am I getting the b and enclosing apostrophes? I don't see this when monitoring port using Putty.

Thanks in advance for answer or clue!

Dave Z. (dlizimmerman)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,309 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 582 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,321 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 781 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,501 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,515 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Read JSON via API and write to SQL database TecInfo 5 2,108 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  Write and read back data Aggie64 6 1,812 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  python serial port barryjo 2 1,601 Dec-27-2021, 11:09 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020