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()
  • 3
    Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon, but more urgently please read about how to create an MCVE ([MCVE]). 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 `` 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. – Jonathan Leffler Feb 08 '18 at 06: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. – Kevin Feb 08 '18 at 06:35
  • Could you show what is inside the py file, because that is where the error seems to be coming from – whackamadoodle3000 Feb 08 '18 at 06:36
  • @ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000 Please see updated question. – J. A. De la Peña Feb 08 '18 at 06:41
  • The Python script you show is probably not what's actually on disk. – Some programmer dude Feb 08 '18 at 06:42
  • @Kevin Please see updated question. – J. A. De la Peña Feb 08 '18 at 06:42
  • @Someprogrammerdude I may have failed to understand your comment. I am using Raspberry Pi and it's the one in my directory. – J. A. De la Peña Feb 08 '18 at 06:51
  • @JonathanLeffler Sorry about that and thanks for the heads up. Updated the question. – J. A. De la Peña Feb 08 '18 at 07:13
  • 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? – Some programmer dude Feb 08 '18 at 07: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). – Jonathan Leffler Feb 08 '18 at 07:15
  • I tried opening in a hex-editor and everything is correct. I have not seen any rubbish hex characters. – J. A. De la Peña Feb 08 '18 at 09:17
  • @JonathanLeffler I have done hex dumping. I am working with Raspberry Pi so I used the commands you shared. The same results, though. – J. A. De la Peña Feb 08 '18 at 09:18
  • If the file really does contain plain text, then Python is confused. It is saying that it is seeing something else altogether. Given a choice between Python being mistaken and you being mistaken, I'm sorry but I trust Python over you. The difficulty now is working out what's going wrong. I've not programmed a Raspberry Pi. Can you upload the file from the Pi and check that what you upload is what you think you downloaded to the Pi? Have you tested the program — the C you show and the Python script — on a Linux or Windows box? Does it work? If the `serial` module is a problem, change it. – Jonathan Leffler Feb 08 '18 at 09:23

1 Answers1

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

Richard
  • 25
  • 2
  • 8