1

I am trying to communicate with my module by using a Python file. I create line-break point to be sure that I will have an interruption while writing in the module.

But, I don't have any result for reading from the port communication. I need to display all data in my cmd.exe and that already diplayed on COM4 by using my python file

import serial
ser = serial.Serial(
    port='COM4',\
    baudrate= 230400,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)  # open serial port
print(ser.name)         # check which port was really used
ser.write(b'hello')     # write a string
str=ser.readline()
print str
ser.close()             # close ports

That means that these two lines:

str=ser.readline() 
print str

don't give me any results.

user7161303
  • 29
  • 1
  • 1
  • 7
  • From which device are you trying to read? Is it a Windows OS (I guess so)? If you use the ser.readline() method, it will wait until a breakline ('\n') is introduced. Use better the ser.read() method. Moreover, you don't need to use the backslashes (\) on the *ser* assigning instruction. – Jalo Nov 15 '16 at 16:15
  • 2
    [```str```](https://docs.python.org/3/library/stdtypes.html#str) is a Python datatype/class - assigning something else to that name could lead you astray later if you try to use ```str``` and it is no longer a datatype/class but just a variable name. – wwii Nov 15 '16 at 16:21
  • Dear Jalo, I need to display all data already diplayed on COM4 in my cmd.exe by using my python file. I use Windows, my UART is included in an FPGA device. – user7161303 Nov 15 '16 at 16:24
  • 1
    You mean, that you already have data in your CMD window, and you want to run a Python script to read it? I'm not sure that's possible, at least not the way you want to. – SiHa Nov 15 '16 at 16:27
  • I mean that I have data in my COM4 and i want to display it in my cmd. – user7161303 Nov 15 '16 at 16:30
  • So, have you already seen your data printed in your cmd and thus you have checked that the port is working alright? If you know the data length sent by the FPGA e.g. 10 you can read it using ser.read(10) – Jalo Nov 15 '16 at 17:00
  • Dear @Jalo, I have already seen my data in Ter term, I am sur with the parameters of the port. – user7161303 Nov 15 '16 at 17:08
  • So, try the ser.read() instruction – Jalo Nov 15 '16 at 17:11

1 Answers1

1

What's most likely happening is that ser.readline() is waiting for a newline char (\n) to be received on the serial port, but isn't getting it so it hangs. If your serial port is set to echo what you're sending it, you likely need to include the newline char with the data you send it, i.e.

ser.write(b'Hello\n')

or, if your serial device is expecting Windows style newlines:

ser.write(b'Hello\r\n')

If you're stilll not getting any response, you can try debugging after your ser.write statement with

while True:
    print(ser.read(1).decode())

in order to display every byte as it comes back Note: only use the above for debugging, it will hang until the device is closed outside your script.

If you see nothing, then there's probably something more fundamental going on, like the serial port setup. I notice that your baud rate is not a standard baud rate, are you sure that's right?

Side note: there's no need for the backslashes after each argument in your serial.Serial declaration, the fact that the text is within enclosed parentheses makes the code valid. Also, you're supplying the default arguments for most of the parameters, so there's no need to include them at all.

Another aside: working with I/O devices that block on reads can be tricky, and it might be useful setting up a reader thread that pushes data received into a queue.Queue object like what's described here. Or if you're feeling adventerous, try using the asyncio module.

Community
  • 1
  • 1
Billy
  • 5,179
  • 2
  • 27
  • 53
  • I need to display all data already diplayed on COM4 in my cmd.exe by using my python file – user7161303 Nov 15 '16 at 16:27
  • Dear @Bill, I try what you ask me to do, however , i don't understand this note: Note: only use the above for debugging, it will hang until the device is closed outside your script. – user7161303 Nov 15 '16 at 16:51
  • import serial ser = serial.Serial( port='COM4',\ baudrate= 230400) # open serial port print(ser.name) # check which port was really used ser.write(b'hello') # write a string #str=ser.readline() while True: print(ser.read(1).decode()) ser.close() # close ports – user7161303 Nov 15 '16 at 16:53
  • did you try `ser.write(b'hello\n')`? – Billy Nov 15 '16 at 16:54
  • http://stackoverflow.com/questions/19908167/reading-serial-data-in-realtime-in-python I saw this link, it is my same situation, although i try to use tera term and not putty – user7161303 Nov 15 '16 at 17:00
  • "Yes and it works perfectly" - does that imply the answer is accepted? If so, can you accept it? – Billy Nov 15 '16 at 17:21