CSharp Keyence MD X2500

Prompt

Create C# code to send QR code with text “QXD9000” for keyence MD X2500.

Abacus

Here’s C# code to send a QR code with text “QXD9000” to a Keyence MD-X2500 marker:

using System;
using System.Net.Sockets;
using System.Text;

namespace KeyenceMarkerControl
{
    class Program
    {
        static void Main(string[] args)
        {
            string ipAddress = "192.168.0.10"; // Replace with your MD-X2500 IP address
            int port = 9004; // Default Keyence port
            string qrText = "QXD9000";
            
            try
            {
                SendQRCodeToKeyence(ipAddress, port, qrText);
                Console.WriteLine("QR code sent successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }

        static void SendQRCodeToKeyence(string ipAddress, int port, string qrText)
        {
            using (TcpClient client = new TcpClient())
            {
                // Connect to the Keyence marker
                client.Connect(ipAddress, port);
                NetworkStream stream = client.GetStream();

                // Command to set QR code data
                // Format: "WD,<data>\r" for write data command
                string command = $"WD,{qrText}\r";
                byte[] data = Encoding.ASCII.GetBytes(command);

                // Send the command
                stream.Write(data, 0, data.Length);
                Console.WriteLine($"Sent: {command.Trim()}");

                // Read response
                byte[] buffer = new byte[1024];
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine($"Response: {response.Trim()}");

                // Optional: Send print command
                string printCommand = "IP\r"; // Issue Print command
                byte[] printData = Encoding.ASCII.GetBytes(printCommand);
                stream.Write(printData, 0, printData.Length);
                Console.WriteLine($"Sent: {printCommand.Trim()}");

                // Read print response
                bytesRead = stream.Read(buffer, 0, buffer.Length);
                response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine($"Response: {response.Trim()}");

                stream.Close();
            }
        }
    }
}

Key points:

  1. IP Address & Port: Update ipAddress with your MD-X2500’s actual IP address. Default port is typically 9004.
  2. Commands:
    • WD,<data>\r – Write data to the marker
    • IP\r – Issue print command to mark the QR code
  3. Alternative commands you might need:
    • WP,<program_number>\r – Select a specific program/pattern
    • RC\r – Remote control ON
    • RF\r – Remote control OFF
  4. Configuration: Ensure your MD-X2500 is configured for:
    • Ethernet communication enabled
    • QR code pattern set up in the device
    • Remote control mode enabled

Adjust the commands based on your specific MD-X2500 configuration and communication protocol settings.