问题描述
我想利用委托,在form2中设置form1的串口,可是发现实现不了,本人菜鸟,求大神指点啊!!!!http://yunpan.cn/c3Wz6M3Merm5N访问密码5308这个是我写的form1中的代码,namespaceWindowsFormsApplication1{publicpartialclassForm1:Form{privateSerialPortscanRS232SP=newSerialPort("COM1",9600,Parity.None,8,StopBits.One);privateSerialPortmoduleRS232P=newSerialPort("COM2",9600,Parity.None,8,StopBits.One);publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidbutton1_Click(objectsender,EventArgse){setSerialPorts=newForm2();setSerialPorts.ShowDialog();setSerialPorts.setSPInvoke=UpdateSerialPorts;}publicForm2setSerialPorts;publicvoidUpdateSerialPorts(stringscanportname,intscanbaudrate,intscandatabits,Parityscanparity,StopBitsscanstopbit,stringmoduleportname,intmodulebaudrate,intmoduledatabits,Paritymoduleparity,StopBitsmodulestopbit){scanRS232SP.PortName=scanportname;scanRS232SP.BaudRate=scanbaudrate;scanRS232SP.DataBits=scandatabits;scanRS232SP.Parity=scanparity;scanRS232SP.StopBits=scanstopbit;moduleRS232P.PortName=moduleportname;moduleRS232P.BaudRate=modulebaudrate;moduleRS232P.DataBits=moduledatabits;moduleRS232P.Parity=moduleparity;moduleRS232P.StopBits=modulestopbit;}}}
下面是form2的,求指点:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.IO.Ports;usingWindowsFormsApplication1.Properties;namespaceWindowsFormsApplication1{publicpartialclassForm2:Form{publicForm2(){InitializeComponent();InitializeControlValues();}privatevoidForm2_Load(objectsender,EventArgse){}publicdelegatevoidSetSPInvoke(stringscanportname,intscanbaudrate,intscandatabits,Parityscanparity,StopBitsscanstopbit,stringmoduleportname,intmodulebaudrate,intmoduledatabits,Paritymoduleparity,StopBitsmodulestopbit);publicSetSPInvokesetSPInvoke;privatevoidbutton1_Click(objectsender,EventArgse){setSPInvoke(Settings.Default.ScanPortName,Settings.Default.ScanBaudRate,Settings.Default.ScanDataBits,Settings.Default.ScanParityBit,Settings.Default.ScanStopBit,Settings.Default.ModulePortName,Settings.Default.ModuleBaudRate,Settings.Default.ModuleDataBits,Settings.Default.ModuleParityBit,Settings.Default.ModuleStopBit);}privatevoidSaveSettings(){Settings.Default.ScanBaudRate=int.Parse(cbScanBaudRate.Text);Settings.Default.ScanDataBits=int.Parse(cbScanDataBits.Text);Settings.Default.ScanParityBit=(Parity)Enum.Parse(typeof(Parity),cbScanParityBit.Text);Settings.Default.ScanStopBit=(StopBits)Enum.Parse(typeof(StopBits),cbScanStopBit.Text);Settings.Default.ScanPortName=cbScanPortName.Text;Settings.Default.ModuleBaudRate=int.Parse(cbModuleBaudRate.Text);Settings.Default.ModuleDataBits=int.Parse(cbModuleDataBits.Text);Settings.Default.ModuleParityBit=(Parity)Enum.Parse(typeof(Parity),cbModuleParityBit.Text);Settings.Default.ModuleStopBit=(StopBits)Enum.Parse(typeof(StopBits),cbModuleStopBit.Text);Settings.Default.ModulePortName=cbModulePortName.Text;Settings.Default.Save();MessageBox.Show("COM口配置成功!","正确",MessageBoxButtons.OK,MessageBoxIcon.None);}privatevoidInitializeControlValues(){cbScanParityBit.Items.Clear();cbScanParityBit.Items.AddRange(Enum.GetNames(typeof(Parity)));cbScanStopBit.Items.Clear();cbScanStopBit.Items.AddRange(Enum.GetNames(typeof(StopBits)));cbScanParityBit.Text=Settings.Default.ScanParityBit.ToString();cbScanStopBit.Text=Settings.Default.ScanStopBit.ToString();cbScanDataBits.Text=Settings.Default.ScanDataBits.ToString();cbScanBaudRate.Text=Settings.Default.ScanBaudRate.ToString();cbScanPortName.Text=Settings.Default.ScanPortName.ToString();cbScanPortName.Items.Clear();foreach(stringsinSerialPort.GetPortNames())cbScanPortName.Items.Add(s);if(cbScanPortName.Items.Contains(Settings.Default.ScanPortName))cbScanPortName.Text=Settings.Default.ScanPortName;elseif(cbScanPortName.Items.Count>0)cbScanPortName.SelectedIndex=0;//if(cbScanPortName.Items.Contains(Settings.Default.ScanPortName))//cbScanPortName.Text=Settings.Default.ScanPortName;//elseif(cbScanPortName.Items.Count>0)cbScanPortName.SelectedIndex=0;//else//{//MessageBox.Show(this,"没有发现通讯设备,请安装通讯设备","无通讯设备连接",MessageBoxButtons.OK,MessageBoxIcon.Error);//this.Close();//}cbModuleParityBit.Items.Clear();cbModuleParityBit.Items.AddRange(Enum.GetNames(typeof(Parity)));cbModuleStopBit.Items.Clear();cbModuleStopBit.Items.AddRange(Enum.GetNames(typeof(StopBits)));cbModuleParityBit.Text=Settings.Default.ModuleParityBit.ToString();cbModuleStopBit.Text=Settings.Default.ModuleStopBit.ToString();cbModuleDataBits.Text=Settings.Default.ModuleDataBits.ToString();cbModuleBaudRate.Text=Settings.Default.ModuleBaudRate.ToString();cbModulePortName.Items.Clear();foreach(stringsinSerialPort.GetPortNames())cbModulePortName.Items.Add(s);if(cbModulePortName.Items.Contains(Settings.Default.ModulePortName))cbModulePortName.Text=Settings.Default.ModulePortName;elseif(cbModulePortName.Items.Count>0)cbModulePortName.SelectedIndex=0;//if(cbModulePortName.Items.Contains(Settings.Default.ModulePortName))cbModulePortName.Text=Settings.Default.ModulePortName;//elseif(cbModulePortName.Items.Count>0)cbModulePortName.SelectedIndex=0;//else//{//MessageBox.Show(this,"没有发现通讯设备,请安装通讯设备","无通讯设备",MessageBoxButtons.OK,MessageBoxIcon.Error);//this.Close();//}}}}