Beispiel #1
0
        string testHexString(string inputString, int length, ref ushort outPut)
        {
            string workString = inputString;
            ushort workOutPut = outPut;

            if (workString.Length > length)
            {
                workString = workString.Substring(0, length);
            }
            if (workString.Length == 4)
            {
                int    discarded = 0;
                byte[] result    = new byte[2];

                result = HexEncoding.GetBytes(workString, out discarded);
                try
                {
                    outPut = (ushort)((result[0] << 8) + result[1]);
                    return(workString);
                }
                catch
                {
                    AsyncMessageBox asyncMSGBOX = new AsyncMessageBox("No valid Hex value");
                    asyncMSGBOX.Show();
                    workString = "";
                    outPut     = 0;
                    return(workString);
                }
            }
            return(workString);
        }
Beispiel #2
0
        private void textBoxHoldingRegsWrite_TextChanged(object sender, EventArgs e)
        {
            if (textBoxHoldingRegsWrite.Text.Length > 4)
            {
                textBoxHoldingRegsWrite.Text = textBoxHoldingRegsWrite.Text.Substring(0, 4);
            }
            if (textBoxHoldingRegsWrite.Text.Length == 4)
            {
                int    discarded = 0;
                byte[] result    = new byte[2];

                result = HexEncoding.GetBytes(textBoxHoldingRegsWrite.Text, out discarded);
                try
                {
                    HoldingRegsWriteAddress         = result;
                    textBoxHoldingRegsDecWrite.Text = (((int)HoldingRegsWriteAddress[0] << 8) + (int)HoldingRegsWriteAddress[1]).ToString();
                }
                catch
                {
                    AsyncMessageBox asyncMSGBOX = new AsyncMessageBox("No valid Hex value");
                    asyncMSGBOX.Show();
                    textBoxHoldingRegsWrite.Text    = "";
                    textBoxHoldingRegsDecWrite.Text = "";
                    HoldingRegsWriteAddress[0]      = 0x00;
                    HoldingRegsWriteAddress[1]      = 0x00;
                }
            }
        }
Beispiel #3
0
        private void textBoxCoilAddressRead_TextChanged(object sender, EventArgs e)
        {
            if (textBoxCoilAddressRead.Text.Length > 4)
            {
                textBoxCoilAddressRead.Text = textBoxCoilAddressRead.Text.Substring(0, 4);
            }
            if (textBoxCoilAddressRead.Text.Length == 4)
            {
                int    discarded = 0;
                byte[] result    = new byte[2];

                result = HexEncoding.GetBytes(textBoxCoilAddressRead.Text, out discarded);
                try
                {
                    coilAddressRead = result;
                    textBoxCoilDecAddressRead.Text = ((int)coilAddressRead[0] * 256 + (int)coilAddressRead[1]).ToString();
                }
                catch
                {
                    AsyncMessageBox asyncMSGBOX = new AsyncMessageBox("No valid Hex value");
                    asyncMSGBOX.Show();
                    textBoxCoilAddressRead.Text    = "";
                    textBoxCoilDecAddressRead.Text = "";
                    coilAddressRead[0]             = 0x00;
                    coilAddressRead[1]             = 0x00;
                }
            }
        }
Beispiel #4
0
        private void textBoxRegWriteValue_TextChanged(object sender, EventArgs e)
        {
            if (textBoxRegWriteValue.Text.Length > 4)
            {
                textBoxRegWriteValue.Text = textBoxRegWriteValue.Text.Substring(0, 4);
            }
            if (textBoxRegWriteValue.Text.Length == 4)
            {
                int    discarded = 0;
                byte[] result    = new byte[2];

                result = HexEncoding.GetBytes(textBoxRegWriteValue.Text, out discarded);
                try
                {
                    RegWriteValue = result;
                }
                catch
                {
                    AsyncMessageBox asyncMSGBOX = new AsyncMessageBox("No valid Hex value");
                    asyncMSGBOX.Show();
                    textBoxRegWriteValue.Text = "0000";
                    RegWriteValue[0]          = 0x00;
                    RegWriteValue[1]          = 0x00;
                }
            }
        }
Beispiel #5
0
        private void textBoxAddress_TextChanged(object sender, EventArgs e)
        {
            if (textBoxAddress.Text.Length > 2)
            {
                textBoxAddress.Text = textBoxAddress.Text.Substring(0, 2);
            }
            if (textBoxAddress.Text.Length == 2)
            {
                int    discarded = 0;
                byte[] result    = new byte[1];

                result = HexEncoding.GetBytes(textBoxAddress.Text, out discarded);
                try
                {
                    clientAddress = result[0];
                    textBox2.Text = clientAddress.ToString();
                }
                catch
                {
                    AsyncMessageBox asyncMSGBOX = new AsyncMessageBox("No valid Hex value");
                    asyncMSGBOX.Show();
                    textBoxAddress.Text = "";
                    textBox2.Text       = "";
                    clientAddress       = 0x00;
                }
            }
        }