示例#1
0
 void LoadConfig()
 {
     try
     {
         serialPortConfig config = serialPortConfig.getDefaultConfig();
         if (config == null)
         {
             return;
         }
         string portname = config.portName;
         if (string.Empty == portname)
         {
             cmbPortName.SelectedIndex = -1;
         }
         else
         {
             cmbPortName.SelectedIndex = cmbPortName.Items.IndexOf(portname);
         }
         string baudRate = config.baudRate;
         if (string.Empty != baudRate)
         {
             cmbBaudRate.SelectedIndex = cmbBaudRate.Items.IndexOf(baudRate);
         }
         else
         {
             cmbBaudRate.SelectedIndex = -1;
         }
         string parity = config.parity;
         if (string.Empty != parity)
         {
             cmbParity.SelectedIndex = cmbParity.Items.IndexOf(parity);
         }
         else
         {
             cmbParity.SelectedIndex = -1;
         }
         string stopbites = config.stopBits;
         if (string.Empty != stopbites)
         {
             cmbStopBits.SelectedIndex = cmbStopBits.Items.IndexOf(stopbites);
         }
         else
         {
             cmbStopBits.SelectedIndex = -1;
         }
         string databits = config.dataBits;
         if (string.Empty != databits)
         {
             cmbDataBits.SelectedIndex = cmbDataBits.Items.IndexOf(databits);
         }
         else
         {
             cmbDataBits.SelectedIndex = -1;
         }
     }
     catch (System.Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
        /// <summary>
        /// 这个函数使用统一的ConfigManager来配置串口参数,如果项目中没有ConfigManager类,需要将此函数注释
        /// </summary>
        public static void resetStaticSerialPort()
        {
            SerialPort sp = StaticDataPort.comport;

            if (sp == null)
            {
                return;
            }
            bool biniOpened = sp.IsOpen;

            if (biniOpened)
            {
                sp.Close();
                //MessageBox.Show("请先关闭串口!");
                //return;
            }
            try
            {
                serialPortConfig config = serialPortConfig.getDefaultConfig();
                if (config != null)
                {
                    sp.PortName = config.portName;
                    sp.BaudRate = int.Parse(config.baudRate);
                    sp.DataBits = int.Parse(config.dataBits);
                    sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits), config.stopBits);
                    sp.Parity   = (Parity)Enum.Parse(typeof(Parity), config.parity);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("配置文件出现错误!" + ex.Message);
            }
        }
示例#3
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtInterval.Text == null || this.txtInterval.Text.Length <= 0)
            {
                MessageBox.Show("请填写一个时间间隔,单位为微秒", "提示");
                return;
            }
            string interval = this.txtInterval.Text;

            enumSendDataType type = enumSendDataType.None;

            try
            {
                int i = int.Parse(interval);
            }
            catch
            {
                MessageBox.Show("填写的时间间隔不正确", "提示");
                return;
            }
            if (this.radioSerialPort.Checked == true)
            {
                type = enumSendDataType.SerialPort;
            }
            else if (this.radioUDP.Checked == true)
            {
                type = enumSendDataType.UDP;
            }

            appConfig config = new appConfig(type, interval);

            genericConfig.saveConfig(config);

            if (type == enumSendDataType.SerialPort)
            {
                serialPortConfig serialConfig = new serialPortConfig(cmbPortName.Text,
                                                                     cmbBaudRate.Text,
                                                                     cmbParity.Text,
                                                                     cmbDataBits.Text,
                                                                     cmbStopBits.Text);
                genericConfig.saveConfig(serialConfig);
            }
            else
            if (type == enumSendDataType.UDP)
            {
                UDPConfig udpConfig = new UDPConfig(this.txtServerIP.Text, this.txtPort.Text);
                genericConfig.saveConfig(udpConfig);
            }


            this.Close();
        }
示例#4
0
        private void btnSaveConfig_Click(object sender, EventArgs e)
        {
            //ConfigManager.SaveSerialPortConfigurnation(cmbPortName.Text,
            //                                 cmbBaudRate.Text,
            //                                 cmbParity.Text,
            //                                 cmbDataBits.Text,
            //                                 cmbStopBits.Text);


            serialPortConfig config = new serialPortConfig("default", cmbPortName.Text,
                                                           cmbBaudRate.Text,
                                                           cmbParity.Text,
                                                           cmbDataBits.Text,
                                                           cmbStopBits.Text);

            serialPortConfig.saveConfig(config);
            //MessageBox.Show("保存完成!", "串口设置");
            StaticSerialPort.resetStaticSerialPort();
            this.Close();
        }
