Esempio n. 1
0
        private void GetComClick(int idx, string PortName)
        {
            String flagMessage = ComConnector.GetCom(PortName);

            DataGridViewCell statusCell = ComDataGrid.Rows[idx].Cells[IDX_CONSTATUS_COUNT];

            if (statusCell != null)
            {
                if (flagMessage == null)
                {
                    statusCell.Style.ForeColor = Color.Green;
                    String retError = ComConnector.sendToMachine(idx, PortName, ComConnector.GetHexValue(NONE));
                    if (retError != null)
                    {
                        MessageBox.Show(retError, "Error");
                        statusCell.Style.ForeColor = Color.DarkRed;
                        statusCell.Value           = "断开";
                    }
                    else
                    {
                        statusCell.Value = "连通";
                    }
                    return;
                }

                statusCell.Style.ForeColor = Color.DarkRed;
                statusCell.Value           = "断开";
                MessageBox.Show(flagMessage, "Error");
            }
        }
Esempio n. 2
0
        private bool ResetAL1(int idx)
        {
            String           PortName     = ComDataGrid.Rows[idx].Cells[IDX_PORT_NAME].Value.ToString();
            DataGridViewCell addressCell  = ComDataGrid.Rows[idx].Cells[IDX_ADDRESS_CODE];
            String           FunctionCode = addressCell.Value + " " + RESET_AL1;
            String           CrcCode      = ComConnector.ToModbusCRC16(FunctionCode);
            String           retError     = ComConnector.sendToMachine(idx, PortName,
                                                                       ComConnector.GetHexValue(FunctionCode + " " + CrcCode.Substring(0, 2) + " " + CrcCode.Substring(2, 2)));

            if (retError != null)
            {
                DataGridViewCell statusCell = ComDataGrid.Rows[idx].Cells[IDX_CONSTATUS_COUNT];

                MessageBox.Show(retError, "Error");
                statusCell.Style.ForeColor = Color.DarkRed;
                statusCell.Value           = "断开";
                ComErrorCount++;
                if (ComErrorCount > 8)
                {
                    MessageBox.Show("COM 连接次数过多,将关闭程序", "Error");
                    Environment.Exit(0);
                }

                return(false);
            }

            ComErrorCount = 0;
            ComDataGrid.Rows[idx].Cells[IDX_CURRENT_COUNT].Value = 0;
            return(true);
        }