public MainForm()
        {
            InitializeComponent();

               lstDisplayWindowTwo.Visible = false;
               listBoxOneSelected = true;
               comPortConfigForm = null;
               dataProcessor = new DataProcessor(this);
               lstMessageSummary.Sorted = true;
               cmbViews.Items.Add(ALL_MESSAGES);
               cmbViews.Sorted = true;
               cmbViews.SelectedIndex = 0;
               recordedMessages = new List<String>();

               initialized = false;
               stopReceiving = false;
               this.comPortName = "--";
               this.baudRate = DEFAULT_BAUD_RATE;
               this.parity = DEFAULT_PARITY;
               this.dataBits = DEFAULT_DATA_BITS;
               this.stopBits = DEFAULT_STOP_BITS;
               this.handshake = DEFAULT_HANDSHAKE;
               this.rtsEnable = DEFAULT_RTS;
               this.dtrEnable = DEFAULT_DTR;

               lblvPortName.Text = this.comPortName;
               lblvBaudRate.Text = this.baudRate.ToString();
               lblvParity.Text = this.parity.ToString();
               lblvDataBits.Text = this.dataBits.ToString();
               lblvStopBits.Text = this.stopBits.ToString();
               lblvHandshake.Text = this.handshake.ToString();
               lblvRTSEnable.Text = this.rtsEnable.ToString();
               lblvDTREnable.Text = this.dtrEnable.ToString();

               btnOpenAndClose.BackColor = Color.Red;
               btnOpenAndClose.Text = "Closed";
               comPortClosed = true;

               availableComPortNames = FindAvailableComPorts();

               if(availableComPortNames.Count > 0)
                    OpenComPort(availableComPortNames.ElementAt<String>(0), DEFAULT_BAUD_RATE, DEFAULT_PARITY, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_HANDSHAKE, DEFAULT_RTS, DEFAULT_DTR);

               Configuration config = new Configuration();

               string[] connections = config.Read();

              if (!config.IsAdministrator)
                  btnAddCompanyMessage.Enabled = false;
              else
                  isAdministrator = true;

               string errors = "";
               List<MessageFormat> messages = new List<MessageFormat>();
               foreach (string connection in connections)
               {
                   SqlMessageConnection sql = new SqlMessageConnection(connection);

                   if (sql.isConnected())
                       messages.AddRange(sql.GetMessageList());
                   else
                   {
                       try
                       {
                           errors += connection.Split(';')[0] + ';' + connection.Split(';')[1] + ';' + connection.Split(';')[2] + ";Table=" + connection.Split(';')[4] + '\n';
                       }
                       catch { }
                   }
               }

               if (errors != "")
                   MessageBox.Show("The following SQL connections could not be opened:\n\n" + errors);

               foreach (MessageFormat m in messages)
                   AddMessageFormat(m, MessageType.Company);

              initialized = true;
        }