示例#5
0
        public static void openDataPort(int port)
        {
            if (bDataPortOpen == true)
            {
                return;
            }
            try
            {
#if UDP_TRANSE
                initial_udp_server(port);
                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                //The epSender identifies the incoming clients
                EndPoint epSender = (EndPoint)ipeSender;

                //Start receiving data
                serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
                                              SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
#endif

#if SERIAL_PORT_TRANSE
                serialPortConfig config = serialPortConfig.getDefaultConfig();
                if (config != null && config.portName.Length > 0)
                {
                    if (!StaticDataPort.getStaticSerialPort(config.portName, int.Parse(config.baudRate)).IsOpen)
                    {
                        StaticDataPort.getStaticSerialPort(config.portName, int.Parse(config.baudRate)).Open();
                    }
                }
                else
                {
                    MessageBox.Show("串口打开失败,请先设置串口!", "信息提示", MessageBoxButtons.OK);
                }
#endif
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message + ",请检查后重启本系统", "信息提示", MessageBoxButtons.OK);
            }

            bDataPortOpen = true;
        }
示例#6
0
        /// <summary>
        /// 这个函数使用统一的ConfigManager来配置串口参数,如果项目中没有ConfigManager类,需要将此函数注释
        /// </summary>
        public static void resetStaticSerialPort()
        {
            SerialPort sp = getStaticSerialPort();

            if (sp == null)
            {
                return;
            }
            bool biniOpened = sp.IsOpen;

            if (biniOpened)
            {
                sp.Close();
            }
            try
            {
                serialPortConfig config = serialPortConfig.getDefaultConfig();
                if (config != null)
                {
                    sp.PortName = config.portName;
                    sp.BaudRate = int.Parse(config.baudRate);
                    sp.DataBits = int.Parse(config.dataBits);
                    sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits), config.stopBits);
                    sp.Parity   = (Parity)Enum.Parse(typeof(Parity), config.parity);
                }
                //sp.PortName = ConfigManager.GetItemValue("PortName");
                //sp.BaudRate = int.Parse(ConfigManager.GetItemValue("BaudRate"));
                //sp.DataBits = int.Parse(ConfigManager.GetItemValue("DataBits"));
                //sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits), ConfigManager.GetItemValue("StopBits"));
                //sp.Parity = (Parity)Enum.Parse(typeof(Parity), ConfigManager.GetItemValue("Parity"));
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("配置文件出现错误!" + ex.Message);
            }
        }
示例#7
0
        //Broadcast the message typed by the user to everyone
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSend.Text == "开始")
                {
                    data2Send            = txtMessage.Text;
                    this.maxLength       = data2Send.Length;
                    this.currentLength   = 0;
                    btnSend.Text         = "停止";
                    this.button2.Enabled = false;

                    //根据配置确定udp还是串口
                    appConfig appc = (appConfig)genericConfig.getDefaultConfig(new appConfig());
                    if (appc != null)
                    {
                        this._timer.Interval = int.Parse(appc.sendDataInterval);
                        this.sendDataType    = appc.sendDataType;
                        if (appc.sendDataType == enumSendDataType.UDP)
                        {
                            //打开UDP连接
                            if (this.clientSocket == null)
                            {
                                UDPConfig config = (UDPConfig)genericConfig.getDefaultConfig(new UDPConfig());
                                if (config != null)
                                {
                                    //IP address of the server machine
                                    IPAddress  ipAddress  = IPAddress.Parse(config.ip);
                                    int        port       = int.Parse(config.port);
                                    IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, port);
                                    epServer = (EndPoint)ipEndPoint;
                                }
                                //Using UDP sockets
                                clientSocket = new Socket(AddressFamily.InterNetwork,
                                                          SocketType.Dgram, ProtocolType.Udp);
                            }
                        }
                        else
                        if (appc.sendDataType == enumSendDataType.SerialPort)
                        {
                            //打开串口
                            if (this.comport == null)
                            {
                                serialPortConfig config = (serialPortConfig)genericConfig.getDefaultConfig(new serialPortConfig());
                                if (config != null)
                                {
                                    this.comport     = new SerialPort();
                                    comport.PortName = config.portName;
                                    comport.BaudRate = int.Parse(config.baudRate);
                                    comport.DataBits = int.Parse(config.dataBits);
                                    comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), config.stopBits);
                                    comport.Parity   = (Parity)Enum.Parse(typeof(Parity), config.parity);

                                    comport.Open();
                                }
                            }
                        }
                    }

                    this._timer.Enabled = true;
                }
                else
                {
                    this._timer.Enabled  = false;
                    this.button2.Enabled = true;

                    btnSend.Text = "开始";
                }


                //string msgToSend = txtMessage.Text;
                //byte[] byteData = Encoding.UTF8.GetBytes(msgToSend);
                ////Send it to the server
                //clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);

                //txtMessage.Text = null;
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to send message to the server.", "SGSclientUDP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }