コード例 #1
0
        private bool SetPLCWLCData(string pid, string vin, string suffix, string modelcode, string plccode, string chassis)
        {
            int    iReturnCode;                     //Return code
            String szDeviceName  = "ZR1101";        //List data for 'DeviceName'
            int    iNumberOfData = 29;              //Data for 'DeviceSize'

            short[] arrDeviceValue;                 //Data for 'DeviceValue'
            char[]  chpid       = new char[10];
            char[]  chvin       = new char[18];
            char[]  chsuffix    = new char[2];
            char[]  chmodelcode = new char[4];
            char[]  chplccode   = new char[4];
            char[]  chchassis   = new char[20];

            chpid       = pid.ToCharArray();
            chvin       = vin.ToCharArray();
            chsuffix    = suffix.ToCharArray();
            chmodelcode = modelcode.ToCharArray();
            chplccode   = plccode.ToCharArray();
            chchassis   = chassis.ToCharArray();

            //var listch = chpid.ToList();
            //listch.Add(chvin.ToList());

            //Char[] cr = chpid + chvin + chsuffix + chmodelcode + chplccode + chchassis;
            //Char[] cr = str.ToCharArray(); //Kumpulkan data dalam bentuk byte array

            //Assign the array for 'DeviceValue'.
            arrDeviceValue = new short[iNumberOfData];

            int k = 0;

            //int l = cr.Length;
            byte[] byt = new byte[58];
            foreach (var ea in chpid)
            {
                byt[k] = Convert.ToByte(ea);
                k++;
            }
            foreach (var ea in chvin)
            {
                byt[k] = Convert.ToByte(ea);
                k++;
            }
            foreach (var ea in chsuffix)
            {
                byt[k] = Convert.ToByte(ea);
                k++;
            }
            foreach (var ea in chmodelcode)
            {
                byt[k] = Convert.ToByte(ea);
                k++;
            }
            foreach (var ea in chplccode)
            {
                byt[k] = Convert.ToByte(ea);
                k++;
            }
            foreach (var ea in chchassis)
            {
                byt[k] = Convert.ToByte(ea);
                k++;
            }

            var a = 0;
            var b = 0;

            while (a < byt.Count())
            {
                //var txt = "ZR" + j;
                Int16 value = byt[a + 1];
                value <<= 8;
                value  += Convert.ToInt16(byt[a]);
                //plc.WriteDeviceBlock(txt, 1, Convert.ToInt32(value));
                //nilai[k] = ;
                arrDeviceValue[b] = value;
                a += 2;
                b += 1;
                //j++;
            }

            try
            {
                //The WriteDeviceRandom2 method is executed.
                iReturnCode = plc.WriteDeviceBlock2(szDeviceName,
                                                    iNumberOfData,
                                                    ref arrDeviceValue[0]);
            }
            //Exception processing
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
                string stringFormat = "Send WLC Data; PID = " + pid + " Failed";
                StatusPLCLog = stringFormat;
                return(false);
            }
            if (iReturnCode == 0)
            {
                string stringFormat = "Send WLC Data; PID = " + pid + " Success";
                StatusPLCLog = stringFormat;
                return(true);
            }
            else
            {
                return(false);
            }
        }