private void button1_Click(object sender, EventArgs e)
        {
            this.lbState.Text = "";

            if (checkBox1.Checked)
            {
                if (MessageBox.Show("确定减少水电量?",
                                    "减少确认", MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
                {
                    return;
                }
            }
            else
            {
                if (MessageBox.Show("确定充值水电量?",
                                    "充值确认", MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
                {
                    return;
                }
            }

            pfu.getNew();

            getNew();

            if (this.lbCardType.Text.Trim() != InfoSys.CardTypeUser)
            {
                MessageBox.Show("非用户卡不可充值");
                return;
            }

            if (!CheckValue())
            {
                return;
            }

            double d_UnitPriceWater    = Tools.StringToDouble(UnitPriceWater, 0);
            double d_UnitPriceElectric = Tools.StringToDouble(UnitPriceElectric, 0);
            double d_NumberWater       = Tools.StringToDouble(WaterNum, 0);
            double d_NumberElectric    = Tools.StringToDouble(ElectricNum, 0);
            double d_WaterPrice        = Tools.StringToDouble(WaterPrice, 0);
            double d_ElectricPrice     = Tools.StringToDouble(ElectricPrice, 0);

            if (checkBox1.Checked)
            {
                d_NumberWater    = -d_NumberWater;
                d_NumberElectric = -d_NumberElectric;
                d_WaterPrice     = -d_WaterPrice;
                d_ElectricPrice  = -d_ElectricPrice;
            }

            double d_ResidualWater    = Tools.StringToDouble(this.txtResidualWater.Text, 0);
            double d_ResidualElectric = Tools.StringToDouble(this.txtResidualElectric.Text, 0);

            int i_ResidualWater_new = (int)((d_ResidualWater + d_NumberWater) * 10);

            if (i_ResidualWater_new < 0)
            {
                i_ResidualWater_new = 0;
            }
            if (i_ResidualWater_new > 99999999)
            {
                i_ResidualWater_new = 99999999;
            }
            int i_ResidualElectric_new = (int)((d_ResidualElectric + d_NumberElectric) * 10);

            if (i_ResidualElectric_new < 0)
            {
                i_ResidualElectric_new = 0;
            }
            if (i_ResidualElectric_new > 99999999)
            {
                i_ResidualElectric_new = 99999999;
            }


            //MessageBox.Show("可以充值");
            //保存远程服务器数据库
            string str = DataTransfer.RechargeCardUser(this.lbSerialNumber.Text.Trim(), this.txtUserNo.Text.Trim(),
                                                       this.txtUserName.Text.Trim(), this.txtIdentityNumber.Text.Trim(), this.txtTelephone.Text.Trim(),
                                                       d_WaterPrice.ToString(), d_NumberWater.ToString(), d_ElectricPrice.ToString(), d_NumberElectric.ToString(),
                                                       this.txtWaterUsed.Text, this.txtElectricUsed.Text, this.txtRemark.Text.Trim());
            JavaScriptObject result = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);

            if (!bool.Parse(result["Result"].ToString()))
            {
                string txt = result["Message"].ToString();
                MessageBox.Show(txt);
                pf.Log(txt);
                this.lbState.Text = txt;
                pf.Log(pf.getDateStr() + this.lbState.Text);
                return;
            }

            int    mode           = 4;            //以B密码认证
            int    sec            = 1;            //扇区
            int    block          = 0;
            string key            = pf.getKeyB(); //读卡密码
            string result_WriteIC = "";
            string result_AuthIC  = "";

            //设置扇区2内容
            sec = 2;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrUser, InfoSys.MethodModifyCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //写数据块0,剩余可用水量(4字节)剩余可用电量(4字节)累计用水量(4字节)累计用电量(4字节)
                block = 0;
                double d3 = Tools.StringToDouble(this.txtTotalWater.Text, 0);
                double d4 = Tools.StringToDouble(this.txtTotalElectric.Text, 0);
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block,
                                                    i_ResidualWater_new.ToString().PadLeft(8, '0') +
                                                    i_ResidualElectric_new.ToString().PadLeft(8, '0') +
                                                    d3.ToString().PadLeft(8, '0') +
                                                    d4.ToString().PadLeft(8, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = "写卡出错,充值失败!";
                    pf.Log(pf.getDateStr() + this.lbState.Text);
                    return;
                }
            }
            else
            {
                this.lbState.Text = "认证出错,充值失败!";
                pf.Log(pf.getDateStr() + this.lbState.Text);
                return;
            }

            this.lbState.Text = "充值成功!";
            getInfo();
            this.txtNumberWater.Text        = "0";
            this.txtTotalPriceWater.Text    = "0";
            this.txtNumberElectric.Text     = "0";
            this.txtTotalPriceElectric.Text = "0";
            pf.Log(pf.getDateStr() + this.lbState.Text + "充值后剩余水量:" + i_ResidualWater_new + ",充值后剩余电量:" + i_ResidualElectric_new);
            return;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (SerialNumber_old == "")
            {
                MessageBox.Show("请先读卡再注销卡!");
                return;
            }

            if (this.lbCardType.Text != InfoSys.CardTypeDevice)
            {
                MessageBox.Show("非" + InfoSys.CardTypeStrDevice + "无法注销!");
                return;
            }

            if (MessageBox.Show("确定" + InfoSys.CardTypeStrDevice + InfoSys.MethodCancelCard + "?",
                                InfoSys.CardTypeStrDevice + InfoSys.MethodCancelCard, MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
            {
                return;
            }

            pf.BeginLog(InfoSys.CardTypeStrDevice, InfoSys.MethodCancelCard);

            if (!pf.SeedIC())
            {
                this.lbCardType.Text = InfoSys.StrSeekFailure;
                pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodCancelCard);
                return;
            }

            this.lbSerialNumber.Text = pf.getSnr().ToString("X");

            if (SerialNumber_old != this.lbSerialNumber.Text)
            {
                MessageBox.Show("请重新读卡再注销卡!");
                pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodCancelCard);
                return;
            }

            //保存远程服务器数据库
            string           str    = DataTransfer.CancelCardDevice(this.lbSerialNumber.Text);
            JavaScriptObject result = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);

            if (!bool.Parse(result["Result"].ToString()))
            {
                string txt = result["Message"].ToString();
                MessageBox.Show(txt);
                pf.Log(txt);
                pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodCancelCard);
                return;
            }

            int    mode           = 0;            //以A密码认证
            string key            = pf.getKeyB(); //读卡密码
            string keyOld         = pf.getKeyA(); //读卡密码
            string result_WriteIC = "";
            string result_AuthIC  = "";

            for (int i = 0; i < pf.getSize(); i++)
            {
                result_AuthIC = CardCommon.AuthIC(icdev, mode, i, key);
                pf.AuthLog(InfoSys.CardTypeStrDevice, InfoSys.MethodCancelCard, i, result_AuthIC);
                if (result_AuthIC != InfoSys.StrAuthSuccess)
                {
                    continue;
                }
                else
                {
                    int begin = 0;
                    if (i == 0)
                    {
                        begin = 1;
                    }

                    for (int j = begin; j < 3; j++)
                    {
                        result_WriteIC = CardCommon.WriteIC(icdev, i, j, "".PadRight(32, '0'));
                        pf.Log("注销卡扇区:" + i + " 数据块:" + j + " 结果:" + (result_WriteIC == "" ? "成功" : result_WriteIC));
                    }

                    {
                        int block = 3;
                        result_WriteIC = CardCommon.WriteIC(icdev, i, block, keyOld + InfoSys.KeyControl + keyOld);
                        pf.Log("注销卡写密码扇区:" + i + " 数据块:" + block + " 结果:" + (result_WriteIC == "" ? "成功" : result_WriteIC));
                    }
                }
            }

            SerialNumber_old  = "";
            this.lbState.Text = InfoSys.MethodCancelCard + "结束";
            pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodCancelCard);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            clear();

            pf.BeginLog(InfoSys.CardTypeStrDevice, InfoSys.MethodReadCard);

            if (!pf.SeedIC())
            {
                this.lbCardType.Text = InfoSys.StrSeekFailure;
                pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodReadCard);
                return;
            }

            this.lbSerialNumber.Text = pf.getSnr().ToString("X");

            int    mode          = 4;            //以B密码认证
            int    sec           = 1;            //扇区
            int    block         = 0;
            string key           = pf.getKeyB(); //读卡密码
            string result_ReadIC = "";
            string result_AuthIC = "";

            //读取扇区1内容
            sec = 1;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrDevice, InfoSys.MethodReadCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //读数据块0,数据块0为卡类型(1字节)、行政区划码(3字节)、镇(乡)村编码(3字节)、测站编码(1字节)、年度可开采水量(4字节)、可用水量提醒值(2字节)、可用水量提醒值(2字节)
                block         = 0;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.lbCardType.Text = result_ReadIC.Substring(0, 2);
                    if (this.lbCardType.Text != InfoSys.CardTypeDevice)
                    {
                        this.lbState.Text = "非" + InfoSys.CardTypeStrDevice + "," + InfoSys.StrCannotRead;
                        pf.Log(this.lbState.Text);
                        pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodReadCard);
                        return;
                    }
                    this.txtAddressCode1.Text = result_ReadIC.Substring(2, 6);
                    this.txtAddressCode2.Text = result_ReadIC.Substring(8, 6);
                    int i_AddressCode3 = Convert.ToInt32("0x" + result_ReadIC.Substring(14, 2), 16);
                    this.txtAddressCode3.Text = i_AddressCode3.ToString();
                    ToolsForm.setTextTextBox(this.txtYearExploitation, result_ReadIC.Substring(16, 8), "0", 1);
                    try
                    {
                        ToolsForm.setTextTextBox(this.txtAlertAvailableWater, Convert.ToInt32("0x" + result_ReadIC.Substring(24, 4), 16).ToString(), "0", 0);
                    }
                    catch { }
                    try
                    {
                        ToolsForm.setTextTextBox(this.txtAlertAvailableElectric, Convert.ToInt32("0x" + result_ReadIC.Substring(28, 4), 16).ToString(), "0", 0);
                    }
                    catch { }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrDevice, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块1,数据块1为流量计类型(1字节)、电表脉冲数(2字节bcd码)、水位报警值(4字节)
                block         = 1;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    string s = result_ReadIC.Substring(0, 2);
                    this.cboTypeCode.SelectedIndex = 0;
                    foreach (object item in this.cboTypeCode.Items)
                    {
                        if (int.Parse(item.ToString().Split('-')[0]) == Convert.ToInt32("0x" + s, 16))
                        {
                            this.cboTypeCode.SelectedItem = item;
                            break;
                        }
                    }
                    this.txtMeterPulse.Text = result_ReadIC.Substring(2, 4).TrimStart('0');
                    ToolsForm.setTextTextBox(this.txtAlertWaterLevel, result_ReadIC.Substring(6, 8), "0", 1);
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrDevice, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块2,数据块2为站类型(1字节)、地址码(2字节)
                block         = 2;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    string s1 = result_ReadIC.Substring(0, 2);
                    this.comboBox1.SelectedIndex = 0;
                    foreach (object item in this.comboBox1.Items)
                    {
                        if (int.Parse(item.ToString().Split('-')[0]) == Convert.ToInt32("0x" + s1, 16))
                        {
                            this.comboBox1.SelectedItem = item;
                            break;
                        }
                    }

                    string s2 = result_ReadIC.Substring(2, 4);
                    try
                    {
                        this.txtStationCode.Text = Convert.ToInt32("0x" + s2, 16).ToString().TrimStart('0');
                    }
                    catch { }
                    if (this.txtStationCode.Text == "")
                    {
                        this.txtStationCode.Text = "0";
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrDevice, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodReadCard);
                    return;
                }
            }

            if (this.lbCardType.Text == InfoSys.CardTypeDevice)
            {
                SerialNumber_old  = this.lbSerialNumber.Text.Trim();
                this.lbState.Text = InfoSys.StrReadSuccess;
            }
            else
            {
                this.lbState.Text = "非" + InfoSys.CardTypeStrDevice + "," + InfoSys.StrCannotRead;
                MessageBox.Show(this.lbState.Text);
            }
            pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodReadCard);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            clear();

            pf.BeginLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);

            if (!pf.SeedIC())
            {
                this.lbCardType.Text = InfoSys.StrSeekFailure;
                pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                return;
            }

            this.lbSerialNumber.Text = pf.getSnr().ToString("X");

            int    mode          = 4;            //以B密码认证
            int    sec           = 1;            //扇区
            int    block         = 0;
            string key           = pf.getKeyB(); //读卡密码
            string result_ReadIC = "";
            string result_AuthIC = "";

            //读取扇区1内容
            sec = 1;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //读数据块0,数据块0为卡类型(1字节)
                block         = 0;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.lbCardType.Text = result_ReadIC.Substring(0, 2);
                    if (this.lbCardType.Text != InfoSys.CardTypeRead)
                    {
                        this.lbState.Text = "非" + InfoSys.CardTypeStrRead + "," + InfoSys.StrCannotRead;
                        pf.Log(this.lbState.Text);
                        pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                        return;
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块1,累计用水量(4字节),累计用电量(4字节),年可开采量(4字节),年剩余可开采量(4字节)
                block         = 1;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    ToolsForm.setTextLabel(this.label35, result_ReadIC.Substring(0, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label36, result_ReadIC.Substring(8, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label37, result_ReadIC.Substring(16, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label38, result_ReadIC.Substring(24, 8), "0", 1);
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块2,地址码(7字节),设备类型(1字节)
                block         = 2;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.label33.Text = result_ReadIC.Substring(0, 8);
                    this.label34.Text = result_ReadIC.Substring(8, 2);
                    if (this.label34.Text.Trim() == "00")
                    {
                        this.label34.Text += " - 单站";
                    }
                    else if (this.label34.Text.Trim() == "01")
                    {
                        this.label34.Text += " - 主站";
                    }
                    else if (this.label34.Text.Trim() == "02")
                    {
                        this.label34.Text += " - 从站";
                    }
                    else
                    {
                        this.label34.Text += " - 未知类型";
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }
            }

            //读取扇区2内容
            sec = 2;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //读数据块0,用户卡号(4字节),ic序列号(4字节),开泵剩余水量(4字节),开泵剩余电量(4字节)
                block         = 0;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.label39.Text = result_ReadIC.Substring(0, 8);
                    this.label40.Text = result_ReadIC.Substring(8, 8);
                    ToolsForm.setTextLabel(this.label43, result_ReadIC.Substring(16, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label44, result_ReadIC.Substring(24, 8), "0", 1);
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块1,本次用水量(4字节),本次用电量(4字节),关泵剩余水量(4字节),关泵剩余电量(4字节)
                block         = 1;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    ToolsForm.setTextLabel(this.label49, result_ReadIC.Substring(0, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label50, result_ReadIC.Substring(8, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label47, result_ReadIC.Substring(16, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label48, result_ReadIC.Substring(24, 8), "0", 1);
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块1,开泵时间(6字节),关泵时间(6字节),记录类型(1字节),开泵类型(1字节),关泵类型(1字节)
                block         = 2;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.label41.Text = result_ReadIC.Substring(0, 12);
                    this.label45.Text = result_ReadIC.Substring(12, 12);
                    this.label51.Text = result_ReadIC.Substring(24, 2);
                    if (this.label51.Text.Trim() == "00")
                    {
                        this.label51.Text += " - 正常扣费";
                    }
                    else if (this.label51.Text.Trim() == "01")
                    {
                        this.label51.Text += " - 未扣费";
                    }
                    else if (this.label51.Text.Trim() == "02")
                    {
                        this.label51.Text += " - 免费";
                    }
                    else
                    {
                        this.label51.Text += " - 未知";
                    }
                    this.label42.Text = result_ReadIC.Substring(26, 2);
                    if (this.label42.Text.Trim() == "01")
                    {
                        this.label42.Text += " - 刷卡开泵";
                    }
                    else if (this.label42.Text.Trim() == "02")
                    {
                        this.label42.Text += " - 手动开泵";
                    }
                    else if (this.label42.Text.Trim() == "03")
                    {
                        this.label42.Text += " - 远程GPRS开泵";
                    }
                    else if (this.label42.Text.Trim() == "04")
                    {
                        this.label42.Text += " - 远程短信开泵";
                    }
                    else
                    {
                        this.label42.Text += " - 未知";
                    }
                    this.label46.Text = result_ReadIC.Substring(28, 2);
                    if (this.label46.Text.Trim() == "01")
                    {
                        this.label46.Text += " - 刷卡关泵";
                    }
                    else if (this.label46.Text.Trim() == "02")
                    {
                        this.label46.Text += " - 手动关泵";
                    }
                    else if (this.label46.Text.Trim() == "03")
                    {
                        this.label46.Text += " - 远程GPRS关泵";
                    }
                    else if (this.label46.Text.Trim() == "04")
                    {
                        this.label46.Text += " - 远程短信关泵";
                    }
                    else if (this.label46.Text.Trim() == "05")
                    {
                        this.label46.Text += " - 欠费关泵";
                    }
                    else
                    {
                        this.label46.Text += " - 未知";
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }
            }

            //读取扇区3内容
            sec = 3;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //读数据块0,用户卡号(4字节),ic序列号(4字节),开泵剩余水量(4字节),开泵剩余电量(4字节)
                block         = 0;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.label52.Text = result_ReadIC.Substring(0, 8);
                    this.label53.Text = result_ReadIC.Substring(8, 8);
                    ToolsForm.setTextLabel(this.label56, result_ReadIC.Substring(16, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label57, result_ReadIC.Substring(24, 8), "0", 1);
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块1,本次用水量(4字节),本次用电量(4字节),关泵剩余水量(4字节),关泵剩余电量(4字节)
                block         = 1;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    ToolsForm.setTextLabel(this.label62, result_ReadIC.Substring(0, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label63, result_ReadIC.Substring(8, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label60, result_ReadIC.Substring(16, 8), "0", 1);
                    ToolsForm.setTextLabel(this.label61, result_ReadIC.Substring(24, 8), "0", 1);
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块1,开泵时间(6字节),关泵时间(6字节),记录类型(1字节),开泵类型(1字节),关泵类型(1字节)
                block         = 2;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.label54.Text = result_ReadIC.Substring(0, 12);
                    this.label58.Text = result_ReadIC.Substring(12, 12);
                    this.label64.Text = result_ReadIC.Substring(24, 2);
                    if (this.label64.Text.Trim() == "00")
                    {
                        this.label64.Text += " - 正常扣费";
                    }
                    else if (this.label64.Text.Trim() == "01")
                    {
                        this.label64.Text += " - 未扣费";
                    }
                    else if (this.label64.Text.Trim() == "02")
                    {
                        this.label64.Text += " - 免费";
                    }
                    else
                    {
                        this.label64.Text += " - 未知";
                    }
                    this.label55.Text = result_ReadIC.Substring(26, 2);
                    if (this.label55.Text.Trim() == "01")
                    {
                        this.label55.Text += " - 刷卡开泵";
                    }
                    else if (this.label55.Text.Trim() == "02")
                    {
                        this.label55.Text += " - 手动开泵";
                    }
                    else if (this.label55.Text.Trim() == "03")
                    {
                        this.label55.Text += " - 远程GPRS开泵";
                    }
                    else if (this.label55.Text.Trim() == "04")
                    {
                        this.label55.Text += " - 远程短信开泵";
                    }
                    else
                    {
                        this.label55.Text += " - 未知";
                    }
                    this.label59.Text = result_ReadIC.Substring(28, 2);
                    if (this.label59.Text.Trim() == "01")
                    {
                        this.label59.Text += " - 刷卡关泵";
                    }
                    else if (this.label59.Text.Trim() == "02")
                    {
                        this.label59.Text += " - 手动关泵";
                    }
                    else if (this.label59.Text.Trim() == "03")
                    {
                        this.label59.Text += " - 远程GPRS关泵";
                    }
                    else if (this.label59.Text.Trim() == "04")
                    {
                        this.label59.Text += " - 远程短信关泵";
                    }
                    else if (this.label59.Text.Trim() == "05")
                    {
                        this.label59.Text += " - 欠费关泵";
                    }
                    else
                    {
                        this.label59.Text += " - 未知";
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
                    return;
                }
            }

            if (this.lbCardType.Text == InfoSys.CardTypeRead)
            {
                SerialNumber_old  = this.lbSerialNumber.Text.Trim();
                this.lbState.Text = InfoSys.StrReadSuccess;
            }
            else
            {
                this.lbState.Text = "非" + InfoSys.CardTypeStrRead + "," + InfoSys.StrCannotRead;
                MessageBox.Show(this.lbState.Text);
            }
            pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodReadCard);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (SerialNumber_old == "")
            {
                MessageBox.Show("请先读卡再修改卡!");
                return;
            }

            if (MessageBox.Show("确定" + InfoSys.CardTypeStrDevice + InfoSys.MethodModifyCard + "?",
                                InfoSys.CardTypeStrDevice + InfoSys.MethodModifyCard, MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
            {
                return;
            }

            pf.BeginLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard);

            if (!CheckValue())
            {
                pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard);
                return;
            }

            if (!pf.SeedIC())
            {
                this.lbCardType.Text = InfoSys.StrSeekFailure;
                pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard);
                return;
            }

            this.lbSerialNumber.Text = pf.getSnr().ToString("X");

            if (SerialNumber_old != this.lbSerialNumber.Text)
            {
                MessageBox.Show("请重新读卡再修改卡!");
                pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard);
                return;
            }

            //保存远程服务器数据库
            string str = DataTransfer.ModifyCardDevice(this.lbSerialNumber.Text, AddressCode1, AddressCode2, AddressCode3,
                                                       YearExploitation, AlertAvailableWater, AlertAvailableElectric, TypeCode, MeterPulse, AlertWaterLevel, StationType, StationCode, Frequency);
            JavaScriptObject result = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);

            if (!bool.Parse(result["Result"].ToString()))
            {
                string txt = result["Message"].ToString();
                MessageBox.Show(txt);
                pf.Log(txt);
                pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard);
                return;
            }

            int    mode           = 4;            //以B密码认证
            int    sec            = 1;            //扇区
            int    block          = 0;
            string key            = pf.getKeyB(); //读卡密码
            string result_WriteIC = "";
            string result_AuthIC  = "";

            //设置扇区1内容
            sec = 1;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //写数据块0,数据块0为卡类型(1字节)、行政区划码(3字节)、镇(乡)村编码(3字节)、测站编码(1字节)、年度可开采水量(4字节)、可用水量提醒值(2字节)、可用水量提醒值(2字节)
                block = 0;
                string hex_AddressCode3           = int.Parse(AddressCode3).ToString("X").PadLeft(2, '0');
                double d_YearExploitation         = Tools.StringToDoubleMultiply10(YearExploitation, 0);
                string hex_AlertAvailableWater    = int.Parse(AlertAvailableWater).ToString("X").PadLeft(4, '0');
                string hex_AlertAvailableElectric = int.Parse(AlertAvailableElectric).ToString("X").PadLeft(4, '0');
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, InfoSys.CardTypeDevice + AddressCode1 + AddressCode2 +
                                                    hex_AddressCode3 + d_YearExploitation.ToString().PadLeft(8, '0') + hex_AlertAvailableWater + hex_AlertAvailableElectric);
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrDevice, InfoSys.StrModifyFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard);
                    return;
                }

                //写数据块1,数据块1为流量计类型(1字节)、电表脉冲数(2字节bcd码)、水位报警值(4字节)
                block = 1;
                string hex_TypeCode      = int.Parse(TypeCode).ToString("X").PadLeft(2, '0');
                double d_AlertWaterLevel = Tools.StringToDoubleMultiply10(AlertWaterLevel, 0);
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, (hex_TypeCode +
                                                                        MeterPulse.ToString().PadLeft(4, '0') +
                                                                        d_AlertWaterLevel.ToString().PadLeft(8, '0')).PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrDevice, InfoSys.StrModifyFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard);
                    return;
                }

                //写数据块2,数据块2为站类型(1字节)、地址码(2字节)
                block = 2;
                string hex_StationType = int.Parse(StationType).ToString("X").PadLeft(2, '0');
                string hex_StationCode = int.Parse(StationCode).ToString("X").PadLeft(4, '0');
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, (hex_StationType + hex_StationCode).PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrDevice, InfoSys.StrOpenFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodOpenCard);
                    return;
                }
            }

            SerialNumber_old     = "";
            this.lbCardType.Text = InfoSys.CardTypeDevice;
            this.lbState.Text    = InfoSys.StrModifySuccess;
            pf.Log(this.lbState.Text);
            pf.EndLog(InfoSys.CardTypeStrDevice, InfoSys.MethodModifyCard);
        }
