J. A. De la Peña
J. A. De la Peña

Reputation: 125

Embedding Python in C - Syntax Error line 1 in .py file

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()

Upvotes: 0

Views: 484

Answers (1)

Richard
Richard

Reputation: 25

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

Upvotes: 1

Related Questions