0

I have the following lines of code:

#include <Python.h>
#include <stdio.h>

int main(int argc, char *argv[]){

   FILE * file;

   Py_Initialize();
   file = fopen("LIFT_Head_move_to_Max.py","r");
   PyRun_SimpleFile(file,"LIFT_Head_move_to_Max.py");

   Py_Finalize();
   return 0;
}

When I run the application, the following is the output.

  File "LIFT_Head_move_to_Max.py", line 1
    ▒v▒v@▒p
    ^
SyntaxError: invalid syntax

What seems to be the problem here? I have tried editing in Notepad++ and changed the Encoding but the same thing happens.

We have the same issue with this post.

Thanks!

UPDATE

Contents of the .py file:

#LIFT_Head_move_to_Max
import serial
import struct

ser = serial.Serial(
   port='/dev/ttyS0',
   baudrate=115200,
   parity=serial.PARITY_NONE,
   stopbits=serial.STOPBITS_ONE,
   bytesize=serial.EIGHTBITS
)

print(ser.isOpen())

data="\x5A\x10\x10\x02\x40\x00"

ser.write(data)
ser.close()
13
  • 3
    Welcome to Stack Overflow. Please read the About and How to Ask pages soon, but more urgently please read about how to create an MCVE (minimal reproducible example). Please do not post pictures of plain text. It is annoying. Post the plain text as if it were code. If you don't want any syntax highlighting, include <!-- language: lang-none --> on a line on its own with a blank line above and below it before the output. And the image doesn't show the text in the .py file. Commented Feb 8, 2018 at 6:33
  • 2
    Voting to close: We need to see the contents of LIFT_Head_move_to_Max.py before we can begin to diagnose this. Commented Feb 8, 2018 at 6:35
  • Could you show what is inside the py file, because that is where the error seems to be coming from Commented Feb 8, 2018 at 6:36
  • 1
    And you don't have multiple LIFT_Head_move_to_Max.py files in different directories? If you open the file in a hex-editor, is it correct? Commented Feb 8, 2018 at 7:14
  • 2
    Are you absolutely sure that the .py file is not encoded unusually? One possible source of trouble could be RTF (rich text format) instead of plain text. Another might be UTF-16 instead of UTF-8 or 8859-15 or CP1252. One way to verify this is to do a hex dump of the first few bytes of the file (32 — 64 at most). Since you're using Notepad++, I suspect you may be on a Windows machine, in which case I'm not sure what the best hex dump tool is. On a Unix-like machine, I'd suggest od -c or xxd or something similar (xxd -u -l 64 -g 1 LIFT_Head_move_to_Max.py, for example). Commented Feb 8, 2018 at 7:15

1 Answer 1

1

I had the exact same problem. After some debugging, I found my FILE* is actually null due to the wrong file path. And it threw the confusing SyntaxError: invalid syntax. Maybe you should check against that

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.