11using System . Diagnostics ;
22using System . IO . Ports ;
33using System . Reflection ;
4+ using System . Reflection . Metadata . Ecma335 ;
45using minitool . Enums ;
56using minitool . Models ;
67
@@ -11,20 +12,29 @@ namespace minitool;
1112/// </summary>
1213public static class MinipadHelper
1314{
15+ public static SerialPort Open ( string portName )
16+ {
17+ // Create the SerialPort instance with the correct settings.
18+ SerialPort port = new SerialPort ( portName , 115200 , Parity . Even , 8 , StopBits . One ) ;
19+ port . RtsEnable = true ;
20+ port . DtrEnable = true ;
21+
22+ // Open the port and return it.
23+ port . Open ( ) ;
24+ return port ;
25+ }
26+
1427 public static void Send ( MinipadDevice device , params string [ ] commands )
1528 {
1629 // Open the serial port.
17- SerialPort serialPort = new SerialPort ( device . PortName , 115200 , Parity . Even , 8 , StopBits . One ) ;
18- serialPort . RtsEnable = true ;
19- serialPort . DtrEnable = true ;
20- serialPort . Open ( ) ;
30+ SerialPort port = Open ( device . PortName ) ;
2131
2232 // Write all specified commands to the serial interface.
2333 foreach ( string command in commands )
24- serialPort . WriteLine ( command ) ;
34+ port . WriteLine ( command ) ;
2535
2636 // Safely close the serial port for usage by other processes.
27- serialPort . Close ( ) ;
37+ port . Close ( ) ;
2838 }
2939
3040 public static ( int [ ] raw , int [ ] mapped ) GetSensorValues ( MinipadDevice device )
@@ -38,9 +48,7 @@ public static (int[] raw, int[] mapped) GetSensorValues(MinipadDevice device)
3848 mappedValues [ i ] = - 1 ;
3949 }
4050
41- SerialPort port = new SerialPort ( device . PortName , 115200 , Parity . None , 8 , StopBits . One ) ;
42- port . RtsEnable = true ;
43- port . DtrEnable = true ;
51+ SerialPort port = Open ( device . PortName ) ;
4452 port . DataReceived += ( sender , e ) =>
4553 {
4654 lock ( port )
@@ -63,8 +71,7 @@ public static (int[] raw, int[] mapped) GetSensorValues(MinipadDevice device)
6371 }
6472 } ;
6573
66- // Open the port, send the out command, wait until no value in the array is -1 anymore and safely close it.
67- port . Open ( ) ;
74+ // Send the out command, wait until no value in the array is -1 anymore and safely close it.
6875 port . WriteLine ( "out" ) ;
6976 while ( rawValues . Any ( x => x == - 1 ) )
7077 ;
@@ -75,15 +82,12 @@ public static (int[] raw, int[] mapped) GetSensorValues(MinipadDevice device)
7582 return ( rawValues , mappedValues ) ;
7683 }
7784
78- public static async Task < MinipadDevice > GetDeviceAsync ( int port )
85+ public static async Task < MinipadDevice > GetDeviceAsync ( int portNumber )
7986 {
8087 try
8188 {
8289 // Open the serial port.
83- SerialPort serialPort = new SerialPort ( $ "COM{ port } ", 115200 , Parity . Even , 8 , StopBits . One ) ;
84- serialPort . RtsEnable = true ;
85- serialPort . DtrEnable = true ;
86- serialPort . Open ( ) ;
90+ SerialPort port = Open ( $ "COM{ portNumber } ") ;
8791
8892 // Create a semaphore for timeouting the serial data reading.
8993 SemaphoreSlim semaphoreSlim = new SemaphoreSlim ( 0 ) ;
@@ -93,13 +97,13 @@ public static async Task<MinipadDevice> GetDeviceAsync(int port)
9397 void DataReceivedCallback ( object sender , SerialDataReceivedEventArgs e )
9498 {
9599 // Lock the serial port to make sure it's not being closed when the 200ms timeout runs out.
96- lock ( serialPort )
100+ lock ( port )
97101 {
98102 // Read the data all the way to the end, line by line.
99- while ( serialPort . BytesToRead > 0 )
103+ while ( port . BytesToRead > 0 )
100104 {
101105 // Read the next line.
102- string line = serialPort . ReadLine ( ) . TrimEnd ( '\r ' ) ;
106+ string line = port . ReadLine ( ) . TrimEnd ( '\r ' ) ;
103107
104108 // If the end of the 'get' command was reached (indicated by "GET END"), release the semaphore thus returning.
105109 if ( line == "GET END" )
@@ -116,15 +120,15 @@ void DataReceivedCallback(object sender, SerialDataReceivedEventArgs e)
116120 }
117121
118122 // Subscribe to the callback method, write the 'get' command that returns the keypad specifications.
119- serialPort . DataReceived += DataReceivedCallback ;
120- serialPort . WriteLine ( "get" ) ;
123+ port . DataReceived += DataReceivedCallback ;
124+ port . WriteLine ( "get" ) ;
121125
122126 // Give the response 200ms until this reading process times out.
123127 await semaphoreSlim . WaitAsync ( 10 ) ;
124128
125129 // Safely close the serial port for usage by other processes.
126- lock ( serialPort )
127- serialPort . Close ( ) ;
130+ lock ( port )
131+ port . Close ( ) ;
128132
129133 // Create a Configuration object from the retrieved values, starting with the global settings.
130134 Configuration configuration = new Configuration ( )
@@ -165,17 +169,17 @@ void DataReceivedCallback(object sender, SerialDataReceivedEventArgs e)
165169 } ;
166170
167171 // Create a MinipadDevice object with the parsed info and return it.
168- return new MinipadDevice ( port , DeviceState . CONNECTED , values . ContainsKey ( "version" ) ? values [ "version" ] : null , configuration ) ;
172+ return new MinipadDevice ( portNumber , DeviceState . CONNECTED , values . ContainsKey ( "version" ) ? values [ "version" ] : null , configuration ) ;
169173 }
170174 catch ( UnauthorizedAccessException )
171175 {
172176 // If an UnauthorizedAccessException was thrown, the device is connected but the serial interface occupied by another process.
173- return new MinipadDevice ( port , DeviceState . BUSY , null , new Configuration ( ) ) ;
177+ return new MinipadDevice ( portNumber , DeviceState . BUSY , null , new Configuration ( ) ) ;
174178 }
175179 catch ( FileNotFoundException )
176180 {
177181 // If a FileNotFoundException was thrown, the device is disconnected.
178- return new MinipadDevice ( port , DeviceState . DISCONNECTED , null , new Configuration ( ) ) ;
182+ return new MinipadDevice ( portNumber , DeviceState . DISCONNECTED , null , new Configuration ( ) ) ;
179183 }
180184 }
181185
0 commit comments