Synchronize Script to DAQ Measurements


#1

From a MG400 script, I’d like to send a msg, at certain points in the script to signal a laptop connected running TCP/IP Python code to then signal external data acq equipment to take measurements. I know that I could use a DO, but would like to do this through messaging. Is there a way to do this, and please give an example msg script? Thanks.


#2

I would also like to do something like this.
I need to trigger a reflash procedure, so a function call in windows to a .bat file or similar would be appreciated.
I’m thinking about manipulating a keyboard at the moment to trigger the application. Any other ideas?


#3

I was scrolling through the LUA syntax guide the other day and came up with an idea about using TCP-client from the robot to an TCP-server application on the computer side to trigger my reflash application, could that be something for you as well @apinkos32?

I actually generated a simple C# application with ChatGPT (fun to test it out) that handles the “server” side of the solution, it executes my application when requested from the TCP-client.

I haven’t tested the TCP side in DobotStudio yet, but the server-side (computer) is working as it should.


#4

@Niclas - can you share the code? Have you had a chance to test it? Thanks.


#5

Here is the C# code I’m using with good results.
Please feel free to tweak it to your liking. :slight_smile:
I’m executing 6 parallel flash applications, analyzing the output from each thread and then returning the result back to the robot to sort out the failed units.

I’ll see if I can post the robot-side of the application later.

using System; using System.Diagnostics; using System.IO; using System.Threading.Tasks; using System.Net.Sockets; using System.Net;

namespace NinaFlashTCPserver
{
class Program
{
static async Task Main(string[] args)
{
int port = 23; // the port to listen on
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();

        while (true)
        {
            Console.WriteLine("Waiting for TCP connection from Dobot...");
            TcpClient client = listener.AcceptTcpClient();
            NetworkStream stream = client.GetStream();
            Console.WriteLine("TCP connection established!");

            string indata = "";
            Console.WriteLine(@"Waiting for flash command from Dobot...");
            while (true)
            {

                byte[] buffer = new byte[1024];
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                indata += System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead).TrimEnd('\0');
                if (indata.EndsWith("\n"))
                {
                    break;
                }
            }
            string resultForDobot = "";
            if (indata.Contains("Flash!"))
            {
                //Todo Add ack back to Dobot
                    
                byte[] ackData = System.Text.Encoding.ASCII.GetBytes("Ack!\n");
                stream.Write(ackData, 0, ackData.Length);

                Console.WriteLine(@"Executing reflash...");
                    
                // Set up the path to application and the arguments
                string appPath = @"c:\NINAflasher\FlashTool.exe";
                string[] appArgs = new string[]
                {
                @"--port=COM3 --baud=921600 --deviceName=NINA-W152 --secureBootEnabled=TRUE c:\NINAflasher\NINA-W15X-CF-1.0.json",
                @"--port=COM4 --baud=921600 --deviceName=NINA-W152 --secureBootEnabled=TRUE c:\NINAflasher\NINA-W15X-CF-1.0.json",
                @"--port=COM5 --baud=921600 --deviceName=NINA-W152 --secureBootEnabled=TRUE c:\NINAflasher\NINA-W15X-CF-1.0.json",
                @"--port=COM6 --baud=921600 --deviceName=NINA-W152 --secureBootEnabled=TRUE c:\NINAflasher\NINA-W15X-CF-1.0.json",
                @"--port=COM7 --baud=921600 --deviceName=NINA-W152 --secureBootEnabled=TRUE c:\NINAflasher\NINA-W15X-CF-1.0.json",
                @"--port=COM8 --baud=921600 --deviceName=NINA-W152 --secureBootEnabled=TRUE c:\NINAflasher\NINA-W15X-CF-1.0.json"
                };

                // Start each application in parallel and capture its output
                var outputs = await RunApplicationsInParallel(appPath, appArgs);

                // Analyze the output from each application
                for (int i = 0; i < outputs.Length; i++)
                {
                    string result = "";
                    if (outputs[i].Contains("1170KB/1170KBDevice firmware successfully updated."))
                    {
                        result = $"Application {i + 1} succeeded";
                        int slot = i + 1;
                        resultForDobot += slot;
                    }
                    else
                    {
                        result = $"Application {i + 1} failed";
                        resultForDobot += 0;
                    }

                    // Write the result to the console
                    Console.WriteLine(result);

                    // Write the result to a file
                    string logFilePath = $"FlashResult.txt";
                    using (StreamWriter writer = new StreamWriter(logFilePath, true))
                    {
                        writer.WriteLine(result);
                    }
                }
            }
            // Send result to Dobot
            Console.WriteLine(@"Sending result to Dobot");
            Console.WriteLine(resultForDobot);
            byte[] data = System.Text.Encoding.ASCII.GetBytes(resultForDobot);
            stream.Write(data, 0, data.Length);

            Console.WriteLine(@"Reflash finished.");
            stream.Flush();
        }
    }

                static async Task<string[]> RunApplicationsInParallel(string appPath, string[] appArgs)
                {
                    // Set up a list to hold the output from each application
                    var outputs = new string[appArgs.Length];

                    // Start each application in parallel and capture its output
                    var tasks = new Task[appArgs.Length];
                    for (int i = 0; i < appArgs.Length; i++)
                    {
                        int index = i; // Need to copy i into a new variable to use inside the lambda
                        tasks[index] = Task.Run(() =>
                        {
                            // Create a process to run the application
                            ProcessStartInfo startInfo = new ProcessStartInfo(appPath);
                            startInfo.Arguments = appArgs[index];
                            startInfo.RedirectStandardOutput = true;
                            startInfo.UseShellExecute = false;

                            // Start the process and capture its output
                            Console.WriteLine("Executing {0} {1}", startInfo.FileName, startInfo.Arguments);
                            using (Process process = Process.Start(startInfo))
                            {
                                using (StreamReader reader = process.StandardOutput)
                                {
                                    outputs[index] = reader.ReadToEnd();
                                }
                            }
                        });
                    }

                    // Wait for all the applications to finish running
                    await Task.WhenAll(tasks);

                    return outputs;
                }
}

}