-3

I am a bit new to python and

    import serial
    import time
    ser = serial.Serial('COM3', 9600, timeout=0)

    while 1:
     try:
      print ser.readline()
      time.sleep(1)
     except ser.SerialTimeoutException:
      print('Data could not be read')
      time.sleep(1)

I installed pyserial. Why such a simple program like this is giving "invalid syntax" error at the line ser.readline(). Why has been python designed like this that it always makes beginners life difficult. Even here at stackoverflow why syntaxing code is so difficult? everyline has to be indented here.Why cant a simple do the job here.Well this is all together a different topic but why such a simple python program is creating errors?????

  • Are you using Python 3 ? If you are, the syntax is `print(ser.readline())`, not `print ser.readline()`. It's one of the differences with Python 2. – Abrikot Jun 02 '17 at 06:58
  • Even this import serial import time ser = serial.Serial('COM5', 9600, timeout=0) print ser.readline() is creating errors – Sriranjan Rasakatla Jun 02 '17 at 06:59
  • could you us show the exact error? – Nicolas Heimann Jun 02 '17 at 06:59
  • Yes after I adjusted the print syntax statement it worked. The entire internet has examples on earlier versions of python and why did they change such a simple thing. One more questions I am trying to get this code running in windows (with python3 and pyserial). [link](https://github.com/nfd/spi-flash-programmer). What changes do I need to make and get this running from the command line? – Sriranjan Rasakatla Jun 02 '17 at 07:05
  • maybe because you are not making valid python code? – Netwave Jun 02 '17 at 07:11
  • Could you tell me how to modify the code to get it running in windows. What options do I need to give to make this code identify windows serial port. How do I change this statement to make it work in windows. python3 spi_flash_programmer_client.py -d /dev/cu.usbserial --flash-offset 0 -s 4096 -f dump.bin read And how do I change this here: def init(self, filename): self.sock = serial.Serial(filename, 115200, timeout=1) instead of filename should I put 'COM5'? Let me know. – Sriranjan Rasakatla Jun 02 '17 at 07:12
  • Please open a new question, this is not related to your current issue with `print()`. Show your code and *exactly* the errors you are getting. Post them in the question, code put into a comment (unless it is short) is difficult to read. – cdarke Jun 02 '17 at 07:21
  • Your frustrations about Python syntax indicate that you have not yet come to grips with Python's culture. I suggest you follow a python 3 tutorial and read https://www.python.org/dev/peps/pep-0008/. As to why web pages are not updated to use Python 3, one reason is that many people are still using Python 2, another is the nature of the web - python is not the only subject that is not kept up-to-date, its an issue with any technical subject. – cdarke Jun 02 '17 at 07:26

1 Answers1

1

If you used the python 3.x, you must used the print function with the (), for example, you want print the hello world, you need write:

print("Hello World")

To you code, you need change the print ser.readline() to print(ser.readline())

At Python world you need user four space character for code hierarchy.

youDaily
  • 1,372
  • 13
  • 21