问题描述
namespaceFB{publicpartialclassForm1:Form{bool_isReceiving;List<byte>buf=newList<byte>(4096);[DllImport("user32.dll",EntryPoint="SendMessageA")]publicstaticexternintSendMessage(IntPtrhwnd,intwMsg,intwParam,intlParam);[DllImport("user32.dll",EntryPoint="PosrMessageA")]publicstaticexternintPostMessage(IntPtrhwnd,intwMsg,intwParam,intlParam);//[DllImport("user32.dll",EntryPoint="SendMessageA")]//privatestaticexternintSendMessage(IntPtrhwnd,intwMsg,IntPtrwParam,stringlParam);//[DllImport("user32.dll",EntryPoint="SendMessageA")]//privatestaticexternintSendMessage(IntPtrhwnd,intwMsg,IntPtrwParam,refRectanglelParam);publicconstintWM_CHAR=0x0102;publicconstintWM_KEYDOWN=0x0100;publicForm1(){InitializeComponent();}privatevoidDispCommConfigParams(){toolStripStatusLabel1.Text=serialPort1.PortName+","+serialPort1.BaudRate.ToString()+","+serialPort1.DataBits.ToString()+","+serialPort1.Parity.ToString()+","+serialPort1.StopBits.ToString()+""+(serialPort1.IsOpen?"Opened":"Closed");}boolOpen(){serialPort1.PortName=cbPortName.SelectedItem.ToString();serialPort1.ReceivedBytesThreshold=1;serialPort1.WriteTimeout=1000;//写超时,如果底层串口驱动效率问题,能有效的避免死锁serialPort1.ReadTimeout=1000;//读超时,同上serialPort1.NewLine="rn";//新行的文本,用于WriteLine方法中由系统附加在text后serialPort1.Open();//打开串口button1.Text="关闭串口";//timer2.Enabled=true;TurnonLed();returnserialPort1.IsOpen;//返回打开结果}voidnewClose(){inti=Environment.TickCount;while(Environment.TickCount-i<2000&&_isReceiving)Application.DoEvents();button1.Text="打开串口";TurnoffLed();serialPort1.Close();//现在没有死锁了,关闭串口//timer2.Enabled=false;}//波特率privatevoidcbBautrate_SelectedIndexChanged(objectsender,EventArgse){if(serialPort1.IsOpen){Close();TurnoffLed();serialPort1.BaudRate=int.Parse(cbBautrate.Items[cbBautrate.SelectedIndex].ToString());Open();TurnonLed();}serialPort1.BaudRate=int.Parse(cbBautrate.Items[cbBautrate.SelectedIndex].ToString());DispCommConfigParams();}privatevoidcbDataByte_SelectedIndexChanged(objectsender,EventArgse){serialPort1.DataBits=int.Parse(cbDataByte.Items[cbDataByte.SelectedIndex].ToString());DispCommConfigParams();}//停止位privatevoidcbStopByte_SelectedIndexChanged(objectsender,EventArgse){switch(cbStopByte.SelectedIndex){case0:serialPort1.StopBits=System.IO.Ports.StopBits.One;break;case1:serialPort1.StopBits=System.IO.Ports.StopBits.OnePointFive;break;case2:serialPort1.StopBits=System.IO.Ports.StopBits.Two;break;}DispCommConfigParams();}//串口参数设置-波特率privatevoidcbValidByte_SelectedIndexChanged(objectsender,EventArgse){switch(cbValidByte.SelectedIndex){case0:serialPort1.Parity=System.IO.Ports.Parity.None;break;case1:serialPort1.Parity=System.IO.Ports.Parity.Odd;break;case2:serialPort1.Parity=System.IO.Ports.Parity.Even;break;case3:serialPort1.Parity=System.IO.Ports.Parity.Mark;break;case4:serialPort1.Parity=System.IO.Ports.Parity.Space;break;}DispCommConfigParams();}//枚举系统中的串口privatevoidEnumComportfromReg(ComboBoxCombobox){Combobox.Items.Clear();///定义注册表子PathstringstrRegPath=@"Hardware\DeviceMap\SerialComm";///创建两个RegistryKey类,一个将指向RootPath,另一个将指向子PathRegistryKeyregRootKey;RegistryKeyregSubKey;///定义Root指向注册表HKEY_LOCAL_MACHINE节点regRootKey=Registry.LocalMachine;///Registry枚举类提供了以下几种/*Registry.ClassesRoot-------------->指向注册表HKEY_CLASSES_ROOT节点Registry.CurrentConfig-------------->指向注册表HKEY_CURRENT_CONFIG节点Registry.CurrentUser-------------->指向注册表HKEY_CURRENT_USER节点Registry.DynData-------------->指向注册表HKEY_DYN_DATA节点(动态注册表数据)Registry.LocalMachine-------------->指向注册表HKEY_LOCAL_MACHINE节点Registry.PerformanceData-------------->指向注册表HKEY_PERFORMANCE_DATA节点Registry.Users-------------->指向注册表HKEY_USERS节点*/regSubKey=regRootKey.OpenSubKey(strRegPath);string[]strCommList=regSubKey.GetValueNames();foreach(stringVNameinstrCommList){//向listbox1中添加字符串的名称和数据,数据是从rk对象中的GetValue(it)方法中得来的Combobox.Items.Add(regSubKey.GetValue(VName));}if(Combobox.Items.Count>0)Combobox.SelectedIndex=0;///关闭regSubKey.Close();regRootKey.Close();}privatevoidTurnonLed(){picLedOff.Visible=false;picLedOn.Visible=true;}privatevoidTurnoffLed(){picLedOff.Visible=true;picLedOn.Visible=false;}//初始化串口参数选择框privatevoidInitComConfigCombobox(){cbBautrate.SelectedIndex=6;cbValidByte.SelectedIndex=2;cbStopByte.SelectedIndex=0;cbDataByte.SelectedIndex=3;}//串口开闭privatevoidbutton1_Click(objectsender,EventArgse){//如果串口没有打开,则先打开串口if(!serialPort1.IsOpen){serialPort1.BaudRate=int.Parse(cbBautrate.Items[cbBautrate.SelectedIndex].ToString());Open();}else{Close();}DispCommConfigParams();}在toolStripStatusLabel1,运行的时候发现串口号波特率是可以保存的,其他三个不能保存在toolStripStatusLabel1
解决方案
解决方案二:
你应该在button1_Click里设置所有的参数串口已经打开,不要随便关闭串口修改设置再打开串口
解决方案三:
而且如果你修改了参数但是不重新打开串口,这些参数根本不会生效
解决方案四:
您的意思,我的串口初始化,包括后来给数据位,停止位,校验位设置的那些命令是不是都要在button1底下完成?
解决方案五:
自己置顶一下,见谅。