1

I have C code successfully running in Visual Studio 2019 that fills a buffer with real time data from an FPGA. It is declared as...

unsigned char *pBuffer;

...in C. I can see that the data is correct in a memory watch window, at the address of pBuffer.

I also have installed Python 3.7 in Visual Studio and am successfully plotting with Matplotlib using python.h from a Python array.

So my question is how do I transfer the data from the C buffer to a python array for plotting??

I have looked at ctypes and since my main code is in C, it does not make much sense to go to Python from C then call C again. I have looked at bytes(), bytearray() and memoryview(). Memoryview appeals to me, because it does not copy data and this plotting needs to be very fast as the data comes in very fast. (think oscilloscope) But it does not seem to be real physical addresses that it works with, rather some kind of identifier that does not correspond to any memory location where my data is. I simply want to plot the data that I know exists in a C buffer (1D array) at a specific address. Python seems to be very restrictive.

I can't seem to get anything to do what I want to do, since Python disallows reading data from some specific memory location apparently. This being the case, I wonder how it might examine memory to display the content in any way, let alone transfer the content to a Python array. How is this done? And yes I am a Pythonic Newbie. Thanks for any help or suggestions.

1 Answers1

0

My first idea would be to pass the data in a file. It will add a few milliseconds to the interchange, but that might not even be perceptible after loading the Python interpreter and the matplotlib module.

If you needed more immediate response, maybe a local loopback network connection would be useful. That's more trouble on the C side than on the Python side, especially on Windows.

As for reading directly from memory, you're right. Standard Python won't do that. You could use C-based modules to do whatever you're doing now from a C main program to get data out of your FPGA. If it's all stock Windows API stuff, you might want to take a look at Mark Hammond's PyWin32 module. That can be installed from the Python Package Index using pip install pywin32 from an elevated command prompt. It might support the APIs you need. If it does, then you might not need a separate C program to get the data out.

PS: Another option for interprocess communication is a named pipe. Windows named pipes can be opened with PyWin32. See the top-vote answer for this SO question.

Mike Housky
  • 3,959
  • 1
  • 17
  • 31
  • Thanks for your reply @mike I think transferring in and out by way of a file will be too slow for what I need. I need to display data in terms of microseconds or faster. I will look into your suggestions, but it's sounding like Python and matplotlib may not be capable of doing this simple thing. I'm not having much luck finding a way to do it strictly in C either without QT or gnuplot some graphing package that is complicated to install and use. – user15606644 Apr 12 '21 at 04:14
  • @user15606644 That "few milliseconds" was for the whole dataset to be plotted, not "per point", if that's what you were thinking. Mostly in file open/close. Any plot is going to take *lots* of milliseconds, I think. – Mike Housky Apr 12 '21 at 16:54