1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
   |  
    using ModbusTCP;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
 
    namespace KMP_WT
    {
        static public class CommunicationImprimante
        {
            private static ModbusTCP.Master MBmaster;
            private static ManualResetEvent connectDone = new ManualResetEvent(false);
            private static ManualResetEvent sendDone = new ManualResetEvent(false);
            static public int NumberCommande;
            static internal CFormWT FrmWt;
            static internal string type_marquage;
            //Création de la connexion avec les tetes
            internal const int ListenPortUDP = 10000, ListenPortTCP = 502;
            static internal StringBuilder adresse_ip_string = new StringBuilder();
            static internal string dossierExecution;
            static internal Socket TCP = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            static internal IPAddress ip_local;
            static internal IPEndPoint ep;
            static internal void Ouvrir_Application(CFormWT frmWt)
            {
                dossierExecution = Directory.GetCurrentDirectory().Replace("\\", "/");
                FichierIni.GetPrivateProfileString("General", "Adress", "127.0.0.1", adresse_ip_string, adresse_ip_string.Capacity, dossierExecution + "/Config/Marquage.ini");
                ip_local = IPAddress.Parse(adresse_ip_string.ToString());
                FrmWt = frmWt;
                type_marquage = frmWt.type_marquage;
                if (type_marquage == "IMAJE" || type_marquage=="DOMINO")
                {
                    ep = new IPEndPoint(ip_local, ListenPortTCP);
                    MBmaster = new Master(adresse_ip_string.ToString(), ListenPortTCP);
                    MBmaster.OnResponseData += new ModbusTCP.Master.ResponseData(MBmaster_OnResponseData);
                }
            }
            static public void Envoyer_Commande(string sendedCommande)
            {
                if (type_marquage == "DOMINO")
                {
                Domino_Traitement(sendedCommande);
            }
        }
        private static void Domino_Traitement(string instruction)
        {
            List<byte> commandeEnvoye = new List<byte>();
            List<byte> information = new List<byte>();
            byte[] enteteDomino = new byte[7]{0x00,0x00,0x00,0x00,0x00,0x00,0x00};
            string[] decomposition = instruction.Split(' ');
            information.Add(0x04);
            information.Add(0x00);
            information.Add(0x1E);
            information.Add(0x00);
            information.Add(0x10);
            int octetData = information.Count;
            enteteDomino[4] = (byte)(octetData / 256);
            enteteDomino[5] = (byte)(octetData % 256);
            foreach (byte entete in enteteDomino)
                commandeEnvoye.Add(entete);
            for (int i = 0; i < information.Count; i++)
            {
                commandeEnvoye.Add(information[i]);
            }
            MBmaster.WriteSingleRegister(0, 0x30, 0, commandeEnvoye.ToArray());
            Console.WriteLine(commandeEnvoye.ToArray());
        }
 
 
 
        // ------------------------------------------------------------------------
        static private void MBmaster_OnResponseData(ushort ID, byte unit, byte function, byte[] values)
        {
            // ------------------------------------------------------------------------
            // Identify requested data
            foreach (byte test in values)
                Console.WriteLine(test);
        }
 
        // ------------------------------------------------------------------------
        // Modbus TCP slave exception
        // ------------------------------------------------------------------------
        static private void MBmaster_OnException(ushort id, byte unit, byte function, byte exception)
        {
            string exc = "Modbus says error: ";
            switch (exception)
            {
                case Master.excIllegalFunction: exc += "Illegal function!"; break;
                case Master.excIllegalDataAdr: exc += "Illegal data adress!"; break;
                case Master.excIllegalDataVal: exc += "Illegal data value!"; break;
                case Master.excSlaveDeviceFailure: exc += "Slave device failure!"; break;
                case Master.excAck: exc += "Acknoledge!"; break;
                case Master.excGatePathUnavailable: exc += "Gateway path unavailbale!"; break;
                case Master.excExceptionTimeout: exc += "Slave timed out!"; break;
                case Master.excExceptionConnectionLost: exc += "Connection is lost!"; break;
                case Master.excExceptionNotConnected: exc += "Not connected!"; break;
            }
 
            MessageBox.Show(exc, "Modbus slave exception");
            }
        }
    } | 
Partager