Ejemplo n.º 2
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < parentForm.GetMessagesCount(msgType); i++)
            {
                if ((Byte.Parse(txtID1.Text, System.Globalization.NumberStyles.HexNumber) == parentForm.GetMessageHighByte(i, msgType)) && (Byte.Parse(txtID2.Text, System.Globalization.NumberStyles.HexNumber) == parentForm.GetMessageLowByte(i, msgType)))
                {
                    MessageBox.Show("Message ID already exists", "Duplicate ID", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                if (txtName.Text == parentForm.GetMessageName(i, msgType))
                {
                    MessageBox.Show("Message name already exists", "Duplicate name", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
            }

            if (cmbFormatType.SelectedIndex == 0)
            {
                txtFormat.Text += "g * " + cmbUniformGroup.Text + " " + Format.getTokenString(cmbUniformFormat.Text);

                if (txtFormat.Text == "g * 1 h")
                {
                    txtFormat.Text = "%"; //slight optimization, the parser will skip some extra work
                }
            }
            else if (cmbFormatType.SelectedIndex == 1)
            {
                foreach (FormatLine f in lstFormats.Items)
                {
                    txtFormat.Text += f.ToFormatString();
                }
            }
            else if (cmbFormatType.SelectedIndex == 2)
            {
                txtFormat.Text = "call " + txtExternalFile.Text;
            }

            parentForm.PrintStatusMessage("Format string: " + txtFormat.Text);
            MessageFormat message = new MessageFormat(
                Byte.Parse(txtVersion1.Text, System.Globalization.NumberStyles.HexNumber),
                Byte.Parse(txtVersion2.Text, System.Globalization.NumberStyles.HexNumber),
                Byte.Parse(txtID1.Text, System.Globalization.NumberStyles.HexNumber),
                Byte.Parse(txtID2.Text, System.Globalization.NumberStyles.HexNumber),
                Byte.Parse(txtLength.Text, System.Globalization.NumberStyles.HexNumber),
                txtName.Text,
                txtFormat.Text);

            if (msgType == MessageType.Company)
            {
                if (cmbConnections.SelectedIndex < 0)
                {
                    MessageBox.Show("Please select a database connection.", "Company message failed", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                Connection           c   = (Connection)cmbConnections.Items[cmbConnections.SelectedIndex];
                SqlMessageConnection sql = new SqlMessageConnection(c.connection);
                if (sql.Write(message) == false)
                {
                    MessageBox.Show("Company message failed", "Company message failed", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                MessageBox.Show("Company message created", "Company message created", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else
            {
                MessageBox.Show("Local message created", "Local message created", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            parentForm.AddMessageFormat(message, msgType);
            this.Visible = false;
            reset();
        }
        public MainForm()
        {
            InitializeComponent();

            lstDisplayWindowTwo.Visible = false;
            listBoxOneSelected          = true;
            comPortConfigForm           = null;
            dataProcessor            = new DataProcessor(this);
            lstMessageSummary.Sorted = true;
            cmbViews.Items.Add(ALL_MESSAGES);
            cmbViews.Sorted        = true;
            cmbViews.SelectedIndex = 0;
            recordedMessages       = new List <String>();

            initialized      = false;
            stopReceiving    = false;
            this.comPortName = "--";
            this.baudRate    = DEFAULT_BAUD_RATE;
            this.parity      = DEFAULT_PARITY;
            this.dataBits    = DEFAULT_DATA_BITS;
            this.stopBits    = DEFAULT_STOP_BITS;
            this.handshake   = DEFAULT_HANDSHAKE;
            this.rtsEnable   = DEFAULT_RTS;
            this.dtrEnable   = DEFAULT_DTR;

            lblvPortName.Text  = this.comPortName;
            lblvBaudRate.Text  = this.baudRate.ToString();
            lblvParity.Text    = this.parity.ToString();
            lblvDataBits.Text  = this.dataBits.ToString();
            lblvStopBits.Text  = this.stopBits.ToString();
            lblvHandshake.Text = this.handshake.ToString();
            lblvRTSEnable.Text = this.rtsEnable.ToString();
            lblvDTREnable.Text = this.dtrEnable.ToString();

            btnOpenAndClose.BackColor = Color.Red;
            btnOpenAndClose.Text      = "Closed";
            comPortClosed             = true;

            availableComPortNames = FindAvailableComPorts();

            if (availableComPortNames.Count > 0)
            {
                OpenComPort(availableComPortNames.ElementAt <String>(0), DEFAULT_BAUD_RATE, DEFAULT_PARITY, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_HANDSHAKE, DEFAULT_RTS, DEFAULT_DTR);
            }

            Configuration config = new Configuration();

            string[] connections = config.Read();

            if (!config.IsAdministrator)
            {
                btnAddCompanyMessage.Enabled = false;
            }
            else
            {
                isAdministrator = true;
            }

            string errors = "";
            List <MessageFormat> messages = new List <MessageFormat>();

            foreach (string connection in connections)
            {
                SqlMessageConnection sql = new SqlMessageConnection(connection);

                if (sql.isConnected())
                {
                    messages.AddRange(sql.GetMessageList());
                }
                else
                {
                    try
                    {
                        errors += connection.Split(';')[0] + ';' + connection.Split(';')[1] + ';' + connection.Split(';')[2] + ";Table=" + connection.Split(';')[4] + '\n';
                    }
                    catch { }
                }
            }

            if (errors != "")
            {
                MessageBox.Show("The following SQL connections could not be opened:\n\n" + errors);
            }

            foreach (MessageFormat m in messages)
            {
                AddMessageFormat(m, MessageType.Company);
            }

            initialized = true;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            lblNameError2.Visible = false;
            lblIdError3.Visible = false;
            error = false;
            txtName_check(sender, e);
            txtID_check(sender, e);
            txtLength_check(sender, e);
            if (error == true)
                return;
            ID = ushort.Parse(txtID.Text, System.Globalization.NumberStyles.HexNumber);
            byte[] versionBytes = BitConverter.GetBytes(MainForm.SOFTWARE_VERSION);
            byte[] IdBytes = BitConverter.GetBytes(ID);

            for (int i = 0; i < parentForm.GetMessagesCount(msgType); i++)
            {
                if (i == editIndex)
                    continue;

                if ((IdBytes[1] == parentForm.GetMessageHighByte(i, msgType)) && (IdBytes[0] == parentForm.GetMessageLowByte(i, msgType)))
                {
                    lblIdError3.Visible = true;
                    error = true;
                }
                if (txtName.Text.Trim() == parentForm.GetMessageName(i, msgType))
                {
                    lblNameError2.Visible = true;
                    error = true;
                }
            }
            if (error == true)
                return;

            if (cmbFormatType.SelectedIndex == 0)
            {
                txtFormat.Text = "* " + cmbUniformGroup.Text + " " + Format.getTokenString(cmbUniformFormat.Text);
                if (cmbUniformFormat.Text == "decimal")
                    if (cmbUniformSigned.Text == "unsigned")
                        txtFormat.Text = "* " + cmbUniformGroup.Text + " " + Format.getTokenString("unsigned");

                if (txtFormat.Text == "* 1 h")
                    txtFormat.Text = "%"; //slight optimization, the parser will skip some extra work
            }
            else if (cmbFormatType.SelectedIndex == 1)
            {
                if ((bytes - bytes_used) < 0)
                {
                    lblCustomError.Visible = true;
                    return;
                }
                else
                    lblCustomError.Visible = false;
                foreach (FormatLine f in lstFormats.Items)
                    txtFormat.Text += f.ToFormatString();
            }
            else if (cmbFormatType.SelectedIndex == 2)
            {
                txtFormat.Text = "call " + txtExternalFile.Text;
            }

            MessageFormat message = new MessageFormat(
                versionBytes[1],
                versionBytes[0],
                IdBytes[1],
                IdBytes[0],
                Byte.Parse(txtLength.Text, System.Globalization.NumberStyles.HexNumber),
                txtName.Text.Trim(),
                txtFormat.Text);

            if (msgType == MessageType.Company)
            {
                if (cmbConnections.SelectedIndex < 0)
                {
                    lblCompanyError1.Visible = true;
                    return;
                }
                else
                    lblCompanyError1.Visible = false;
                Connection c = (Connection)cmbConnections.Items[cmbConnections.SelectedIndex];
                SqlMessageConnection sql = new SqlMessageConnection(c.connection);
                if (sql.Write(message) == false)
                {
                    lblCompanyError2.Visible = true;
                    return;
                }
                else
                    lblCompanyError2.Visible = false;
            }
            if (editIndex > -1)
                parentForm.RemoveMessageFormat(editIndex);
            parentForm.AddMessageFormat(message, msgType);
            this.Visible = false;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < parentForm.GetMessagesCount(msgType); i++)
            {
                if ((Byte.Parse(txtID1.Text, System.Globalization.NumberStyles.HexNumber) == parentForm.GetMessageHighByte(i, msgType)) && (Byte.Parse(txtID2.Text, System.Globalization.NumberStyles.HexNumber) == parentForm.GetMessageLowByte(i, msgType)))
                {
                    MessageBox.Show("Message ID already exists", "Duplicate ID", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
             if (txtName.Text == parentForm.GetMessageName(i, msgType))
                {
                    MessageBox.Show("Message name already exists", "Duplicate name", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
            }

            if (cmbFormatType.SelectedIndex == 0)
            {
                txtFormat.Text += "g * " + cmbUniformGroup.Text + " " + Format.getTokenString(cmbUniformFormat.Text);

                if (txtFormat.Text == "g * 1 h")
                    txtFormat.Text = "%"; //slight optimization, the parser will skip some extra work
            }
            else if (cmbFormatType.SelectedIndex == 1)
            {
                foreach (FormatLine f in lstFormats.Items)
                    txtFormat.Text += f.ToFormatString();
            }
            else if (cmbFormatType.SelectedIndex == 2)
            {
                txtFormat.Text = "call " + txtExternalFile.Text;
            }

            parentForm.PrintStatusMessage("Format string: " + txtFormat.Text);
            MessageFormat message = new MessageFormat(
                Byte.Parse(txtVersion1.Text, System.Globalization.NumberStyles.HexNumber),
                Byte.Parse(txtVersion2.Text, System.Globalization.NumberStyles.HexNumber),
                Byte.Parse(txtID1.Text, System.Globalization.NumberStyles.HexNumber),
                Byte.Parse(txtID2.Text, System.Globalization.NumberStyles.HexNumber),
                Byte.Parse(txtLength.Text, System.Globalization.NumberStyles.HexNumber),
                txtName.Text,
                txtFormat.Text);

            if (msgType == MessageType.Company)
            {
                if (cmbConnections.SelectedIndex < 0)
                {
                    MessageBox.Show("Please select a database connection.", "Company message failed", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                Connection c = (Connection)cmbConnections.Items[cmbConnections.SelectedIndex];
                SqlMessageConnection sql = new SqlMessageConnection(c.connection);
                if (sql.Write(message) == false)
                {
                    MessageBox.Show("Company message failed", "Company message failed", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                MessageBox.Show("Company message created", "Company message created", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else
            {
                MessageBox.Show("Local message created", "Local message created", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            parentForm.AddMessageFormat(message, msgType);
            this.Visible = false;
            reset();
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            lblNameError2.Visible = false;
            lblIdError3.Visible   = false;
            error = false;
            txtName_check(sender, e);
            txtID_check(sender, e);
            txtLength_check(sender, e);
            if (error == true)
            {
                return;
            }
            ID = ushort.Parse(txtID.Text, System.Globalization.NumberStyles.HexNumber);
            byte[] versionBytes = BitConverter.GetBytes(MainForm.SOFTWARE_VERSION);
            byte[] IdBytes      = BitConverter.GetBytes(ID);

            for (int i = 0; i < parentForm.GetMessagesCount(msgType); i++)
            {
                if (i == editIndex)
                {
                    continue;
                }

                if ((IdBytes[1] == parentForm.GetMessageHighByte(i, msgType)) && (IdBytes[0] == parentForm.GetMessageLowByte(i, msgType)))
                {
                    lblIdError3.Visible = true;
                    error = true;
                }
                if (txtName.Text.Trim() == parentForm.GetMessageName(i, msgType))
                {
                    lblNameError2.Visible = true;
                    error = true;
                }
            }
            if (error == true)
            {
                return;
            }

            if (cmbFormatType.SelectedIndex == 0)
            {
                txtFormat.Text = "* " + cmbUniformGroup.Text + " " + Format.getTokenString(cmbUniformFormat.Text);
                if (cmbUniformFormat.Text == "decimal")
                {
                    if (cmbUniformSigned.Text == "unsigned")
                    {
                        txtFormat.Text = "* " + cmbUniformGroup.Text + " " + Format.getTokenString("unsigned");
                    }
                }


                if (txtFormat.Text == "* 1 h")
                {
                    txtFormat.Text = "%"; //slight optimization, the parser will skip some extra work
                }
            }
            else if (cmbFormatType.SelectedIndex == 1)
            {
                if ((bytes - bytes_used) < 0)
                {
                    lblCustomError.Visible = true;
                    return;
                }
                else
                {
                    lblCustomError.Visible = false;
                }
                foreach (FormatLine f in lstFormats.Items)
                {
                    txtFormat.Text += f.ToFormatString();
                }
            }
            else if (cmbFormatType.SelectedIndex == 2)
            {
                txtFormat.Text = "call " + txtExternalFile.Text;
            }

            MessageFormat message = new MessageFormat(
                versionBytes[1],
                versionBytes[0],
                IdBytes[1],
                IdBytes[0],
                Byte.Parse(txtLength.Text, System.Globalization.NumberStyles.HexNumber),
                txtName.Text.Trim(),
                txtFormat.Text);

            if (msgType == MessageType.Company)
            {
                if (cmbConnections.SelectedIndex < 0)
                {
                    lblCompanyError1.Visible = true;
                    return;
                }
                else
                {
                    lblCompanyError1.Visible = false;
                }
                Connection           c   = (Connection)cmbConnections.Items[cmbConnections.SelectedIndex];
                SqlMessageConnection sql = new SqlMessageConnection(c.connection);
                if (sql.Write(message) == false)
                {
                    lblCompanyError2.Visible = true;
                    return;
                }
                else
                {
                    lblCompanyError2.Visible = false;
                }
            }
            if (editIndex > -1)
            {
                parentForm.RemoveMessageFormat(editIndex);
            }
            parentForm.AddMessageFormat(message, msgType);
            this.Visible = false;
        }