Beispiel #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            clear();

            pf.BeginLog(InfoSys.CardTypeStrHigh, InfoSys.MethodReadCard);

            if (!pf.SeedIC())
            {
                this.lbCardType.Text = InfoSys.StrSeekFailure;
                pf.EndLog(InfoSys.CardTypeStrHigh, InfoSys.MethodReadCard);
                return;
            }

            this.lbSerialNumber.Text = pf.getSnr().ToString("X");

            int    mode          = 4;            //以B密码认证
            int    sec           = 1;            //扇区
            int    block         = 0;
            string key           = pf.getKeyB(); //读卡密码
            string result_ReadIC = "";
            string result_AuthIC = "";

            //读取扇区1内容
            sec = 1;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrHigh, InfoSys.MethodReadCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //读数据块0,数据块0为卡类型(1字节)
                block         = 0;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.lbCardType.Text = result_ReadIC.Substring(0, 2);
                    if (this.lbCardType.Text != InfoSys.CardTypeHigh)
                    {
                        this.lbState.Text = "非" + InfoSys.CardTypeStrHigh + "," + InfoSys.StrCannotRead;
                        pf.Log(this.lbState.Text);
                        pf.EndLog(InfoSys.CardTypeStrHigh, InfoSys.MethodReadCard);
                        return;
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrHigh, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrHigh, InfoSys.MethodOpenCard);
                    return;
                }
            }

            if (this.lbCardType.Text == InfoSys.CardTypeHigh)
            {
                SerialNumber_old  = this.lbSerialNumber.Text.Trim();
                this.lbState.Text = InfoSys.StrReadSuccess;
            }
            else
            {
                this.lbState.Text = "非" + InfoSys.CardTypeStrHigh + "," + InfoSys.StrCannotRead;
                MessageBox.Show(this.lbState.Text);
            }
            pf.EndLog(InfoSys.CardTypeStrHigh, InfoSys.MethodReadCard);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定" + InfoSys.CardTypeStrRead + InfoSys.MethodOpenCard + "?",
                                InfoSys.CardTypeStrRead + InfoSys.MethodOpenCard, MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
            {
                return;
            }

            pf.BeginLog(InfoSys.CardTypeStrRead, InfoSys.MethodOpenCard);

            if (!CheckValue())
            {
                pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodOpenCard);
                return;
            }

            if (!pf.SeedIC())
            {
                this.lbCardType.Text = InfoSys.StrSeekFailure;
                pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodOpenCard);
                return;
            }

            this.lbSerialNumber.Text = pf.getSnr().ToString("X");

            //保存远程服务器数据库
            string           str    = DataTransfer.OpenCardRead(this.lbSerialNumber.Text, "", "", "0");
            JavaScriptObject result = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);

            if (!bool.Parse(result["Result"].ToString()))
            {
                string txt = result["Message"].ToString();
                MessageBox.Show(txt);
                pf.Log(txt);
                pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodOpenCard);
                return;
            }

            int    mode           = 4;            //以B密码认证
            int    sec            = 1;            //扇区
            int    block          = 0;
            string key            = pf.getKeyA(); //读卡密码
            string keyNew         = pf.getKeyB(); //读卡密码
            string result_WriteIC = "";
            string result_AuthIC  = "";

            //设置扇区1内容
            sec = 1;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrRead, InfoSys.MethodOpenCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //写数据块0,数据块0为卡类型(1字节)
                block          = 0;
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, (InfoSys.CardTypeRead).PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrRead, InfoSys.StrOpenFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodOpenCard);
                    return;
                }
            }
            else
            {
                this.lbState.Text = InfoSys.StrCannotOpen + InfoSys.StrOpenFailure;
                pf.Log(this.lbState.Text);
                pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodOpenCard);
                return;
            }

            for (int i = 0; i < pf.getSize(); i++)
            {
                result_AuthIC = CardCommon.AuthIC(icdev, mode, i, key);
                if (result_AuthIC == InfoSys.StrAuthSuccess)
                {
                    //写数据块3,密码eeeeeeeeeeee
                    block = 3;
                    CardCommon.WritePWD(icdev, i, block, keyNew, InfoSys.KeyControl, keyNew);
                }
            }

            SerialNumber_old     = "";
            this.lbCardType.Text = InfoSys.CardTypeRead;
            this.lbState.Text    = InfoSys.StrOpenSuccess;
            pf.Log(this.lbState.Text);
            pf.EndLog(InfoSys.CardTypeStrRead, InfoSys.MethodOpenCard);
        }
        private void btnReadIC_Click(object sender, EventArgs e)
        {
            clearLog();
            this.groupBox6.Visible = false;
            this.groupBox4.Visible = true;
            Log(getDateStr() + "开始读卡");
            if (connectIC)
            {
                Log("可以读卡");

                if (!SeedIC())
                {
                    Log("1 寻卡失败");
                    Log(getDateStr() + "结束读卡");
                    return;
                }

                bool flag = false;
                for (int j = 0; j < size; j++)
                {
                    string result_AuthIC = CardCommon.AuthIC(icdev, 0, j, getKeyB());
                    Log("1 读卡扇区:" + j + " 认证状态:" + result_AuthIC);
                    if (result_AuthIC == InfoSys.StrAuthSuccess)
                    {
                        if (!flag)
                        {
                            flag = true;
                        }
                        for (int k = 0; k < 4; k++)
                        {
                            Log("1 读卡扇区:" + j + " 数据块:" + k + " 结果:" + CardCommon.ReadIC(icdev, j, k));
                        }
                    }
                }

                if (!flag)
                {
                    Log("第一次读取失败,开始第二次读取");
                    if (!SeedIC())
                    {
                        Log("2 寻卡失败");
                        Log(getDateStr() + "结束读卡");
                        return;
                    }

                    for (int j = 0; j < size; j++)
                    {
                        string result_AuthIC = CardCommon.AuthIC(icdev, 0, j, getKeyA());
                        Log("2 读卡扇区:" + j + " 认证状态:" + result_AuthIC);
                        if (result_AuthIC == InfoSys.StrAuthSuccess)
                        {
                            for (int k = 0; k < 4; k++)
                            {
                                Log("2 读卡扇区:" + j + " 数据块:" + k + " 结果:" + CardCommon.ReadIC(icdev, j, k));
                            }
                        }
                    }
                }
            }
            else
            {
                Log("无法读卡,确保打开串口成功!");
            }

            Log(getDateStr() + "结束读卡");
        }
        private void btnReOpen_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("请选择一行记录!");
                return;
            }
            string SerialNumber   = this.dataGridView1.SelectedRows[0].Cells["SerialNumber"].Value.ToString();
            string UserNo         = this.dataGridView1.SelectedRows[0].Cells["UserNo"].Value.ToString();
            string UserName       = this.dataGridView1.SelectedRows[0].Cells["UserName"].Value.ToString();
            string IdentityNumber = this.dataGridView1.SelectedRows[0].Cells["IdentityNumber"].Value.ToString();

            byte[] newUserName = null;
            newUserName = HexStringUtility.StrToByteArray("".PadRight(16, ' '));

            string DeviceList = this.dataGridView1.SelectedRows[0].Cells["DeviceList"].Value.ToString();

            string ResidualWater    = this.dataGridView1.SelectedRows[0].Cells["ResidualWater"].Value.ToString();
            string ResidualElectric = this.dataGridView1.SelectedRows[0].Cells["ResidualElectric"].Value.ToString();
            string TotalWater       = this.dataGridView1.SelectedRows[0].Cells["TotalWater"].Value.ToString();
            string TotalElectric    = this.dataGridView1.SelectedRows[0].Cells["TotalElectric"].Value.ToString();
            string showInfo         = "确定重新开卡当前选择用户卡?" + Environment.NewLine + "卡号:" + UserNo + ",用户名:" + UserName +
                                      Environment.NewLine + "身份证号:" + IdentityNumber;

            if ((MessageBox.Show(showInfo, "重新开卡", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) == DialogResult.Yes)
            {
                Log(getDateStr() + "开始重新开卡");

                if (!SeedIC())
                {
                    Log("寻卡失败");
                    Log(getDateStr() + "结束重新开卡");
                    return;
                }

                string SerialNumberNew = getSnr().ToString("X");

                //保存远程服务器数据库
                string           str    = DataTransfer.ReOpenCardUser(SerialNumber, SerialNumberNew);
                JavaScriptObject result = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);
                if (bool.Parse(result["Result"].ToString()))
                {
                    MessageBox.Show("取消挂失成功!");
                    SearchCardUser();
                }
                else
                {
                    string txt = result["Message"].ToString();
                    MessageBox.Show(txt);
                    myLogger.Error(txt);
                    Log(txt);
                    Log(getDateStr() + "结束重新开卡");
                    return;
                }

                int    mode           = 4;         //以B密码认证
                int    sec            = 1;         //扇区
                int    block          = 0;
                string key            = getKeyA(); //读卡密码
                string result_WriteIC = "";
                string result_AuthIC  = "";

                //设置扇区1内容
                sec = 1;
                //认证卡密码B
                result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
                if (result_AuthIC == InfoSys.StrAuthSuccess)
                {
                    //写数据块0,数据块0为卡类型(1字节)、用户卡号(4字节)
                    block          = 0;
                    result_WriteIC = CardCommon.WriteIC(icdev, sec, block, (InfoSys.CardTypeUser + UserNo.PadLeft(8, '0')).PadRight(32, '0'));
                    if (result_WriteIC != "")
                    {
                        Log("重新写卡扇区" + sec + "数据块" + block + "写入失败");
                        Log(getDateStr() + "结束重新开卡");
                        return;
                    }

                    //写数据块1,数据块1为用户名(16字节)
                    block          = 1;
                    result_WriteIC = CardCommon.WriteIC(icdev, sec, block, HexStringUtility.ByteArrayToHexString(newUserName));
                    if (result_WriteIC != "")
                    {
                        Log("重新写卡扇区" + sec + "数据块" + block + "写入失败");
                        Log(getDateStr() + "结束重新开卡");
                        return;
                    }

                    //写数据块2,数据块2为身份证号(9字节)、联系电话(6字节)
                    block          = 2;
                    result_WriteIC = CardCommon.WriteIC(icdev, sec, block, (IdentityNumber + "0" + Telephone).PadRight(32, '0'));
                    if (result_WriteIC != "")
                    {
                        Log("重新写卡扇区" + sec + "数据块" + block + "写入失败");
                        Log(getDateStr() + "结束重新开卡");
                        return;
                    }

                    //写数据块3,密码eeeeeeeeeeee
                    block = 3;
                    CardCommon.WritePWD(icdev, sec, block, getKeyA(), InfoSys.KeyControl, getKeyB());
                }
                else
                {
                    Log("重新写卡扇区" + sec + "数据块" + block + "写入失败");
                    Log(getDateStr() + "结束重新开卡");
                    return;
                }

                //设置扇区2内容
                sec = 2;
                //认证卡密码B
                result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
                AuthLog(InfoSys.CardTypeStrUser, InfoSys.MethodOpenCard, sec, result_AuthIC);
                if (result_AuthIC == InfoSys.StrAuthSuccess)
                {
                    //写数据块0,剩余可用水量(4字节)剩余可用电量(4字节)累计用水量(4字节)累计用电量(4字节)
                    block = 0;
                    double d1 = Tools.StringToDouble(ResidualWater, 0);
                    double d2 = Tools.StringToDouble(ResidualElectric, 0);
                    double d3 = Tools.StringToDouble(TotalWater, 0);
                    double d4 = Tools.StringToDouble(TotalElectric, 0);
                    result_WriteIC = CardCommon.WriteIC(icdev, sec, block,
                                                        d1.ToString().PadLeft(8, '0') +
                                                        d2.ToString().PadLeft(8, '0') +
                                                        d3.ToString().PadLeft(8, '0') +
                                                        d4.ToString().PadLeft(8, '0'));
                    if (result_WriteIC != "")
                    {
                        Log("重新写卡扇区" + sec + "数据块" + block + "写入失败");
                        Log(getDateStr() + "结束重新开卡");
                        return;
                    }

                    string[] s           = { "", "", "", "" };
                    string[] DeviceLists = DeviceList.Split(',');

                    for (int i = 0; i < DeviceLists.Length; i++)
                    {
                        s[i] = DeviceLists[i];
                    }

                    string data = "";
                    //写数据块1,地址码1(7字节)地址码2(7字节)
                    block          = 1;
                    data           = (s[0] + s[1]).PadRight(32, '0');
                    result_WriteIC = CardCommon.WriteIC(icdev, sec, block, data);
                    if (result_WriteIC != "")
                    {
                        Log("重新写卡扇区" + sec + "数据块" + block + "写入失败");
                        Log(getDateStr() + "结束重新开卡");
                        return;
                    }

                    //写数据块2,地址码1(7字节)地址码2(7字节)
                    block          = 2;
                    data           = (s[2] + s[3]).PadRight(32, '0');
                    result_WriteIC = CardCommon.WriteIC(icdev, sec, block, data);
                    if (result_WriteIC != "")
                    {
                        Log("重新写卡扇区" + sec + "数据块" + block + "写入失败");
                        Log(getDateStr() + "结束重新开卡");
                        return;
                    }

                    //写数据块3,密码eeeeeeeeeeee
                    block = 3;
                    CardCommon.WritePWD(icdev, sec, block, getKeyA(), InfoSys.KeyControl, getKeyB());
                }

                Log("重新开卡完成");
                Log(getDateStr() + "结束重新开卡");
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Log(getDateStr() + "检测卡开始");
            if (connectIC)
            {
                if (!SeedIC())
                {
                    Log("寻卡失败");
                    MessageBox.Show("寻卡失败!");
                    Log(getDateStr() + "检测卡结束");
                    return;
                }

                int    mode          = 4;         //以B密码认证
                int    sec           = 1;         //扇区
                int    block         = 0;
                string keyA          = getKeyA(); //读卡密码
                string keyB          = getKeyB(); //读卡密码
                string result_ReadIC = "";
                string result_AuthIC = "";

                //读取扇区1内容
                sec = 1;
                //认证卡密码B,认证成功为非新卡
                result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, keyB);
                if (result_AuthIC == InfoSys.StrAuthSuccess)
                {
                    //读取数据块0
                    block         = 0;
                    result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                    if (result_ReadIC.Length == 32)
                    {
                        string CardType = result_ReadIC.Substring(0, 2);
                        if (CardType == InfoSys.CardTypeUser)
                        {
                            Log("此卡为" + InfoSys.CardTypeStrUser);
                            MessageBox.Show("此卡为" + InfoSys.CardTypeStrUser);
                        }
                        else if (CardType == InfoSys.CardTypeDevice)
                        {
                            Log("此卡为" + InfoSys.CardTypeStrDevice);
                            MessageBox.Show("此卡为" + InfoSys.CardTypeStrDevice);
                        }
                        else if (CardType == InfoSys.CardTypeRead)
                        {
                            Log("此卡为" + InfoSys.CardTypeStrRead);
                            MessageBox.Show("此卡为" + InfoSys.CardTypeStrRead);
                        }
                        else if (CardType == InfoSys.CardTypeClear)
                        {
                            Log("此卡为" + InfoSys.CardTypeStrClear);
                            MessageBox.Show("此卡为" + InfoSys.CardTypeStrClear);
                        }
                        else if (CardType == InfoSys.CardTypeNetSet)
                        {
                            Log("此卡为" + InfoSys.CardTypeStrNetSet);
                            MessageBox.Show("此卡为" + InfoSys.CardTypeStrNetSet);
                        }
                        else
                        {
                            Log("卡类型未知:" + CardType);
                            MessageBox.Show("卡类型未知:" + CardType);
                        }
                    }
                    else
                    {
                        Log("卡类型数据获取失败!");
                        MessageBox.Show("卡类型数据获取失败!");
                    }
                }
                else
                {
                    Log("重新寻卡进行新卡认证");
                    if (!SeedIC())
                    {
                        Log("寻卡失败");
                        Log(getDateStr() + "检测卡结束");
                        return;
                    }

                    //新卡检测
                    result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, keyA);
                    if (result_AuthIC == InfoSys.StrAuthSuccess)
                    {
                        block         = 0;
                        result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                        if (result_ReadIC.Length == 32)
                        {
                            Log("此卡为新卡");
                            MessageBox.Show("此卡为新卡!");
                        }
                        else
                        {
                            Log("新卡检测失败!");
                            MessageBox.Show("新卡检测失败!");
                        }
                    }
                    else
                    {
                        Log("新卡认证失败!");
                        MessageBox.Show("新卡认证失败!");
                    }
                }
            }
            else
            {
                Log("无法检测卡,确保打开串口成功!");
                MessageBox.Show("无法检测卡,确保打开串口成功!");
            }
            Log(getDateStr() + "检测卡结束");
        }
        private void btnInit_Click(object sender, EventArgs e)
        {
            if (!(MessageBox.Show("确定注销卡?", "注销卡", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes))
            {
                return;
            }

            Log(getDateStr() + "开始注销");
            if (connectIC)
            {
                if (!SeedIC())
                {
                    Log("寻卡失败");
                    Log(getDateStr() + "结束注销");
                    return;
                }

                bool flag = false;
                for (int j = 0; j < size; j++)
                {
                    string result_AuthIC = CardCommon.AuthIC(icdev, 0, j, getKeyB());
                    Log("注销卡扇区:" + j + " 认证状态:" + result_AuthIC);
                    if (result_AuthIC != InfoSys.StrAuthSuccess)
                    {
                        continue;
                    }
                    else
                    {
                        if (!flag)
                        {
                            flag = true;
                        }
                        int begin = 0;
                        if (j == 0)
                        {
                            begin = 1;
                        }

                        for (int k = begin; k < 3; k++)
                        {
                            string result = CardCommon.WriteIC(icdev, j, k, "".PadRight(32, '0'));
                            Log("注销卡扇区:" + j + " 数据块:" + k + " 结果:" + (result == "" ? "成功" : result));
                        }

                        {
                            int    block  = 3;
                            string result = CardCommon.WriteIC(icdev, j, block, getKeyA() + InfoSys.KeyControl + getKeyA());
                            Log("注销卡写密码扇区:" + j + " 数据块:" + block + " 结果:" + (result == "" ? "成功" : result));
                        }
                    }
                }

                if (!flag)
                {
                    Log("第一次注销失败,开始第二次注销");
                    if (!SeedIC())
                    {
                        Log("寻卡失败");
                        Log(getDateStr() + "结束注销");
                        return;
                    }

                    for (int j = 0; j < size; j++)
                    {
                        string result_AuthIC = CardCommon.AuthIC(icdev, 0, j, getKeyA());
                        Log("注销卡扇区:" + j + " 认证状态:" + result_AuthIC);
                        if (result_AuthIC != InfoSys.StrAuthSuccess)
                        {
                            continue;
                        }
                        else
                        {
                            if (!flag)
                            {
                                flag = true;
                            }
                            int begin = 0;
                            if (j == 0)
                            {
                                begin = 1;
                            }

                            for (int k = begin; k < 3; k++)
                            {
                                string result = CardCommon.WriteIC(icdev, j, k, "".PadRight(32, '0'));
                                Log("注销卡扇区:" + j + " 数据块:" + k + " 结果:" + (result == "" ? "成功" : result));
                            }

                            {
                                int    block  = 3;
                                string result = CardCommon.WriteIC(icdev, j, block, getKeyA() + InfoSys.KeyControl + getKeyA());
                                Log("注销卡写密码扇区:" + j + " 数据块:" + block + " 结果:" + (result == "" ? "成功" : result));
                            }
                        }
                    }
                }
            }
            else
            {
                Log("无法注销!确保打开串口成功!");
            }

            Log(getDateStr() + "结束注销");
        }
 public static string WritePWD(int icdev, int sec, int block, string keyA, string keyControl, string keyB)
 {
     return(CardCommon.WriteIC(icdev, sec, 3, keyA + keyControl + keyB));
 }
        private void button1_Click(object sender, EventArgs e)
        {
            clear();

            pf.BeginLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);

            if (!pf.SeedIC())
            {
                this.lbCardType.Text = InfoSys.StrSeekFailure;
                pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);
                return;
            }

            this.lbSerialNumber.Text = pf.getSnr().ToString("X");

            int    mode          = 4;            //以B密码认证
            int    sec           = 1;            //扇区
            int    block         = 0;
            string key           = pf.getKeyB(); //读卡密码
            string result_ReadIC = "";
            string result_AuthIC = "";

            int len_IP       = 0;
            int len_Port     = 0;
            int len_APN      = 0;
            int len_UserName = 0;
            int len_Password = 0;

            //读取扇区1内容
            sec = 1;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //读数据块0,数据块0为卡类型(1字节)、是否域名访问(1字节)、有效IP地址长度(1字节)、有效端口号长度(1字节)
                //APN名称长度(1字节)、APN用户名长度(1字节)、APN密码长度(1字节)
                block         = 0;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    this.lbCardType.Text = result_ReadIC.Substring(0, 2);
                    if (this.lbCardType.Text != InfoSys.CardTypeNetSet)
                    {
                        this.lbState.Text = "非" + InfoSys.CardTypeStrNetSet + "," + InfoSys.StrCannotRead;
                        pf.Log(this.lbState.Text);
                        pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);
                        return;
                    }

                    IsDomain_old             = result_ReadIC.Substring(2, 2);
                    this.ckbIsDomain.Checked = IsDomain_old == "01";

                    len_IP       = int.Parse(result_ReadIC.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
                    len_Port     = int.Parse(result_ReadIC.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
                    len_APN      = int.Parse(result_ReadIC.Substring(8, 2), System.Globalization.NumberStyles.HexNumber);
                    len_UserName = int.Parse(result_ReadIC.Substring(10, 2), System.Globalization.NumberStyles.HexNumber);
                    len_Password = int.Parse(result_ReadIC.Substring(12, 2), System.Globalization.NumberStyles.HexNumber);
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块1,IP地址或域名地址
                block         = 1;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    if (len_IP > 0)
                    {
                        string v = result_ReadIC.Substring(0, len_IP * 2);
                        this.txtIP.Text = HexStringUtility.ByteArrayToStr(HexStringUtility.HexStringToByteArray(v));
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块2,端口号(5字节)
                block         = 2;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    if (len_Port > 0)
                    {
                        string v = result_ReadIC.Substring(0, len_Port * 2);
                        this.txtPort.Text = HexStringUtility.ByteArrayToStr(HexStringUtility.HexStringToByteArray(v));
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);
                    return;
                }
            }

            //读取扇区2内容
            sec = 2;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //读数据块0,APN名称
                block         = 0;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    if (len_APN > 0)
                    {
                        string v = result_ReadIC.Substring(0, len_APN * 2);
                        this.txtAPN.Text = HexStringUtility.ByteArrayToStr(HexStringUtility.HexStringToByteArray(v));
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块1,用户名
                block         = 1;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    if (len_UserName > 0)
                    {
                        string v = result_ReadIC.Substring(0, len_UserName * 2);
                        this.txtUserName.Text = HexStringUtility.ByteArrayToStr(HexStringUtility.HexStringToByteArray(v));
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);
                    return;
                }

                //读数据块2,密码
                block         = 2;
                result_ReadIC = CardCommon.ReadIC(icdev, sec, block);
                if (result_ReadIC.Length == 32)
                {
                    if (len_Password > 0)
                    {
                        string v = result_ReadIC.Substring(0, len_Password * 2);
                        this.txtPassword.Text = HexStringUtility.ByteArrayToStr(HexStringUtility.HexStringToByteArray(v));
                    }
                }
                else
                {
                    this.lbState.Text = InfoSys.InfoReadFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrReadFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodReadCard);
                    return;
                }
            }

            if (this.lbCardType.Text == InfoSys.CardTypeNetSet)
            {
                SerialNumber_old  = this.lbSerialNumber.Text.Trim();
                this.lbState.Text = InfoSys.StrReadSuccess;
            }
            else
            {
                this.lbState.Text = "非" + InfoSys.CardTypeStrNetSet + "," + InfoSys.StrCannotRead;
                MessageBox.Show(this.lbState.Text);
            }
            pf.EndLog(InfoSys.CardTypeNetSet, InfoSys.MethodReadCard);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (SerialNumber_old == "")
            {
                MessageBox.Show("请先读卡再修改卡!");
                return;
            }

            if (MessageBox.Show("确定" + InfoSys.CardTypeStrNetSet + InfoSys.MethodModifyCard + "?",
                                InfoSys.CardTypeStrNetSet + InfoSys.MethodModifyCard, MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes)
            {
                return;
            }

            pf.BeginLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);

            if (!CheckValue())
            {
                pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                return;
            }

            if (!pf.SeedIC())
            {
                this.lbCardType.Text = InfoSys.StrSeekFailure;
                pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                return;
            }

            this.lbSerialNumber.Text = pf.getSnr().ToString("X");

            if (SerialNumber_old != this.lbSerialNumber.Text)
            {
                MessageBox.Show("请重新读卡再修改卡!");
                pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                return;
            }

            //保存远程服务器数据库
            string           str    = DataTransfer.ModifyCardNetSet(this.lbSerialNumber.Text, IP, Port, IsDomain, APN, UserName, Password);
            JavaScriptObject result = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);

            if (!bool.Parse(result["Result"].ToString()))
            {
                string txt = result["Message"].ToString();
                MessageBox.Show(txt);
                pf.Log(txt);
                pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodOpenCard);
                return;
            }

            int    mode           = 4;            //以B密码认证
            int    sec            = 1;            //扇区
            int    block          = 0;
            string key            = pf.getKeyB(); //读卡密码
            string result_WriteIC = "";
            string result_AuthIC  = "";

            string hex_IP       = HexStringUtility.StrToHexString(IP);
            string hex_Port     = HexStringUtility.StrToHexString(Port);
            string hex_APN      = HexStringUtility.StrToHexString(APN);
            string hex_UserName = HexStringUtility.StrToHexString(UserName);
            string hex_Password = HexStringUtility.StrToHexString(Password);

            string len_IP       = (hex_IP.Length / 2).ToString("X").PadLeft(2, '0');
            string len_Port     = (hex_Port.Length / 2).ToString("X").PadLeft(2, '0');
            string len_APN      = (hex_APN.Length / 2).ToString("X").PadLeft(2, '0');
            string len_UserName = (hex_UserName.Length / 2).ToString("X").PadLeft(2, '0');
            string len_Password = (hex_Password.Length / 2).ToString("X").PadLeft(2, '0');

            //设置扇区1内容
            sec = 1;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //写数据块0,数据块0为卡类型(1字节)、是否域名访问(1字节)、有效IP地址长度(1字节)、有效端口号长度(1字节)
                //APN名称长度(1字节)、APN用户名长度(1字节)、APN密码长度(1字节)
                block          = 0;
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, (InfoSys.CardTypeNetSet + IsDomain +
                                                                        len_IP + len_Port + len_APN + len_UserName + len_Password).PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrModifyFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                    return;
                }

                //写数据块1,IP地址或域名地址
                block          = 1;
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, hex_IP.PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrModifyFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                    return;
                }

                //写数据块2,端口号(5字节)
                block          = 2;
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, hex_Port.PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrModifyFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                    return;
                }
            }

            //设置扇区2内容
            sec = 2;
            //认证卡密码B
            result_AuthIC = CardCommon.AuthIC(icdev, mode, sec, key);
            pf.AuthLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodOpenCard, sec, result_AuthIC);
            if (result_AuthIC == InfoSys.StrAuthSuccess)
            {
                //写数据块0,APN名称
                block          = 0;
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, hex_APN.PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrModifyFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                    return;
                }

                //写数据块1,用户名
                block          = 1;
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, hex_UserName.PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrModifyFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                    return;
                }

                //写数据块2,密码
                block          = 2;
                result_WriteIC = CardCommon.WriteIC(icdev, sec, block, hex_Password.PadRight(32, '0'));
                if (result_WriteIC != "")
                {
                    this.lbState.Text = InfoSys.InfoWriteFailure(sec, block, InfoSys.CardTypeStrNetSet, InfoSys.StrModifyFailure);
                    pf.Log(this.lbState.Text);
                    pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
                    return;
                }
            }

            SerialNumber_old     = "";
            this.lbCardType.Text = InfoSys.CardTypeNetSet;
            this.lbState.Text    = InfoSys.StrModifySuccess;
            pf.Log(this.lbState.Text);
            pf.EndLog(InfoSys.CardTypeStrNetSet, InfoSys.MethodModifyCard);
        }