コード例 #1
0
        public static bool WriteSingleRegister(UInt16 address, UInt16 value, ISerialPortIo port)
        {
            int timeOutCounter = 0;
            byte[] AddressBytes = BitConverter.GetBytes(address);
            byte[] ValueBytes = BitConverter.GetBytes(value);

            byte[] Command = new byte[8];
            Command[0] = 0x00;                          //Node ID
            Command[1] = 0x06;                          //Function
            Command[2] = AddressBytes[1];               //Starting Address Hi
            Command[3] = AddressBytes[0];               //Strating Address Low
            Command[4] = ValueBytes[1];                 //Quantity of Registers Hi
            Command[5] = ValueBytes[0];                 //Quantity of Registers Low
            Command[6] = 0x00;                          //CRC Hi
            Command[7] = 0x00;                          //CRC Low

            byte[] array = BitConverter.GetBytes(ModRTU_CRC(Command, 6));

            Command[6] = array[0];               //CRC Hi
            Command[7] = array[1];               //CRC Low

            //bool result = TransmitCommand(GlobalVariables.serialPort, Command, 8);

            port.DiscardInBuffer();
            port.DiscardOutBuffer();
            port.Write(Command, 0, 8);
            port.Flush();
            while (port.BytesToWrite != 0)
            {
                //System.Diagnostics.Debug.WriteLine("Waiting to Write Bites: " + GlobalVariables.serialPort.BytesToWrite);
            }

            while (true)
            {
                if (timeOutCounter >= 1000000)
                {
                    System.Diagnostics.Debug.WriteLine("failed");
                    return false;
                }

                if (port.BytesToRead >= 8)
                {
                    System.Diagnostics.Debug.WriteLine(timeOutCounter++);
                    break;
                }

                timeOutCounter++;
            }

            //while (GlobalVariables.serialPort.BytesToRead < 8)
            //{
            //    timeOutCounter++;
            //    if (timeOutCounter == 1000)
            //    {
                    
            //        return false;
            //    }
            //}

            byte[] receiveBuffer = new byte[port.BytesToRead];

            try
            {
                int readBytes = port.Read(receiveBuffer, 0, port.BytesToRead);
                System.Diagnostics.Debug.WriteLine("Read Bytes: " + readBytes);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
コード例 #2
0
 public void TryDiscardInBuffer(ISerialPortIo serialPort)
 {
     lock (thisLock)
     {
         serialPort.DiscardInBuffer();
     }
 }
コード例 #3
0
        public static bool ReadRegisters(ISerialPortIo serialPort)
        {
            byte[] AddressBytes1 = BitConverter.GetBytes(2);    //Current Speed At Address 2
            byte[] AddressBytes2 = BitConverter.GetBytes(36);  //Input Contact Status At Address 36
            byte[] AddressBytes3 = BitConverter.GetBytes(6);    //Single -Turn Data L At Address 46
            byte[] AddressBytes4 = BitConverter.GetBytes(8);    //Single -Turn Data H At Address 48            
            byte[] AddressBytes5 = BitConverter.GetBytes(22);   //Current Torque At Address 22
            //byte[] AddressBytes6 = BitConverter.GetBytes(38);  //Output Contact Status At Address 38

            byte[] Command = new byte[15];
            Command[0] = 0x00;                          //Node ID
            Command[1] = 0x6A;                          //Function

            Command[2] = 0x0A;                          //Byte Count
            Command[3] = AddressBytes1[1];               //Starting Address Hi
            Command[4] = AddressBytes1[0];               //Strating Address Low
            Command[5] = AddressBytes2[1];               //Starting Address Hi
            Command[6] = AddressBytes2[0];               //Strating Address Low
            Command[7] = AddressBytes3[1];               //Starting Address Hi
            Command[8] = AddressBytes3[0];               //Strating Address Low
            Command[9] = AddressBytes4[1];               //Starting Address Hi
            Command[10] = AddressBytes4[0];               //Strating Address Low
            Command[11] = AddressBytes5[1];               //Starting Address Hi
            Command[12] = AddressBytes5[0];               //Strating Address Low
            Command[13] = 0x00;                          //CRC Hi
            Command[14] = 0x00;                          //CRC Low

            byte[] array = BitConverter.GetBytes(ModRTU_CRC(Command, 13));

            Command[13] = array[0];               //CRC Hi
            Command[14] = array[1];               //CRC Low

            serialPort.DiscardInBuffer();
            serialPort.DiscardOutBuffer();
            bool result = serialPort.WriteWaitResponse(Command, 0, 15, 15);
            System.Diagnostics.Debug.WriteLine("ReadRegisters Thread Id: " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            return result;
        }