public frmConfig() { InitializeComponent(); string[] ports = SerialPort.GetPortNames(); Array.Sort(ports); cmbPortName.Items.AddRange(ports); appConfig appc = (appConfig)genericConfig.getDefaultConfig(new appConfig()); if (appc != null) { this.txtInterval.Text = appc.sendDataInterval; if (appc.sendDataType == enumSendDataType.UDP) { this.radioUDP.Checked = true; } else if (appc.sendDataType == enumSendDataType.SerialPort) { this.radioSerialPort.Checked = true; } } this.loadUdpConfig(); this.LoadSerialConfig(); }
public static void startUDPListening() { try { //We are using UDP sockets serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); appConfig ac = appConfig.getDefaultConfig(); port = ac.port; IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, port); //Bind this address to the server serverSocket.Bind(ipEndPoint); 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); } catch (Exception ex) { Debug.WriteLine( string.Format("UDPServer.startUDPListening -> error = {0}" , ex.Message)); } }
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(); }
//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); } }