示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static string GetDataFromScale()
        {
            if (device != null)
            {
                device.Close();
            }
            device = new UART();
            device.Open(
                MyGlobal.MySetting.SerialPortName,
                MyGlobal.MySetting.SerialBaudRate,
                MyGlobal.MySetting.SerialDataBits,
                myConverter.FromStringToSerialParity(MyGlobal.MySetting.SerialParity),
                myConverter.FromStringToSerialStopBits(MyGlobal.MySetting.SerialStopBits)
                );

            if (device == null)
            {
                return(null);
            }
            if (device.IsConnected() == false)
            {
                return(null);
            }

            //clear serial receive buffer
            string data = null;

            device.Read();

            //wait time
            int wait_time; bool r = int.TryParse(MyGlobal.MySetting.WaitTime, out wait_time);

            wait_time = r ? wait_time : 100;
            Thread.Sleep(wait_time);

            //read newest data
            data = device.Read();
            if (data == null)
            {
                return(null);
            }

            //process data
            string value = null;

            if (data.Contains("\r\n"))
            {
                string[] buffer = data.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                value = buffer[buffer.Length - 2].Trim();
            }
            device.Close();
            return(value);
        }
示例#2
0
 /// <summary>
 ///
 /// </summary>
 /// <returns></returns>
 public bool Close()
 {
     try {
         if (device != null && IsConnected == true)
         {
             return(device.Close());
         }
         return(true);
     }
     catch {
         return(false);
     }
 }
示例#3
0
        //Calling this Function



        private void Connect_Click(object sender, EventArgs e)
        {
            if (btnConnect.Text == "CONNECT")
            {
                btnConnect.Text  = "DISCONNECT";
                btnStart.Enabled = true;
                btnStop.Enabled  = true;
                UART.Open();
                Status.Text = "Đã kết nối";
            }
            else
            {
                btnConnect.Text  = "CONNECT";
                btnStart.Enabled = false;
                btnStop.Enabled  = false;
                Status.Text      = "Dừng kết nối";
                UART.Close();
            }
        }
        private void BtnDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (UART.IsOpen)
                {
                    while (UART.BytesToWrite > 0)
                    {
                    }
                    UART.DiscardInBuffer();
                    UART.Dispose();
                    UART.Close();

                    btnConnect.Enabled    = true;
                    btnDisconnect.Enabled = false;
                    lblStatus.Text        = "Port is closed !";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 private void BtnConnect_Click(object sender, EventArgs e)
 {
     try
     {
         if (UART.IsOpen == true)
         {
             UART.DiscardInBuffer();
             UART.Close();
         }
         UART.PortName = cbxCom.Text;
         UART.Open();
         lblStatus.Text = "PC is connecting with " + UART.PortName.ToString();
         if (UART.IsOpen == true)
         {
             btnConnect.Enabled    = false;
             btnDisconnect.Enabled = true;
             btnStart.Enabled      = true;
         }
     }
     catch
     {
         MessageBox.Show("Bạn chưa chọn cổng COM !");
     }
 }
示例#6
0
        private void bntConnect_Click(object sender, EventArgs e)
        {
            if (bntConnect.Text == "CONNECT")
            {
                try
                {
                    if (cbCom.Text == "" || cbBaur.Text == "")
                    {
                        lbStripStatus.Text = "Please select PORT setting.";
                    }
                    else
                    {
                        UART.PortName = cbCom.Text;
                        UART.BaudRate = Convert.ToInt32(cbBaur.Text);
                        UART.Open();
                        progressBar.Value = 100;

                        UART.Write("R");

                        if (bntMode.Text == "MODE 1")
                        {
                            UART.Write("2");
                        }
                        else if (bntMode.Text == "MODE 2")
                        {
                            UART.Write("1");
                        }


                        lbStripStatus.Text = "CONNECT TO " + UART.PortName;
                        bntConnect.Text    = "DISCONNECT";

                        cbCom.Enabled    = false;
                        cbBaur.Enabled   = false;
                        bntGate.Enabled  = true;
                        bntReset.Enabled = true;
                        bntReady.Enabled = true;
                        bntSend.Enabled  = true;
                        bntMode.Enabled  = true;
                        tbTx.Enabled     = true;
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    lbStripStatus.Text = "Unauthor Access.";
                }
            }
            else
            {
                progressBar.Value = 0;
                UART.Write("R");
                UART.Close();



                CountDown.Stop();
                Watch.Stop();
                TimerWatch.Stop();
                lbCountDown.Text = "READY";
                lbWatch1.Text    = "00:00:000";
                lbWatch2.Text    = "00:00:000";

                bntConnect.Text = "CONNECT";
                cbCom.Enabled   = true;
                cbBaur.Enabled  = true;

                bntGate.Enabled  = false;
                bntReady.Enabled = false;
                bntStart.Enabled = false;
                bntSend.Enabled  = false;
                bntMode.Enabled  = false;
                tbTx.Enabled     = false;
            }
        }
示例#7
0
        public static bool Output(LampStatus lampstatus)
        {
            try {
                if (thrlamp != null)
                {
                    if (thrlamp.IsAlive == true)
                    {
                        thrlamp.Abort();
                        device.Write("0");
                        Thread.Sleep(100);
                        device.Close();
                    }
                }

                if (device != null)
                {
                    device.Close();
                }
                device = new UART();
                device.Open(
                    MyGlobal.MySetting.LampPortName,
                    MyGlobal.MySetting.LampBaudRate,
                    MyGlobal.MySetting.LampDataBits,
                    myConverter.FromStringToSerialParity(MyGlobal.MySetting.LampParity),
                    myConverter.FromStringToSerialStopBits(MyGlobal.MySetting.LampStopBits)
                    );

                if (device == null)
                {
                    return(false);
                }
                if (device.IsConnected() == false)
                {
                    return(false);
                }

                int _ref = (int)lampstatus;
                switch (_ref)
                {
                case -1: {
                    MyGlobal.OutputLamp = "0";         // off all lamp
                    break;
                }

                case 0: {
                    MyGlobal.OutputLamp = "p";         //on led green
                    break;
                }

                case 1: {
                    MyGlobal.OutputLamp = "f";         //on led red
                    break;
                }

                case 2: {
                    MyGlobal.OutputLamp = "w";         //on led yellow
                    break;
                }

                default: {
                    MyGlobal.OutputLamp = "0";
                    break;
                }
                }

                thrlamp = new Thread(new ThreadStart(() => {
                    if (MyGlobal.OutputLamp == "0")
                    {
                        device.Write("0");
                        Thread.Sleep(100);
                        device.Close();
                    }
                    else
                    {
                        device.Write(MyGlobal.OutputLamp);
                        int delay = MyGlobal.OutputLamp == "p" ? 500 : 3000;
                        Thread.Sleep(delay);
                        device.Write("0");
                        Thread.Sleep(100);
                        device.Close();
                    }
                }));
                thrlamp.IsBackground = true;
                thrlamp.Start();

                return(true);
            }
            catch {
                return(false);
            }
        }
示例#8
0
        ////ST, GS,+    0.0 g
        ////ST, GS,+  106.9 g
        ////US, GS,+    1.2 g

        /// <summary>
        /// Return null if device is not connect
        /// </summary>
        /// <returns></returns>
        public static string GetWeight()
        {
            if (device != null)
            {
                device.Close();
            }
            device = new UART();
            device.Open(
                MyGlobal.MySetting.SerialPortName,
                MyGlobal.MySetting.SerialBaudRate,
                MyGlobal.MySetting.SerialDataBits,
                myConverter.FromStringToSerialParity(MyGlobal.MySetting.SerialParity),
                myConverter.FromStringToSerialStopBits(MyGlobal.MySetting.SerialStopBits)
                );

            if (device == null)
            {
                return(null);
            }
            if (device.IsConnected() == false)
            {
                return(null);
            }

            int count = 0;

REP:
            count++;
            //clear serial receive buffer
            string data = null;

            device.Read();

            int wait_time; bool r = int.TryParse(MyGlobal.MySetting.WaitTime, out wait_time);

            wait_time = r ? wait_time : 100;
            Thread.Sleep(wait_time);

            //get data newest
            data = device.Read();

            //process data
            string value = null;
            string sign  = "";

            if (data.Contains("\r\n"))
            {
                string[] buffer = data.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                value = buffer[buffer.Length - 2].Trim();

                try {
                    buffer = value.Split(',');
                    value  = buffer[2].Replace("g", "").Replace("-", "").Replace("+", "").Trim();
                    sign   = buffer[2].Contains("-") ? "-" : "";
                    value  = sign + value;
                }
                catch {
                    if (count < 3)
                    {
                        goto REP;
                    }
                }
            }
            device.Close();

            if (count >= 3)
            {
                return(null);
            }
            else
            {
                return(value);
            }
        }
		public void Close(){
			myUART.Close();
		}
 // -- Close the Serial Port
 void ClosePort(object sender, EventArgs e)
 {
     UART.Close();
     lbl_status.Text = "Serial Port Disconnected from Microcontroller";
 }