I'm trying to send data from a python script to my c# application using a standard input stream. I also need to eventually send data back from my c# application to the another python script. I first tried to do this with a UDP Connection which works fine for a couple lines, but I have a fair amount of data to send (a few thousand lines) and a requirement for data integrity which UDP cannot provide. I could also write to a file, but that seems very inefficient.
One last restriction is that while my two applications are related I cannot setup a direct connection between them using something like IronPython as they are both spawned separately by a 3rd party application.
This is what I am currently trying, but it is not working. Similar to this question: Passing data between Python and C# without writing a file
p = subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate(blob)
On my C# side I'm not entirely sure how to read this data, but I've tried using things like a loop around this:
Console.ReadLine()
or getting the standard input and reading from it directly using:
Console.OpenStandardInput();
The current issue is that as soon as I call p.communicate my Python script gets locked and doesn't proceed. If it's waiting for the line to be read, what do I need to do to make it stop waiting? I tried only providing the stdin parameter, but that didn't help.
Related
What I'm Trying to Achieve
I'm attempting to build a console game that has multiple console windows that would be displaying inventory, status effects, current map, and health. Another console would be the main one that gathers input to effect the other consoles. The reason I want to do it this way is so that the other consoles can be updating their "graphics" (or text) without disturbing the input flow.
What I've Tried So Far
So far, I've attempted to use System.IO's File, FileStream, StreamWriter, and StreamReader to communicate between the consoles via text files. The problem I've ran into is that, when the main console (the input console) is attempting to write inputs to a file--which is communicating with another console (the "graphics" console)--it throws an error because the "graphical" console is trying to read the input of the file (or vice versa).
I figured that making the FileStream's FileAccess be Readable would do the trick, but I ran into the same issue.
I think I could get this to work if I could communicate between the consoles to tell each other that one is done writing to or reading the file... kind of like a back and forth... "I'm writing to the file... okay, I'm done" "I'm reading the file... okay, I'm done" and the cycle continues...
So, in summary, I suppose, my question is how can I communicate between two consoles using files?
Possible Solutions I could try learning SQL, but I don't know if I'd end up running into the same issue... so, if I must learn SQL for this project, I suppose, that'd be my last option.
Thank you!
IPC (inter process communication) is the keyword you're looking for.
There are multiple ways to do IPC, e.g. shared memory, named pipes or similar. .NET has an IpcChannel which uses TCP or a named pipe if the destination is on the local PC.
I am writing an application which communicates with a network analyser using ivi.visa from Keysight. I often work remotely where I don't have an instrument, so I decided to write an application that can respond to the SCPI commands I need. This works ok for things like "*IDN?", but when I try to receive a binary block using ReadBinaryBlockOfByte(), the call terminates when it sees a newline character in the data.
If I set TerminationCharacterEnabled to false, all reads timeout. This is mentioned in the Keysight visa.net examples, so it doesn't seem to be an option.
So how can I read binary data?
i have a chart pattern recognition program using neural networks written in python .instead of porting the whole code to C# ,i decided it was better to only send certain bits indicating the following :
Buy=1,Sell=-1,Do nothing=0
once they are in C-sharp ,i could relay them to a third party program (multicharts) which would continuously call the C# dll function and receive these values after a certain time interval .
my question is ,is there a way to relay these bit's to C# and pack all of this in a dll ,which gets read by the 3rd party program ?
the whole reason i want to port to C# is because multicharts only reads in dll's and i dont think python has them.
sorry for being naive ,i don't have very good grip on C# .
Your options are as follow,
Use a TCP socket, bind it to a port and listen for data in C# while the python application sends all data to it. C# has some great features for sockets such as System.Net.TcpClient
and
System.Net.TcpServer.
Your other option is that if the C# application only needs to be run once it receives information from the python program and then it can die, you could have the python program start the C# one and pass it parameters containing the information that you need transmitted.
By the looks of it your question only asked if there was a way to communicate, these are probably the best two. Please let me know if I can help anymore.
I am looking to transfer a binary file via RS232. I need to do this to 5000 different devices (no joke). I could do them one-by-one through a terminal program but that will take a lot of time.
So, I am writing a C# program that will be able to automate the process. I am looking at using XMODEM protocol and command line parameters to start the process. I have been looking for this for a few hours now and far my results have turned up little. I tried using uCON but that takes some sort of scripting language.
I was wondering if anyone in the community here might know of a solution to transfer a file over RS232 and program it in C#. Whether it is from a protocol (XMODEM) or a program that accepts command line or some other custom solution, doesn't really matter to me.
Ok so I was able to confirm that the code found on the website( ghielectronics.com/community/codeshare/entry/825) was able to work. The issue was was that I did not know how long it would take the computer to transfer the file. I thought that it would be quick. However, after further testing, it actually takes about 30 sec to 1 min depending on the file size.
This C# code snippet allows anyone to transfer a binary file over the Serial Port using the XMODEM protocol. This is done in code and a terminal is not needed which fits the requirements that I needed for the project.
What is a good way to run a time-consuming function from a C# webpage on the users CPU instead of running it on the web-server? Is it possible to get a C# function to run locally or do I have to write the function in JavaScript? The function itself is not secret in any way but I would prefer if the input and the output will be kept secret from the user. The solution should not require the user to download anything.
This functions uses data from the database + user input and when it's finished it writes the output to the database.
The functions best case is ~1s, average ~30s and worst ~10min (for every user) so it's not an option to run this function on the web-server.
You can get this to work, by either
coding the function in JavaScript
compiling the C# source to JavaScript (off the beaten track)
running the C# code in silverlight (done and done again, remember Java Applets? same science)
You won't really be able to keep the data totally secret, though.
Now, how to go about this... You will need a web service to provide the data to your silverlight component and another one to accept the computed results. You know, I don't really see why everyone else here thinks this is such a no-go...
As for data secrecy... The best you can do is obfuscate, though you should use a secure communication layer for aquiring the data and posting the results back, what ends up on the users computer will eventually be open for inspection by the user. If you use obfuscation techniques for your code, this will make reverse engineering an encryption/decryption scheme for the data payload harder, but you're playing essentially the same game as game devs / game crackers...
Personally, I would code the client side stuff in JavaScript. Chances are, what you want done is more of an algorithmic thing than a library thing, so porting to js should not be difficult to pull off.
You can't and shouldn't run arbitrary cs code on the user's computer. You also can't really run a long lived process in javascript on the client. User interaction with the page will be blocked and if they navigate to a different page it will stop.
What you should do is write a windows service to run these tasks in the background. Have a queue table where you save the input data from the web side, then have a service that polls the table for work and processes the input data.
You can't execute any C# code on the client computer when you they enter a webpage. You are correct in your assumption that you need to write it in javascript to execute it on the client.
This doesn't work. Web server does the processing. Unless you are doing distributed computing and stuff like that, it is not designed to work on a client. Client needs to download software to process stuff. Webpage is webpage. Text.
The only code you can run on the client is js.
That being said, you definitely don't want to run your function on the client if you have sensitive information involved.