├── README.md ├── SerialPortConnection.sln ├── SerialPortConnection.suo └── SerialPortConnection ├── Form1.Designer.cs ├── Form1.cs ├── Form1.resx ├── IniFile.cs ├── Profile.cs ├── Program.cs ├── Properties ├── AssemblyInfo.cs ├── Resources.Designer.cs ├── Resources.resx ├── Settings.Designer.cs └── Settings.settings ├── SerialPortConnection.csproj ├── bin └── Debug │ ├── Cfg.ini │ ├── SerialPortConnection.exe │ ├── SerialPortConnection.pdb │ ├── SerialPortConnection.vshost.exe │ └── SerialPortConnection.vshost.exe.manifest └── obj └── Debug ├── SerialPortConnection.Form1.resources ├── SerialPortConnection.Properties.Resources.resources ├── SerialPortConnection.csproj.FileListAbsolute.txt ├── SerialPortConnection.csproj.GenerateResource.Cache ├── SerialPortConnection.exe └── SerialPortConnection.pdb /README.md: -------------------------------------------------------------------------------- 1 | SerialPortConnection 2 | ==================== 3 | 4 | 文章《[用C#一步步写串口通信](http://blog.csdn.net/geekwangminli/article/details/7851673)》的代码,点击右下方的“Download Zip”下载 5 | -------------------------------------------------------------------------------- /SerialPortConnection.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual Studio 2008 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialPortConnection", "SerialPortConnection\SerialPortConnection.csproj", "{08018F75-3357-41C6-BA8F-39A237E1076B}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {08018F75-3357-41C6-BA8F-39A237E1076B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {08018F75-3357-41C6-BA8F-39A237E1076B}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {08018F75-3357-41C6-BA8F-39A237E1076B}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {08018F75-3357-41C6-BA8F-39A237E1076B}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /SerialPortConnection.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection.suo -------------------------------------------------------------------------------- /SerialPortConnection/Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace SerialPortConnection 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.components = new System.ComponentModel.Container(); 32 | this.groupBox1 = new System.Windows.Forms.GroupBox(); 33 | this.btnSave = new System.Windows.Forms.Button(); 34 | this.label9 = new System.Windows.Forms.Label(); 35 | this.groupBox3 = new System.Windows.Forms.GroupBox(); 36 | this.cbParity = new System.Windows.Forms.ComboBox(); 37 | this.cbStop = new System.Windows.Forms.ComboBox(); 38 | this.label8 = new System.Windows.Forms.Label(); 39 | this.label7 = new System.Windows.Forms.Label(); 40 | this.cbDataBits = new System.Windows.Forms.ComboBox(); 41 | this.label6 = new System.Windows.Forms.Label(); 42 | this.cbBaudRate = new System.Windows.Forms.ComboBox(); 43 | this.label5 = new System.Windows.Forms.Label(); 44 | this.btnSend = new System.Windows.Forms.Button(); 45 | this.txtSend = new System.Windows.Forms.TextBox(); 46 | this.label4 = new System.Windows.Forms.Label(); 47 | this.label3 = new System.Windows.Forms.Label(); 48 | this.txtSecond = new System.Windows.Forms.TextBox(); 49 | this.label2 = new System.Windows.Forms.Label(); 50 | this.cbTimeSend = new System.Windows.Forms.CheckBox(); 51 | this.groupBox8 = new System.Windows.Forms.GroupBox(); 52 | this.rbRcvStr = new System.Windows.Forms.RadioButton(); 53 | this.rbRcv16 = new System.Windows.Forms.RadioButton(); 54 | this.groupBox7 = new System.Windows.Forms.GroupBox(); 55 | this.radio1 = new System.Windows.Forms.RadioButton(); 56 | this.rdSendStr = new System.Windows.Forms.RadioButton(); 57 | this.btnSwitch = new System.Windows.Forms.Button(); 58 | this.cbSerial = new System.Windows.Forms.ComboBox(); 59 | this.label1 = new System.Windows.Forms.Label(); 60 | this.groupBox2 = new System.Windows.Forms.GroupBox(); 61 | this.txtReceive = new System.Windows.Forms.RichTextBox(); 62 | this.tmSend = new System.Windows.Forms.Timer(this.components); 63 | this.btnClear = new System.Windows.Forms.Button(); 64 | this.btnExit = new System.Windows.Forms.Button(); 65 | this.serialPort1 = new System.IO.Ports.SerialPort(this.components); 66 | this.statusStrip1 = new System.Windows.Forms.StatusStrip(); 67 | this.tsSpNum = new System.Windows.Forms.ToolStripStatusLabel(); 68 | this.tsBaudRate = new System.Windows.Forms.ToolStripStatusLabel(); 69 | this.tsDataBits = new System.Windows.Forms.ToolStripStatusLabel(); 70 | this.tsStopBits = new System.Windows.Forms.ToolStripStatusLabel(); 71 | this.tsParity = new System.Windows.Forms.ToolStripStatusLabel(); 72 | this.groupBox1.SuspendLayout(); 73 | this.groupBox3.SuspendLayout(); 74 | this.groupBox8.SuspendLayout(); 75 | this.groupBox7.SuspendLayout(); 76 | this.groupBox2.SuspendLayout(); 77 | this.statusStrip1.SuspendLayout(); 78 | this.SuspendLayout(); 79 | // 80 | // groupBox1 81 | // 82 | this.groupBox1.Controls.Add(this.btnSave); 83 | this.groupBox1.Controls.Add(this.label9); 84 | this.groupBox1.Controls.Add(this.groupBox3); 85 | this.groupBox1.Controls.Add(this.btnSend); 86 | this.groupBox1.Controls.Add(this.txtSend); 87 | this.groupBox1.Controls.Add(this.label4); 88 | this.groupBox1.Controls.Add(this.label3); 89 | this.groupBox1.Controls.Add(this.txtSecond); 90 | this.groupBox1.Controls.Add(this.label2); 91 | this.groupBox1.Controls.Add(this.cbTimeSend); 92 | this.groupBox1.Controls.Add(this.groupBox8); 93 | this.groupBox1.Controls.Add(this.groupBox7); 94 | this.groupBox1.Controls.Add(this.btnSwitch); 95 | this.groupBox1.Controls.Add(this.cbSerial); 96 | this.groupBox1.Controls.Add(this.label1); 97 | this.groupBox1.Location = new System.Drawing.Point(5, 5); 98 | this.groupBox1.Name = "groupBox1"; 99 | this.groupBox1.Size = new System.Drawing.Size(297, 366); 100 | this.groupBox1.TabIndex = 8; 101 | this.groupBox1.TabStop = false; 102 | this.groupBox1.Text = "发送方"; 103 | // 104 | // btnSave 105 | // 106 | this.btnSave.Location = new System.Drawing.Point(207, 14); 107 | this.btnSave.Name = "btnSave"; 108 | this.btnSave.Size = new System.Drawing.Size(75, 21); 109 | this.btnSave.TabIndex = 32; 110 | this.btnSave.Text = "保存设置"; 111 | this.btnSave.UseVisualStyleBackColor = true; 112 | this.btnSave.Click += new System.EventHandler(this.btnSave_Click); 113 | // 114 | // label9 115 | // 116 | this.label9.AutoSize = true; 117 | this.label9.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 118 | this.label9.ForeColor = System.Drawing.SystemColors.AppWorkspace; 119 | this.label9.Location = new System.Drawing.Point(7, 220); 120 | this.label9.Name = "label9"; 121 | this.label9.Size = new System.Drawing.Size(221, 12); 122 | this.label9.TabIndex = 31; 123 | this.label9.Text = "(16进制时,用空格或“,”将字节隔开)"; 124 | // 125 | // groupBox3 126 | // 127 | this.groupBox3.Controls.Add(this.cbParity); 128 | this.groupBox3.Controls.Add(this.cbStop); 129 | this.groupBox3.Controls.Add(this.label8); 130 | this.groupBox3.Controls.Add(this.label7); 131 | this.groupBox3.Controls.Add(this.cbDataBits); 132 | this.groupBox3.Controls.Add(this.label6); 133 | this.groupBox3.Controls.Add(this.cbBaudRate); 134 | this.groupBox3.Controls.Add(this.label5); 135 | this.groupBox3.Location = new System.Drawing.Point(9, 39); 136 | this.groupBox3.Name = "groupBox3"; 137 | this.groupBox3.Size = new System.Drawing.Size(278, 71); 138 | this.groupBox3.TabIndex = 30; 139 | this.groupBox3.TabStop = false; 140 | this.groupBox3.Text = "串口设置"; 141 | // 142 | // cbParity 143 | // 144 | this.cbParity.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 145 | this.cbParity.FormattingEnabled = true; 146 | this.cbParity.Items.AddRange(new object[] { 147 | "无", 148 | "奇校验", 149 | "偶校验"}); 150 | this.cbParity.Location = new System.Drawing.Point(202, 44); 151 | this.cbParity.Name = "cbParity"; 152 | this.cbParity.Size = new System.Drawing.Size(68, 20); 153 | this.cbParity.TabIndex = 29; 154 | // 155 | // cbStop 156 | // 157 | this.cbStop.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 158 | this.cbStop.FormattingEnabled = true; 159 | this.cbStop.Items.AddRange(new object[] { 160 | "1", 161 | "1.5", 162 | "2"}); 163 | this.cbStop.Location = new System.Drawing.Point(61, 44); 164 | this.cbStop.Name = "cbStop"; 165 | this.cbStop.Size = new System.Drawing.Size(63, 20); 166 | this.cbStop.TabIndex = 28; 167 | // 168 | // label8 169 | // 170 | this.label8.AutoSize = true; 171 | this.label8.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 172 | this.label8.Location = new System.Drawing.Point(138, 47); 173 | this.label8.Name = "label8"; 174 | this.label8.Size = new System.Drawing.Size(63, 14); 175 | this.label8.TabIndex = 27; 176 | this.label8.Text = "校验位:"; 177 | // 178 | // label7 179 | // 180 | this.label7.AutoSize = true; 181 | this.label7.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 182 | this.label7.Location = new System.Drawing.Point(8, 47); 183 | this.label7.Name = "label7"; 184 | this.label7.Size = new System.Drawing.Size(63, 14); 185 | this.label7.TabIndex = 27; 186 | this.label7.Text = "停止位:"; 187 | // 188 | // cbDataBits 189 | // 190 | this.cbDataBits.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 191 | this.cbDataBits.FormattingEnabled = true; 192 | this.cbDataBits.Items.AddRange(new object[] { 193 | "5", 194 | "6", 195 | "7", 196 | "8"}); 197 | this.cbDataBits.Location = new System.Drawing.Point(202, 18); 198 | this.cbDataBits.Name = "cbDataBits"; 199 | this.cbDataBits.Size = new System.Drawing.Size(68, 20); 200 | this.cbDataBits.TabIndex = 26; 201 | // 202 | // label6 203 | // 204 | this.label6.AutoSize = true; 205 | this.label6.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 206 | this.label6.Location = new System.Drawing.Point(139, 21); 207 | this.label6.Name = "label6"; 208 | this.label6.Size = new System.Drawing.Size(63, 14); 209 | this.label6.TabIndex = 25; 210 | this.label6.Text = "数据位:"; 211 | // 212 | // cbBaudRate 213 | // 214 | this.cbBaudRate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 215 | this.cbBaudRate.FormattingEnabled = true; 216 | this.cbBaudRate.Items.AddRange(new object[] { 217 | "300", 218 | "600", 219 | "1200", 220 | "2400", 221 | "4800", 222 | "9600", 223 | "19200", 224 | "38400", 225 | "115200"}); 226 | this.cbBaudRate.Location = new System.Drawing.Point(61, 18); 227 | this.cbBaudRate.Name = "cbBaudRate"; 228 | this.cbBaudRate.Size = new System.Drawing.Size(63, 20); 229 | this.cbBaudRate.TabIndex = 24; 230 | // 231 | // label5 232 | // 233 | this.label5.AutoSize = true; 234 | this.label5.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 235 | this.label5.Location = new System.Drawing.Point(8, 21); 236 | this.label5.Name = "label5"; 237 | this.label5.Size = new System.Drawing.Size(56, 14); 238 | this.label5.TabIndex = 23; 239 | this.label5.Text = "波特率:"; 240 | // 241 | // btnSend 242 | // 243 | this.btnSend.Location = new System.Drawing.Point(228, 221); 244 | this.btnSend.Name = "btnSend"; 245 | this.btnSend.Size = new System.Drawing.Size(60, 25); 246 | this.btnSend.TabIndex = 22; 247 | this.btnSend.Text = "发送"; 248 | this.btnSend.UseVisualStyleBackColor = true; 249 | this.btnSend.Click += new System.EventHandler(this.btnSend_Click); 250 | // 251 | // txtSend 252 | // 253 | this.txtSend.Location = new System.Drawing.Point(6, 194); 254 | this.txtSend.Name = "txtSend"; 255 | this.txtSend.Size = new System.Drawing.Size(282, 21); 256 | this.txtSend.TabIndex = 21; 257 | this.txtSend.TextChanged += new System.EventHandler(this.txtSend_TextChanged); 258 | this.txtSend.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtSend_KeyPress); 259 | // 260 | // label4 261 | // 262 | this.label4.AutoSize = true; 263 | this.label4.Location = new System.Drawing.Point(9, 179); 264 | this.label4.Name = "label4"; 265 | this.label4.Size = new System.Drawing.Size(65, 12); 266 | this.label4.TabIndex = 20; 267 | this.label4.Text = "发送数据:"; 268 | // 269 | // label3 270 | // 271 | this.label3.AutoSize = true; 272 | this.label3.Location = new System.Drawing.Point(251, 159); 273 | this.label3.Name = "label3"; 274 | this.label3.Size = new System.Drawing.Size(17, 12); 275 | this.label3.TabIndex = 19; 276 | this.label3.Text = "秒"; 277 | // 278 | // txtSecond 279 | // 280 | this.txtSecond.Location = new System.Drawing.Point(199, 154); 281 | this.txtSecond.Name = "txtSecond"; 282 | this.txtSecond.Size = new System.Drawing.Size(44, 21); 283 | this.txtSecond.TabIndex = 18; 284 | this.txtSecond.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtSecond_KeyPress); 285 | // 286 | // label2 287 | // 288 | this.label2.AutoSize = true; 289 | this.label2.Location = new System.Drawing.Point(132, 159); 290 | this.label2.Name = "label2"; 291 | this.label2.Size = new System.Drawing.Size(65, 12); 292 | this.label2.TabIndex = 17; 293 | this.label2.Text = "时间间隔:"; 294 | // 295 | // cbTimeSend 296 | // 297 | this.cbTimeSend.AutoSize = true; 298 | this.cbTimeSend.Location = new System.Drawing.Point(10, 159); 299 | this.cbTimeSend.Name = "cbTimeSend"; 300 | this.cbTimeSend.Size = new System.Drawing.Size(96, 16); 301 | this.cbTimeSend.TabIndex = 16; 302 | this.cbTimeSend.Text = "定时发送数据"; 303 | this.cbTimeSend.UseVisualStyleBackColor = true; 304 | // 305 | // groupBox8 306 | // 307 | this.groupBox8.Controls.Add(this.rbRcvStr); 308 | this.groupBox8.Controls.Add(this.rbRcv16); 309 | this.groupBox8.Location = new System.Drawing.Point(146, 115); 310 | this.groupBox8.Name = "groupBox8"; 311 | this.groupBox8.Size = new System.Drawing.Size(142, 36); 312 | this.groupBox8.TabIndex = 15; 313 | this.groupBox8.TabStop = false; 314 | this.groupBox8.Text = "接收数据格式"; 315 | // 316 | // rbRcvStr 317 | // 318 | this.rbRcvStr.AutoSize = true; 319 | this.rbRcvStr.Location = new System.Drawing.Point(72, 14); 320 | this.rbRcvStr.Name = "rbRcvStr"; 321 | this.rbRcvStr.Size = new System.Drawing.Size(59, 16); 322 | this.rbRcvStr.TabIndex = 2; 323 | this.rbRcvStr.TabStop = true; 324 | this.rbRcvStr.Text = "字符串"; 325 | this.rbRcvStr.UseVisualStyleBackColor = true; 326 | // 327 | // rbRcv16 328 | // 329 | this.rbRcv16.AutoSize = true; 330 | this.rbRcv16.Location = new System.Drawing.Point(9, 14); 331 | this.rbRcv16.Name = "rbRcv16"; 332 | this.rbRcv16.Size = new System.Drawing.Size(59, 16); 333 | this.rbRcv16.TabIndex = 1; 334 | this.rbRcv16.TabStop = true; 335 | this.rbRcv16.Text = "16进制"; 336 | this.rbRcv16.UseVisualStyleBackColor = true; 337 | // 338 | // groupBox7 339 | // 340 | this.groupBox7.Controls.Add(this.radio1); 341 | this.groupBox7.Controls.Add(this.rdSendStr); 342 | this.groupBox7.Location = new System.Drawing.Point(6, 115); 343 | this.groupBox7.Name = "groupBox7"; 344 | this.groupBox7.Size = new System.Drawing.Size(134, 37); 345 | this.groupBox7.TabIndex = 14; 346 | this.groupBox7.TabStop = false; 347 | this.groupBox7.Text = "发送数据格式"; 348 | // 349 | // radio1 350 | // 351 | this.radio1.AutoSize = true; 352 | this.radio1.Location = new System.Drawing.Point(9, 15); 353 | this.radio1.Name = "radio1"; 354 | this.radio1.Size = new System.Drawing.Size(59, 16); 355 | this.radio1.TabIndex = 7; 356 | this.radio1.TabStop = true; 357 | this.radio1.Text = "16进制"; 358 | this.radio1.UseVisualStyleBackColor = true; 359 | // 360 | // rdSendStr 361 | // 362 | this.rdSendStr.AutoSize = true; 363 | this.rdSendStr.Location = new System.Drawing.Point(73, 15); 364 | this.rdSendStr.Name = "rdSendStr"; 365 | this.rdSendStr.Size = new System.Drawing.Size(59, 16); 366 | this.rdSendStr.TabIndex = 6; 367 | this.rdSendStr.TabStop = true; 368 | this.rdSendStr.Text = "字符串"; 369 | this.rdSendStr.UseVisualStyleBackColor = true; 370 | // 371 | // btnSwitch 372 | // 373 | this.btnSwitch.Location = new System.Drawing.Point(127, 14); 374 | this.btnSwitch.Name = "btnSwitch"; 375 | this.btnSwitch.Size = new System.Drawing.Size(75, 21); 376 | this.btnSwitch.TabIndex = 9; 377 | this.btnSwitch.Text = "打开串口"; 378 | this.btnSwitch.UseVisualStyleBackColor = true; 379 | this.btnSwitch.Click += new System.EventHandler(this.btnSwitch_Click); 380 | // 381 | // cbSerial 382 | // 383 | this.cbSerial.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 384 | this.cbSerial.FormattingEnabled = true; 385 | this.cbSerial.Location = new System.Drawing.Point(53, 14); 386 | this.cbSerial.Name = "cbSerial"; 387 | this.cbSerial.Size = new System.Drawing.Size(62, 20); 388 | this.cbSerial.TabIndex = 8; 389 | // 390 | // label1 391 | // 392 | this.label1.AutoSize = true; 393 | this.label1.Font = new System.Drawing.Font("SimSun", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 394 | this.label1.Location = new System.Drawing.Point(10, 18); 395 | this.label1.Name = "label1"; 396 | this.label1.Size = new System.Drawing.Size(49, 14); 397 | this.label1.TabIndex = 7; 398 | this.label1.Text = "串口:"; 399 | // 400 | // groupBox2 401 | // 402 | this.groupBox2.Controls.Add(this.txtReceive); 403 | this.groupBox2.Location = new System.Drawing.Point(310, 5); 404 | this.groupBox2.Name = "groupBox2"; 405 | this.groupBox2.Size = new System.Drawing.Size(315, 366); 406 | this.groupBox2.TabIndex = 9; 407 | this.groupBox2.TabStop = false; 408 | this.groupBox2.Text = "接收方"; 409 | // 410 | // txtReceive 411 | // 412 | this.txtReceive.BackColor = System.Drawing.SystemColors.ControlLightLight; 413 | this.txtReceive.Location = new System.Drawing.Point(18, 21); 414 | this.txtReceive.Name = "txtReceive"; 415 | this.txtReceive.ReadOnly = true; 416 | this.txtReceive.Size = new System.Drawing.Size(283, 329); 417 | this.txtReceive.TabIndex = 0; 418 | this.txtReceive.Text = ""; 419 | // 420 | // tmSend 421 | // 422 | this.tmSend.Tick += new System.EventHandler(this.tmSend_Tick); 423 | // 424 | // btnClear 425 | // 426 | this.btnClear.Location = new System.Drawing.Point(501, 380); 427 | this.btnClear.Name = "btnClear"; 428 | this.btnClear.Size = new System.Drawing.Size(59, 23); 429 | this.btnClear.TabIndex = 10; 430 | this.btnClear.Text = "清空"; 431 | this.btnClear.UseVisualStyleBackColor = true; 432 | this.btnClear.Click += new System.EventHandler(this.btnClear_Click); 433 | // 434 | // btnExit 435 | // 436 | this.btnExit.Location = new System.Drawing.Point(566, 380); 437 | this.btnExit.Name = "btnExit"; 438 | this.btnExit.Size = new System.Drawing.Size(59, 23); 439 | this.btnExit.TabIndex = 10; 440 | this.btnExit.Text = "退出"; 441 | this.btnExit.UseVisualStyleBackColor = true; 442 | this.btnExit.Click += new System.EventHandler(this.btnExit_Click); 443 | // 444 | // statusStrip1 445 | // 446 | this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 447 | this.tsSpNum, 448 | this.tsBaudRate, 449 | this.tsDataBits, 450 | this.tsStopBits, 451 | this.tsParity}); 452 | this.statusStrip1.Location = new System.Drawing.Point(0, 409); 453 | this.statusStrip1.Name = "statusStrip1"; 454 | this.statusStrip1.Size = new System.Drawing.Size(626, 22); 455 | this.statusStrip1.TabIndex = 12; 456 | this.statusStrip1.Text = "statusStrip1"; 457 | // 458 | // tsSpNum 459 | // 460 | this.tsSpNum.Name = "tsSpNum"; 461 | this.tsSpNum.Size = new System.Drawing.Size(95, 17); 462 | this.tsSpNum.Text = "串口号:未指定|"; 463 | // 464 | // tsBaudRate 465 | // 466 | this.tsBaudRate.Name = "tsBaudRate"; 467 | this.tsBaudRate.Size = new System.Drawing.Size(89, 17); 468 | this.tsBaudRate.Text = "波特率:未指定|"; 469 | // 470 | // tsDataBits 471 | // 472 | this.tsDataBits.Name = "tsDataBits"; 473 | this.tsDataBits.Size = new System.Drawing.Size(89, 17); 474 | this.tsDataBits.Text = "数据位:未指定|"; 475 | // 476 | // tsStopBits 477 | // 478 | this.tsStopBits.Name = "tsStopBits"; 479 | this.tsStopBits.Size = new System.Drawing.Size(89, 17); 480 | this.tsStopBits.Text = "停止位:未指定|"; 481 | // 482 | // tsParity 483 | // 484 | this.tsParity.Name = "tsParity"; 485 | this.tsParity.Size = new System.Drawing.Size(89, 17); 486 | this.tsParity.Text = "停止位:未指定|"; 487 | // 488 | // Form1 489 | // 490 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 491 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 492 | this.ClientSize = new System.Drawing.Size(626, 431); 493 | this.Controls.Add(this.statusStrip1); 494 | this.Controls.Add(this.btnExit); 495 | this.Controls.Add(this.btnClear); 496 | this.Controls.Add(this.groupBox1); 497 | this.Controls.Add(this.groupBox2); 498 | this.Name = "Form1"; 499 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 500 | this.Text = "串口通信助手"; 501 | this.Load += new System.EventHandler(this.Form1_Load); 502 | this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); 503 | this.groupBox1.ResumeLayout(false); 504 | this.groupBox1.PerformLayout(); 505 | this.groupBox3.ResumeLayout(false); 506 | this.groupBox3.PerformLayout(); 507 | this.groupBox8.ResumeLayout(false); 508 | this.groupBox8.PerformLayout(); 509 | this.groupBox7.ResumeLayout(false); 510 | this.groupBox7.PerformLayout(); 511 | this.groupBox2.ResumeLayout(false); 512 | this.statusStrip1.ResumeLayout(false); 513 | this.statusStrip1.PerformLayout(); 514 | this.ResumeLayout(false); 515 | this.PerformLayout(); 516 | 517 | } 518 | 519 | #endregion 520 | 521 | private System.Windows.Forms.GroupBox groupBox1; 522 | private System.Windows.Forms.GroupBox groupBox2; 523 | private System.Windows.Forms.ComboBox cbSerial; 524 | private System.Windows.Forms.Label label1; 525 | private System.Windows.Forms.Button btnSwitch; 526 | private System.Windows.Forms.GroupBox groupBox7; 527 | private System.Windows.Forms.GroupBox groupBox8; 528 | private System.Windows.Forms.RadioButton rbRcvStr; 529 | private System.Windows.Forms.RadioButton rbRcv16; 530 | private System.Windows.Forms.RadioButton rdSendStr; 531 | //private System.Windows.Forms.RadioButton rdse; 532 | private System.Windows.Forms.TextBox txtSecond; 533 | private System.Windows.Forms.Label label2; 534 | private System.Windows.Forms.CheckBox cbTimeSend; 535 | private System.Windows.Forms.Label label3; 536 | private System.Windows.Forms.TextBox txtSend; 537 | private System.Windows.Forms.Label label4; 538 | private System.Windows.Forms.Button btnSend; 539 | private System.Windows.Forms.RadioButton radio1; 540 | private System.Windows.Forms.RichTextBox txtReceive; 541 | private System.Windows.Forms.Timer tmSend; 542 | private System.Windows.Forms.Button btnClear; 543 | private System.Windows.Forms.Button btnExit; 544 | private System.Windows.Forms.ComboBox cbBaudRate; 545 | private System.Windows.Forms.Label label5; 546 | private System.Windows.Forms.ComboBox cbDataBits; 547 | private System.Windows.Forms.Label label6; 548 | private System.IO.Ports.SerialPort serialPort1; 549 | private System.Windows.Forms.ComboBox cbStop; 550 | private System.Windows.Forms.Label label8; 551 | private System.Windows.Forms.Label label7; 552 | private System.Windows.Forms.ComboBox cbParity; 553 | private System.Windows.Forms.GroupBox groupBox3; 554 | private System.Windows.Forms.StatusStrip statusStrip1; 555 | private System.Windows.Forms.ToolStripStatusLabel tsSpNum; 556 | private System.Windows.Forms.ToolStripStatusLabel tsBaudRate; 557 | private System.Windows.Forms.ToolStripStatusLabel tsDataBits; 558 | private System.Windows.Forms.ToolStripStatusLabel tsStopBits; 559 | private System.Windows.Forms.ToolStripStatusLabel tsParity; 560 | private System.Windows.Forms.Label label9; 561 | private System.Windows.Forms.Button btnSave; 562 | } 563 | } 564 | 565 | -------------------------------------------------------------------------------- /SerialPortConnection/Form1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO.Ports; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.Data; 6 | using System.Drawing; 7 | using System.Linq; 8 | using System.Text; 9 | using System.Windows.Forms; 10 | using INIFILE; 11 | using System.Text.RegularExpressions; 12 | 13 | namespace SerialPortConnection 14 | { 15 | public partial class Form1 : Form 16 | { 17 | SerialPort sp1 = new SerialPort(); 18 | //sp1.ReceivedBytesThreshold = 1;//只要有1个字符送达端口时便触发DataReceived事件 19 | 20 | public Form1() 21 | { 22 | InitializeComponent(); 23 | } 24 | 25 | //加载 26 | private void Form1_Load(object sender, EventArgs e) 27 | { 28 | INIFILE.Profile.LoadProfile();//加载所有 29 | 30 | // 预置波特率 31 | switch (Profile.G_BAUDRATE) 32 | { 33 | case "300": 34 | cbBaudRate.SelectedIndex = 0; 35 | break; 36 | case "600": 37 | cbBaudRate.SelectedIndex = 1; 38 | break; 39 | case "1200": 40 | cbBaudRate.SelectedIndex = 2; 41 | break; 42 | case "2400": 43 | cbBaudRate.SelectedIndex = 3; 44 | break; 45 | case "4800": 46 | cbBaudRate.SelectedIndex = 4; 47 | break; 48 | case "9600": 49 | cbBaudRate.SelectedIndex = 5; 50 | break; 51 | case "19200": 52 | cbBaudRate.SelectedIndex = 6; 53 | break; 54 | case "38400": 55 | cbBaudRate.SelectedIndex = 7; 56 | break; 57 | case "115200": 58 | cbBaudRate.SelectedIndex = 8; 59 | break; 60 | default: 61 | { 62 | MessageBox.Show("波特率预置参数错误。"); 63 | return; 64 | } 65 | } 66 | 67 | //预置波特率 68 | switch (Profile.G_DATABITS) 69 | { 70 | case "5": 71 | cbDataBits.SelectedIndex = 0; 72 | break; 73 | case "6": 74 | cbDataBits.SelectedIndex = 1; 75 | break; 76 | case "7": 77 | cbDataBits.SelectedIndex = 2; 78 | break; 79 | case "8": 80 | cbDataBits.SelectedIndex = 3; 81 | break; 82 | default: 83 | { 84 | MessageBox.Show("数据位预置参数错误。"); 85 | return; 86 | } 87 | 88 | } 89 | //预置停止位 90 | switch (Profile.G_STOP) 91 | { 92 | case "1": 93 | cbStop.SelectedIndex = 0; 94 | break; 95 | case "1.5": 96 | cbStop.SelectedIndex = 1; 97 | break; 98 | case "2": 99 | cbStop.SelectedIndex = 2; 100 | break; 101 | default: 102 | { 103 | MessageBox.Show("停止位预置参数错误。"); 104 | return; 105 | } 106 | } 107 | 108 | //预置校验位 109 | switch(Profile.G_PARITY) 110 | { 111 | case "NONE": 112 | cbParity.SelectedIndex = 0; 113 | break; 114 | case "ODD": 115 | cbParity.SelectedIndex = 1; 116 | break; 117 | case "EVEN": 118 | cbParity.SelectedIndex = 2; 119 | break; 120 | default: 121 | { 122 | MessageBox.Show("校验位预置参数错误。"); 123 | return; 124 | } 125 | } 126 | 127 | //检查是否含有串口 128 | string[] str = SerialPort.GetPortNames(); 129 | if (str == null) 130 | { 131 | MessageBox.Show("本机没有串口!", "Error"); 132 | return; 133 | } 134 | 135 | //添加串口项目 136 | foreach (string s in System.IO.Ports.SerialPort.GetPortNames()) 137 | {//获取有多少个COM口 138 | //System.Diagnostics.Debug.WriteLine(s); 139 | cbSerial.Items.Add(s); 140 | } 141 | 142 | //串口设置默认选择项 143 | cbSerial.SelectedIndex = 1; //note:获得COM9口,但别忘修改 144 | //cbBaudRate.SelectedIndex = 5; 145 | // cbDataBits.SelectedIndex = 3; 146 | // cbStop.SelectedIndex = 0; 147 | // cbParity.SelectedIndex = 0; 148 | sp1.BaudRate = 9600; 149 | 150 | Control.CheckForIllegalCrossThreadCalls = false; //这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性) 151 | sp1.DataReceived += new SerialDataReceivedEventHandler(sp1_DataReceived); 152 | //sp1.ReceivedBytesThreshold = 1; 153 | 154 | radio1.Checked = true; //单选按钮默认是选中的 155 | rbRcvStr.Checked = true; 156 | 157 | //准备就绪 158 | sp1.DtrEnable = true; 159 | sp1.RtsEnable = true; 160 | //设置数据读取超时为1秒 161 | sp1.ReadTimeout = 1000; 162 | 163 | sp1.Close(); 164 | } 165 | 166 | void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e) 167 | { 168 | if (sp1.IsOpen) //此处可能没有必要判断是否打开串口,但为了严谨性,我还是加上了 169 | { 170 | //输出当前时间 171 | DateTime dt = DateTime.Now; 172 | txtReceive.Text += dt.GetDateTimeFormats('f')[0].ToString() + "\r\n"; 173 | txtReceive.SelectAll(); 174 | txtReceive.SelectionColor = Color.Blue; //改变字体的颜色 175 | 176 | byte[] byteRead = new byte[sp1.BytesToRead]; //BytesToRead:sp1接收的字符个数 177 | if (rdSendStr.Checked) //'发送字符串'单选按钮 178 | { 179 | txtReceive.Text += sp1.ReadLine() + "\r\n"; //注意:回车换行必须这样写,单独使用"\r"和"\n"都不会有效果 180 | sp1.DiscardInBuffer(); //清空SerialPort控件的Buffer 181 | } 182 | else //'发送16进制按钮' 183 | { 184 | try 185 | { 186 | Byte[] receivedData = new Byte[sp1.BytesToRead]; //创建接收字节数组 187 | sp1.Read(receivedData, 0, receivedData.Length); //读取数据 188 | //string text = sp1.Read(); //Encoding.ASCII.GetString(receivedData); 189 | sp1.DiscardInBuffer(); //清空SerialPort控件的Buffer 190 | //这是用以显示字符串 191 | // string strRcv = null; 192 | // for (int i = 0; i < receivedData.Length; i++ ) 193 | // { 194 | // strRcv += ((char)Convert.ToInt32(receivedData[i])) ; 195 | // } 196 | // txtReceive.Text += strRcv + "\r\n"; //显示信息 197 | //} 198 | string strRcv = null; 199 | //int decNum = 0;//存储十进制 200 | for (int i = 0; i < receivedData.Length; i++) //窗体显示 201 | { 202 | 203 | strRcv += receivedData[i].ToString("X2"); //16进制显示 204 | } 205 | txtReceive.Text += strRcv + "\r\n"; 206 | } 207 | catch (System.Exception ex) 208 | { 209 | MessageBox.Show(ex.Message, "出错提示"); 210 | txtSend.Text = ""; 211 | } 212 | } 213 | } 214 | else 215 | { 216 | MessageBox.Show("请打开某个串口", "错误提示"); 217 | } 218 | } 219 | 220 | //发送按钮 221 | private void btnSend_Click(object sender, EventArgs e) 222 | { 223 | if (cbTimeSend.Checked) 224 | { 225 | tmSend.Enabled = true; 226 | } 227 | else 228 | { 229 | tmSend.Enabled = false; 230 | } 231 | 232 | if (!sp1.IsOpen) //如果没打开 233 | { 234 | MessageBox.Show("请先打开串口!", "Error"); 235 | return; 236 | } 237 | 238 | String strSend = txtSend.Text; 239 | if (radio1.Checked == true) //“HEX发送” 按钮 240 | { 241 | //处理数字转换 242 | string sendBuf = strSend; 243 | string sendnoNull = sendBuf.Trim(); 244 | string sendNOComma = sendnoNull.Replace(',', ' '); //去掉英文逗号 245 | string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗号 246 | string strSendNoComma2 = sendNOComma1.Replace("0x", ""); //去掉0x 247 | strSendNoComma2.Replace("0X", ""); //去掉0X 248 | string[] strArray = strSendNoComma2.Split(' '); 249 | 250 | int byteBufferLength = strArray.Length; 251 | for (int i = 0; i < strArray.Length; i++ ) 252 | { 253 | if (strArray[i]=="") 254 | { 255 | byteBufferLength--; 256 | } 257 | } 258 | // int temp = 0; 259 | byte[] byteBuffer = new byte[byteBufferLength]; 260 | int ii = 0; 261 | for (int i = 0; i < strArray.Length; i++) //对获取的字符做相加运算 262 | { 263 | 264 | Byte[] bytesOfStr = Encoding.Default.GetBytes(strArray[i]); 265 | 266 | int decNum = 0; 267 | if (strArray[i] == "") 268 | { 269 | //ii--; //加上此句是错误的,下面的continue以延缓了一个ii,不与i同步 270 | continue; 271 | } 272 | else 273 | { 274 | decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12时,temp == 18 275 | } 276 | 277 | try //防止输错,使其只能输入一个字节的字符 278 | { 279 | byteBuffer[ii] = Convert.ToByte(decNum); 280 | } 281 | catch (System.Exception ex) 282 | { 283 | MessageBox.Show("字节越界,请逐个字节输入!", "Error"); 284 | tmSend.Enabled = false; 285 | return; 286 | } 287 | 288 | ii++; 289 | } 290 | sp1.Write(byteBuffer, 0, byteBuffer.Length); 291 | } 292 | else //以字符串形式发送时 293 | { 294 | sp1.WriteLine(txtSend.Text); //写入数据 295 | } 296 | } 297 | 298 | //开关按钮 299 | private void btnSwitch_Click(object sender, EventArgs e) 300 | { 301 | //serialPort1.IsOpen 302 | if (!sp1.IsOpen) 303 | { 304 | try 305 | { 306 | //设置串口号 307 | string serialName = cbSerial.SelectedItem.ToString(); 308 | sp1.PortName = serialName; 309 | 310 | //设置各“串口设置” 311 | string strBaudRate = cbBaudRate.Text; 312 | string strDateBits = cbDataBits.Text; 313 | string strStopBits = cbStop.Text; 314 | Int32 iBaudRate = Convert.ToInt32(strBaudRate); 315 | Int32 iDateBits = Convert.ToInt32(strDateBits); 316 | 317 | sp1.BaudRate = iBaudRate; //波特率 318 | sp1.DataBits = iDateBits; //数据位 319 | switch (cbStop.Text) //停止位 320 | { 321 | case "1": 322 | sp1.StopBits = StopBits.One; 323 | break; 324 | case "1.5": 325 | sp1.StopBits = StopBits.OnePointFive; 326 | break; 327 | case "2": 328 | sp1.StopBits = StopBits.Two; 329 | break; 330 | default: 331 | MessageBox.Show("Error:参数不正确!", "Error"); 332 | break; 333 | } 334 | switch (cbParity.Text) //校验位 335 | { 336 | case "无": 337 | sp1.Parity = Parity.None; 338 | break; 339 | case "奇校验": 340 | sp1.Parity = Parity.Odd; 341 | break; 342 | case "偶校验": 343 | sp1.Parity = Parity.Even; 344 | break; 345 | default: 346 | MessageBox.Show("Error:参数不正确!", "Error"); 347 | break; 348 | } 349 | 350 | if (sp1.IsOpen == true)//如果打开状态,则先关闭一下 351 | { 352 | sp1.Close(); 353 | } 354 | //状态栏设置 355 | tsSpNum.Text = "串口号:" + sp1.PortName + "|"; 356 | tsBaudRate.Text = "波特率:" + sp1.BaudRate + "|"; 357 | tsDataBits.Text = "数据位:" + sp1.DataBits + "|"; 358 | tsStopBits.Text = "停止位:" + sp1.StopBits + "|"; 359 | tsParity.Text = "校验位:" + sp1.Parity + "|"; 360 | 361 | //设置必要控件不可用 362 | cbSerial.Enabled = false; 363 | cbBaudRate.Enabled = false; 364 | cbDataBits.Enabled = false; 365 | cbStop.Enabled = false; 366 | cbParity.Enabled = false; 367 | 368 | sp1.Open(); //打开串口 369 | btnSwitch.Text = "关闭串口"; 370 | } 371 | catch (System.Exception ex) 372 | { 373 | MessageBox.Show("Error:" + ex.Message, "Error"); 374 | tmSend.Enabled = false; 375 | return; 376 | } 377 | } 378 | else 379 | { 380 | //状态栏设置 381 | tsSpNum.Text = "串口号:未指定|"; 382 | tsBaudRate.Text = "波特率:未指定|"; 383 | tsDataBits.Text = "数据位:未指定|"; 384 | tsStopBits.Text = "停止位:未指定|"; 385 | tsParity.Text = "校验位:未指定|"; 386 | //恢复控件功能 387 | //设置必要控件不可用 388 | cbSerial.Enabled = true; 389 | cbBaudRate.Enabled = true; 390 | cbDataBits.Enabled = true; 391 | cbStop.Enabled = true; 392 | cbParity.Enabled = true; 393 | 394 | sp1.Close(); //关闭串口 395 | btnSwitch.Text = "打开串口"; 396 | tmSend.Enabled = false; //关闭计时器 397 | } 398 | } 399 | 400 | //清空按钮 401 | private void btnClear_Click(object sender, EventArgs e) 402 | { 403 | txtReceive.Text = ""; //清空文本 404 | } 405 | 406 | //退出按钮 407 | private void btnExit_Click(object sender, EventArgs e) 408 | { 409 | Application.Exit(); 410 | } 411 | 412 | //关闭时事件 413 | private void Form1_FormClosing(object sender, FormClosingEventArgs e) 414 | { 415 | INIFILE.Profile.SaveProfile(); 416 | sp1.Close(); 417 | } 418 | 419 | private void txtSend_KeyPress(object sender, KeyPressEventArgs e) 420 | { 421 | if (radio1.Checked== true) 422 | { 423 | //正则匹配 424 | string patten = "[0-9a-fA-F]|\b|0x|0X| "; //“\b”:退格键 425 | Regex r = new Regex(patten); 426 | Match m = r.Match(e.KeyChar.ToString()); 427 | 428 | if (m.Success )//&&(txtSend.Text.LastIndexOf(" ") != txtSend.Text.Length-1)) 429 | { 430 | e.Handled = false; 431 | } 432 | else 433 | { 434 | e.Handled = true; 435 | } 436 | }//end of radio1 437 | else 438 | { 439 | e.Handled = false; 440 | } 441 | } 442 | 443 | private void txtSend_TextChanged(object sender, EventArgs e) 444 | { 445 | 446 | } 447 | 448 | private void btnSave_Click(object sender, EventArgs e) 449 | { 450 | 451 | //设置各“串口设置” 452 | string strBaudRate = cbBaudRate.Text; 453 | string strDateBits = cbDataBits.Text; 454 | string strStopBits = cbStop.Text; 455 | Int32 iBaudRate = Convert.ToInt32(strBaudRate); 456 | Int32 iDateBits = Convert.ToInt32(strDateBits); 457 | 458 | Profile.G_BAUDRATE = iBaudRate+""; //波特率 459 | Profile.G_DATABITS = iDateBits+""; //数据位 460 | switch (cbStop.Text) //停止位 461 | { 462 | case "1": 463 | Profile.G_STOP = "1"; 464 | break; 465 | case "1.5": 466 | Profile.G_STOP = "1.5"; 467 | break; 468 | case "2": 469 | Profile.G_STOP ="2"; 470 | break; 471 | default: 472 | MessageBox.Show("Error:参数不正确!", "Error"); 473 | break; 474 | } 475 | switch (cbParity.Text) //校验位 476 | { 477 | case "无": 478 | Profile.G_PARITY = "NONE"; 479 | break; 480 | case "奇校验": 481 | Profile.G_PARITY = "ODD"; 482 | break; 483 | case "偶校验": 484 | Profile.G_PARITY = "EVEN"; 485 | break; 486 | default: 487 | MessageBox.Show("Error:参数不正确!", "Error"); 488 | break; 489 | } 490 | 491 | //保存设置 492 | // public static string G_BAUDRATE = "1200";//给ini文件赋新值,并且影响界面下拉框的显示 493 | //public static string G_DATABITS = "8"; 494 | //public static string G_STOP = "1"; 495 | //public static string G_PARITY = "NONE"; 496 | Profile.SaveProfile(); 497 | } 498 | 499 | //定时器 500 | private void tmSend_Tick(object sender, EventArgs e) 501 | { 502 | //转换时间间隔 503 | string strSecond = txtSecond.Text; 504 | try 505 | { 506 | int isecond = int.Parse(strSecond) * 1000;//Interval以微秒为单位 507 | tmSend.Interval = isecond; 508 | if (tmSend.Enabled == true) 509 | { 510 | btnSend.PerformClick(); 511 | } 512 | } 513 | catch (System.Exception ex) 514 | { 515 | tmSend.Enabled = false; 516 | MessageBox.Show("错误的定时输入!", "Error"); 517 | } 518 | 519 | } 520 | 521 | private void txtSecond_KeyPress(object sender, KeyPressEventArgs e) 522 | { 523 | string patten = "[0-9]|\b"; //“\b”:退格键 524 | Regex r = new Regex(patten); 525 | Match m = r.Match(e.KeyChar.ToString()); 526 | 527 | if (m.Success) 528 | { 529 | e.Handled = false; //没操作“过”,系统会处理事件 530 | } 531 | else 532 | { 533 | e.Handled = true; 534 | } 535 | } 536 | } 537 | } 538 | -------------------------------------------------------------------------------- /SerialPortConnection/Form1.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 17, 17 122 | 123 | 124 | 106, 17 125 | 126 | 127 | 339, 17 128 | 129 | 130 | 25 131 | 132 | -------------------------------------------------------------------------------- /SerialPortConnection/IniFile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Runtime.InteropServices; 5 | 6 | namespace INIFILE 7 | { 8 | public abstract class CustomIniFile 9 | { 10 | public CustomIniFile(string AFileName) 11 | { 12 | FFileName = AFileName; 13 | } 14 | private string FFileName; 15 | public string FileName 16 | { 17 | get { return FFileName; } 18 | } 19 | public virtual bool SectionExists(string Section) 20 | { 21 | List vStrings = new List(); 22 | ReadSections(vStrings); 23 | return vStrings.Contains(Section); 24 | } 25 | public virtual bool ValueExists(string Section, string Ident) 26 | { 27 | List vStrings = new List(); 28 | ReadSection(Section, vStrings); 29 | return vStrings.Contains(Ident); 30 | } 31 | public abstract string ReadString(string Section, string Ident, string Default); 32 | public abstract bool WriteString(string Section, string Ident, string Value); 33 | public abstract bool ReadSectionValues(string Section, List Strings); 34 | public abstract bool ReadSection(string Section, List Strings); 35 | public abstract bool ReadSections(List Strings); 36 | public abstract bool EraseSection(string Section); 37 | public abstract bool DeleteKey(string Section, string Ident); 38 | public abstract bool UpdateFile(); 39 | } 40 | /// 41 | /// 存储本地INI文件的类。 42 | /// 43 | public class IniFile : CustomIniFile 44 | { 45 | [DllImport("kernel32")] 46 | private static extern uint GetPrivateProfileString( 47 | string lpAppName, // points to section name 48 | string lpKeyName, // points to key name 49 | string lpDefault, // points to default string 50 | byte[] lpReturnedString, // points to destination buffer 51 | uint nSize, // size of destination buffer 52 | string lpFileName // points to initialization filename 53 | ); 54 | 55 | [DllImport("kernel32")] 56 | private static extern bool WritePrivateProfileString( 57 | string lpAppName, // pointer to section name 58 | string lpKeyName, // pointer to key name 59 | string lpString, // pointer to string to add 60 | string lpFileName // pointer to initialization filename 61 | ); 62 | 63 | /// 64 | /// 构造IniFile实例。 65 | /// 指定文件名 66 | /// 67 | public IniFile(string AFileName) 68 | : base(AFileName) 69 | { 70 | } 71 | 72 | /// 73 | /// 析够IniFile实例。 74 | /// 75 | ~IniFile() 76 | { 77 | UpdateFile(); 78 | } 79 | 80 | /// 81 | /// 读取字符串值。 82 | /// 指定变量标识。 83 | /// 指定所在区域。 84 | /// 指定默认值。 85 | /// 返回读取的字符串。如果读取失败则返回该值。 86 | /// 87 | public override string ReadString(string Section, string Ident, string Default) 88 | { 89 | byte[] vBuffer = new byte[2048]; 90 | uint vCount = GetPrivateProfileString(Section, 91 | Ident, Default, vBuffer, (uint)vBuffer.Length, FileName); 92 | return Encoding.Default.GetString(vBuffer, 0, (int)vCount); 93 | } 94 | /// 95 | /// 写入字符串值。 96 | /// 97 | /// 指定所在区域。 98 | /// 指定变量标识。 99 | /// 所要写入的变量值。 100 | /// 返回写入是否成功。 101 | public override bool WriteString(string Section, string Ident, string Value) 102 | { 103 | return WritePrivateProfileString(Section, Ident, Value, FileName); 104 | } 105 | 106 | /// 107 | /// 获得区域的完整文本。(变量名=值格式)。 108 | /// 109 | /// 指定区域标识。 110 | /// 输出处理结果。 111 | /// 返回读取是否成功。 112 | public override bool ReadSectionValues(string Section, List Strings) 113 | { 114 | List vIdentList = new List(); 115 | if (!ReadSection(Section, vIdentList)) return false; 116 | foreach (string vIdent in vIdentList) 117 | Strings.Add(string.Format("{0}={1}", vIdent, ReadString(Section, vIdent, ""))); 118 | return true; 119 | } 120 | 121 | /// 122 | /// 读取区域变量名列表。 123 | /// 124 | /// 指定区域名。 125 | /// 指定输出列表。 126 | /// 返回获取是否成功。 127 | public override bool ReadSection(string Section, List Strings) 128 | { 129 | byte[] vBuffer = new byte[16384]; 130 | uint vLength = GetPrivateProfileString(Section, null, null, vBuffer, 131 | (uint)vBuffer.Length, FileName); 132 | int j = 0; 133 | for (int i = 0; i < vLength; i++) 134 | if (vBuffer[i] == 0) 135 | { 136 | Strings.Add(Encoding.Default.GetString(vBuffer, j, i - j)); 137 | j = i + 1; 138 | } 139 | return true; 140 | } 141 | 142 | /// 143 | /// 读取区域名列表。 144 | /// 145 | /// 指定输出列表。 146 | /// 147 | public override bool ReadSections(List Strings) 148 | { 149 | byte[] vBuffer = new byte[16384]; 150 | uint vLength = GetPrivateProfileString(null, null, null, vBuffer, 151 | (uint)vBuffer.Length, FileName); 152 | int j = 0; 153 | for (int i = 0; i < vLength; i++) 154 | if (vBuffer[i] == 0) 155 | { 156 | Strings.Add(Encoding.Default.GetString(vBuffer, j, i - j)); 157 | j = i + 1; 158 | } 159 | return true; 160 | } 161 | 162 | /// 163 | /// 删除指定区域。 164 | /// 165 | /// 指定区域名。 166 | /// 返回删除是否成功。 167 | public override bool EraseSection(string Section) 168 | { 169 | return WritePrivateProfileString(Section, null, null, FileName); 170 | } 171 | 172 | /// 173 | /// 删除指定变量。 174 | /// 175 | /// 变量所在区域。 176 | /// 变量标识。 177 | /// 返回删除是否成功。 178 | public override bool DeleteKey(string Section, string Ident) 179 | { 180 | return WritePrivateProfileString(Section, Ident, null, FileName); 181 | } 182 | 183 | /// 184 | /// 更新文件。 185 | /// 186 | /// 返回更新是否成功。 187 | public override bool UpdateFile() 188 | { 189 | return WritePrivateProfileString(null, null, null, FileName); 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /SerialPortConnection/Profile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace INIFILE 7 | { 8 | class Profile 9 | { 10 | public static void LoadProfile() 11 | { 12 | string strPath = AppDomain.CurrentDomain.BaseDirectory; 13 | _file = new IniFile(strPath + "Cfg.ini"); 14 | G_BAUDRATE = _file.ReadString("CONFIG", "BaudRate", "4800"); //读数据,下同 15 | G_DATABITS = _file.ReadString("CONFIG", "DataBits", "8"); 16 | G_STOP = _file.ReadString("CONFIG", "StopBits", "1"); 17 | G_PARITY = _file.ReadString("CONFIG", "Parity", "NONE"); 18 | 19 | } 20 | 21 | public static void SaveProfile() 22 | { 23 | string strPath = AppDomain.CurrentDomain.BaseDirectory; 24 | _file = new IniFile(strPath + "Cfg.ini"); 25 | _file.WriteString("CONFIG", "BaudRate", G_BAUDRATE); //写数据,下同 26 | _file.WriteString("CONFIG", "DataBits", G_DATABITS); 27 | _file.WriteString("CONFIG", "StopBits", G_STOP); 28 | _file.WriteString("CONFIG", "G_PARITY", G_PARITY); 29 | } 30 | 31 | private static IniFile _file;//内置了一个对象 32 | 33 | public static string G_BAUDRATE = "1200";//给ini文件赋新值,并且影响界面下拉框的显示 34 | public static string G_DATABITS = "8"; 35 | public static string G_STOP = "1"; 36 | public static string G_PARITY = "NONE"; 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /SerialPortConnection/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Windows.Forms; 5 | 6 | namespace SerialPortConnection 7 | { 8 | static class Program 9 | { 10 | /// 11 | /// The main entry point for the application. 12 | /// 13 | [STAThread] 14 | static void Main() 15 | { 16 | Application.EnableVisualStyles(); 17 | Application.SetCompatibleTextRenderingDefault(false); 18 | Application.Run(new Form1()); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /SerialPortConnection/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SerialPortConnection")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("微软中国")] 12 | [assembly: AssemblyProduct("SerialPortConnection")] 13 | [assembly: AssemblyCopyright("Copyright © 微软中国 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("04ff4c0c-09b2-4d74-888e-0ee127b549bc")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /SerialPortConnection/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:2.0.50727.1891 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace SerialPortConnection.Properties 12 | { 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// Returns the cached ResourceManager instance used by this class. 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SerialPortConnection.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// Overrides the current thread's CurrentUICulture property for all 56 | /// resource lookups using this strongly typed resource class. 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /SerialPortConnection/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /SerialPortConnection/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:2.0.50727.1891 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace SerialPortConnection.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /SerialPortConnection/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SerialPortConnection/SerialPortConnection.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 9.0.21022 7 | 2.0 8 | {08018F75-3357-41C6-BA8F-39A237E1076B} 9 | WinExe 10 | Properties 11 | SerialPortConnection 12 | SerialPortConnection 13 | v3.5 14 | 512 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | 36 | 3.5 37 | 38 | 39 | 3.5 40 | 41 | 42 | 3.5 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | Form 53 | 54 | 55 | Form1.cs 56 | 57 | 58 | 59 | 60 | 61 | 62 | Form1.cs 63 | Designer 64 | 65 | 66 | ResXFileCodeGenerator 67 | Resources.Designer.cs 68 | Designer 69 | 70 | 71 | True 72 | Resources.resx 73 | 74 | 75 | SettingsSingleFileGenerator 76 | Settings.Designer.cs 77 | 78 | 79 | True 80 | Settings.settings 81 | True 82 | 83 | 84 | 85 | 92 | -------------------------------------------------------------------------------- /SerialPortConnection/bin/Debug/Cfg.ini: -------------------------------------------------------------------------------- 1 | [CONFIG] 2 | BaudRate=9600 3 | 4 | DataBits=8 5 | StopBits=1 6 | G_PARITY=NONE 7 | -------------------------------------------------------------------------------- /SerialPortConnection/bin/Debug/SerialPortConnection.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection/bin/Debug/SerialPortConnection.exe -------------------------------------------------------------------------------- /SerialPortConnection/bin/Debug/SerialPortConnection.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection/bin/Debug/SerialPortConnection.pdb -------------------------------------------------------------------------------- /SerialPortConnection/bin/Debug/SerialPortConnection.vshost.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection/bin/Debug/SerialPortConnection.vshost.exe -------------------------------------------------------------------------------- /SerialPortConnection/bin/Debug/SerialPortConnection.vshost.exe.manifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /SerialPortConnection/obj/Debug/SerialPortConnection.Form1.resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection/obj/Debug/SerialPortConnection.Form1.resources -------------------------------------------------------------------------------- /SerialPortConnection/obj/Debug/SerialPortConnection.Properties.Resources.resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection/obj/Debug/SerialPortConnection.Properties.Resources.resources -------------------------------------------------------------------------------- /SerialPortConnection/obj/Debug/SerialPortConnection.csproj.FileListAbsolute.txt: -------------------------------------------------------------------------------- 1 | D:\My Documents\Visual Studio 2008\Projects\SerialPortConnection\SerialPortConnection\obj\Debug\ResolveAssemblyReference.cache 2 | D:\My Documents\Visual Studio 2008\Projects\SerialPortConnection\SerialPortConnection\obj\Debug\SerialPortConnection.Form1.resources 3 | D:\My Documents\Visual Studio 2008\Projects\SerialPortConnection\SerialPortConnection\obj\Debug\SerialPortConnection.Properties.Resources.resources 4 | D:\My Documents\Visual Studio 2008\Projects\SerialPortConnection\SerialPortConnection\obj\Debug\SerialPortConnection.csproj.GenerateResource.Cache 5 | D:\My Documents\Visual Studio 2008\Projects\SerialPortConnection\SerialPortConnection\bin\Debug\SerialPortConnection.exe 6 | D:\My Documents\Visual Studio 2008\Projects\SerialPortConnection\SerialPortConnection\bin\Debug\SerialPortConnection.pdb 7 | D:\My Documents\Visual Studio 2008\Projects\SerialPortConnection\SerialPortConnection\obj\Debug\SerialPortConnection.exe 8 | D:\My Documents\Visual Studio 2008\Projects\SerialPortConnection\SerialPortConnection\obj\Debug\SerialPortConnection.pdb 9 | -------------------------------------------------------------------------------- /SerialPortConnection/obj/Debug/SerialPortConnection.csproj.GenerateResource.Cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection/obj/Debug/SerialPortConnection.csproj.GenerateResource.Cache -------------------------------------------------------------------------------- /SerialPortConnection/obj/Debug/SerialPortConnection.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection/obj/Debug/SerialPortConnection.exe -------------------------------------------------------------------------------- /SerialPortConnection/obj/Debug/SerialPortConnection.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangminli/SerialPortConnection/f094d886a4d8b1a628baaa8dae39a3783185c7a4/SerialPortConnection/obj/Debug/SerialPortConnection.pdb --------------------------------------------------------------------------------