示例#1
0
 private void button_OpenSerial_Click(object sender, EventArgs e)
 {
     if (this.portData==null)
     {
         portData = new PortData(this.comboBox_Serial.Text, 9600, Parity.None);
         portData.Received += new PortDataReceivedEventHandle(Program.mainForm.portData_Received);
         portData.Open();
         this.comboBox_Serial.Enabled = false;
         this.button_OpenSerial.Text = "关闭串口";
         this.DialogResult = DialogResult.OK;
     }
     else
     {
         this.portData.Close();
         this.portData = null;
         this.comboBox_Serial.Enabled = true;
         this.button_OpenSerial.Text = "打开串口";
         this.DialogResult = DialogResult.Cancel;
     }
 }
示例#2
0
 private void button_OpenSerial_Click(object sender, EventArgs e)
 {
     if (this.button_OpenSerial.Text == "打开串口")
     {
         portData = new PortData(this.comboBox_Serial.Text, 9600, Parity.None);
         portData.Received += new PortDataReceivedEventHandle(portData_Received);
         portData.Open();
         this.comboBox_Serial.Enabled = false;
         this.button_OpenSerial.Text = "关闭串口";
     }
     else
     {
         this.portData.Close();
         this.comboBox_Serial.Enabled = true;
         this.button_OpenSerial.Text = "打开串口";
     }
 }
示例#3
0
        public bool OpenSerial()
        {
            try
            {

                //this.SerialInfo.Text = SerialPort.GetPortNames().Length.ToString();
                //if (PortCount == SerialPort.GetPortNames().Length)
                //{
                //    this.IsOld = true;
                //    return true;
                //}
                //else
                //{
                //    this.IsOld = false;
                //    PortCount = SerialPort.GetPortNames().Length;
                //}
                foreach (string portName in SerialPort.GetPortNames())
                {
                    if (this.portData != null && this.portData.port.PortName == portName)
                    {

                        if (this.portData.port.IsOpen)
                        {
                            this.IsOld = true;
                            return true;
                        }
                        else
                        {

                            this.portData = null;
                            //return true;
                        }
                    }
                }
                foreach (string portName in SerialPort.GetPortNames())
                {
                    if (this.BadPortList.IndexOf(portName) != -1)
                        continue;
                    PortData tempPort = new PortData(portName, 9600, Parity.None);
                    tempPort.ReceiveEventFlag = true;
                    //tempPort.Received -= new PortDataReceivedEventHandle(this.tempPort_Received);
                    if (tempPort.Open())
                    {
                        //发送测试命令
                        string command = "F70000";
                        // for (int i = 0; i < 5; i++)
                        //{
                        byte[] receiveData = new byte[2];
                        tempPort.SendCommand(command, ref receiveData, 5);
                        string receiveString = Lib.byteToHexStr(receiveData);
                        if (receiveString.Length == 4 && receiveString == command.Substring(0, 4))
                        {
                            this.portData = tempPort;
                            this.IsOld = false;
                            return true;
                        }
                        else
                        {
                            this.BadPortList.Add(portName);
                            tempPort.Close();
                        }
                    }
                }
                //portData.Close();
                //portData = null;
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
示例#4
0
        void SerialItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            if (item.Checked)
            {
                if (portData.IsOpen())
                {
                    portData.Close();
                    item.Checked = false;
                    portData = null;
                }

                item.Visible = false;

            }
            else
            {
                foreach (ToolStripMenuItem childItem in MenuItem_Serial.DropDownItems)
                {
                    childItem.Checked = false;
                }
                if (portData != null)
                {
                    portData.Close();
                    portData = null;
                }
                portData = new PortData(item.Text, 9600, Parity.None);
                portData.Received += new PortDataReceivedEventHandle(this.portData_Received);
                portData.Open();
                item.Checked = true;
            }
        }
示例#5
0
 public SetMachineTime(PortData portData)
 {
     this.portData = portData;
     InitializeComponent();
 }
示例#6
0
 public OpenSerial(PortData portData)
 {
     this.portData = portData;
     InitializeComponent();
 }