Skip to content

Commit 7961bab

Browse files
committed
add validate method for serial port name.
1 parent efeb7fe commit 7961bab

File tree

4 files changed

+65
-66
lines changed

4 files changed

+65
-66
lines changed

src/GodSharp.SerialPort/Core/GodSerialPort.Static.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO.Ports;
4+
using System.Text.RegularExpressions;
35

46
// ReSharper disable All
57
namespace GodSharp.SerialPort
@@ -9,6 +11,63 @@ namespace GodSharp.SerialPort
911
/// </summary>
1012
public partial class GodSerialPort
1113
{
14+
15+
/// <summary>
16+
/// Get an array of serialport name for current computer.
17+
/// </summary>
18+
/// <returns></returns>
19+
public static string[] GetPortNames() => System.IO.Ports.SerialPort.GetPortNames();
20+
21+
/// <summary>
22+
/// Validate prot name.
23+
/// </summary>
24+
/// <param name="name">The name of serial port.</param>
25+
/// <param name="throwException">weather throw exception.default is false.</param>
26+
/// <returns></returns>
27+
/// <exception cref="ArgumentException"></exception>
28+
public static bool ValidatePortName(string name,bool throwException=false)
29+
{
30+
#if NET35
31+
if (string.IsNullOrEmpty(name) || name?.Trim()=="")
32+
#else
33+
if (string.IsNullOrWhiteSpace(name))
34+
#endif
35+
{
36+
if (throwException)
37+
{
38+
return false;
39+
}
40+
41+
throw new ArgumentNullException(nameof(name));
42+
}
43+
44+
Regex regx = new Regex("^COM([0-9]{1,})$", RegexOptions.IgnoreCase);
45+
if (!regx.IsMatch(name))
46+
{
47+
if (throwException)
48+
{
49+
return false;
50+
}
51+
52+
throw new ArgumentException("port name must be COMxx.", nameof(name));
53+
}
54+
55+
Match match = regx.Match(name);
56+
bool b = int.TryParse(match.Groups[1].Value, out int v);
57+
58+
if (!b || v < 1)
59+
{
60+
if (throwException)
61+
{
62+
return false;
63+
}
64+
65+
throw new ArgumentException("port name must be COMxx.", nameof(name));
66+
}
67+
68+
return true;
69+
}
70+
1271
/// <summary>
1372
/// Gets the baudrate dictionary.
1473
/// </summary>

src/GodSharp.SerialPort/Core/GodSerialPort.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -543,21 +543,9 @@ private void Init()
543543
{
544544
try
545545
{
546-
portName = portName?.Trim() ?? throw new ArgumentNullException(nameof(portName));
546+
portName = portName?.Trim();
547547

548-
Regex regx = new Regex("^COM([0-9]{1,})$", RegexOptions.IgnoreCase);
549-
if (!regx.IsMatch(portName))
550-
{
551-
throw new ArgumentException("port name must be COMxx.", nameof(portName));
552-
}
553-
554-
Match match = regx.Match(portName);
555-
bool b = int.TryParse(match.Groups[1].Value, out int v);
556-
557-
if (!b || v < 1)
558-
{
559-
throw new ArgumentException("port name must be COMxx.", nameof(portName));
560-
}
548+
ValidatePortName(portName);
561549

562550
serialPort.PortName = portName;
563551
serialPort.BaudRate = baudRate;
@@ -911,12 +899,6 @@ public int Write(byte[] bytes, int offset, int count)
911899
/// Discards the output buffer.
912900
/// </summary>
913901
public void DiscardOutBuffer() => serialPort.DiscardOutBuffer();
914-
915-
/// <summary>
916-
/// Get an array of serialport name for current computer.
917-
/// </summary>
918-
/// <returns></returns>
919-
public static string[] GetPortNames() => System.IO.Ports.SerialPort.GetPortNames();
920902
#endregion
921903
}
922904
}

src/GodSharp.SerialPort/GodSharp.SerialPort.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<AssemblyVersion>1.2.0.1</AssemblyVersion>
4-
<AssemblyFileVersion>1.2.0.1</AssemblyFileVersion>
3+
<AssemblyVersion>1.2.0.2</AssemblyVersion>
4+
<AssemblyFileVersion>1.2.0.2</AssemblyFileVersion>
55
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<NeutralLanguage>en-US</NeutralLanguage>
88
<Title>GodSharp.SerialPort</Title>
99
<PackageId>GodSharp.SerialPort</PackageId>
1010
<PackageTags>GodSharp,GodSharp.SerialPort,SerialPort</PackageTags>
11-
<PackageReleaseNotes>support netstandard2.0.</PackageReleaseNotes>
1211
<Company>GodSharp</Company>
1312
<Authors>seayxu</Authors>
1413
<Description>An easy-to-use .NET SerialPort Library.</Description>
@@ -18,7 +17,7 @@
1817
<Copyright>Copyright © GodSharp 2017</Copyright>
1918
<PackageIconUrl>https://avatars3.githubusercontent.com/u/26563296</PackageIconUrl>
2019
<PackageLicenseUrl>https://github.com/godsharp/GodSharp.SerialPort/blob/master/LICENSE</PackageLicenseUrl>
21-
<Version>1.2.0-preview1</Version>
20+
<Version>1.2.0-preview2</Version>
2221
<TargetFrameworks Condition="'$(LibraryFrameworks)'==''">netstandard2.0;net47;net46;net452;net451;net45;net40;net35;</TargetFrameworks>
2322
<TargetFrameworks Condition="'$(LibraryFrameworks)'!=''">$(LibraryFrameworks)</TargetFrameworks>
2423
<ApplicationIcon />

src/GodSharp.SerialPort/GodSharp.SerialPort.nuspec

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)