11using System ;
22using System . Collections . Specialized ;
33using System . ComponentModel ;
4- using System . Diagnostics ;
54using System . IO . Ports ;
65using System . Linq ;
76using System . Text ;
87using System . Windows ;
98using System . Windows . Threading ;
9+ using NLog ;
1010
1111namespace ServoPIDControl
1212{
@@ -35,9 +35,11 @@ internal enum ServoParam : byte
3535
3636 public class ArduinoCom : IDisposable
3737 {
38- private SerialPort _port ;
38+ private static readonly Logger Log = LogManager . GetCurrentClassLogger ( ) ;
39+
40+ private ISerialPort _port ;
3941 private readonly StringBuilder _readBuf = new StringBuilder ( ) ;
40- private ArduinoModel _model ;
42+ private Model _model ;
4143 private readonly DispatcherTimer _timer = new DispatcherTimer { Interval = TimeSpan . FromMilliseconds ( 250 ) } ;
4244 private readonly object _portLock = new object ( ) ;
4345
@@ -46,7 +48,7 @@ public ArduinoCom()
4648 _timer . Tick += TimerOnTick ;
4749 _timer . Start ( ) ;
4850
49- MessageReceived += ( s , a ) => Debug . WriteLine ( $ "Received: { a . Message } ") ;
51+ MessageReceived += ( s , a ) => Log . Info ( $ "Received: { a . Message } ") ;
5052 }
5153
5254 private void TimerOnTick ( object sender , EventArgs e )
@@ -94,7 +96,7 @@ private void LineReceived(string line)
9496 }
9597 catch ( Exception e )
9698 {
97- Debug . WriteLine ( $ "Bad DT received: { line } - { e . Message } ") ;
99+ Log . Error ( $ "Bad DT received: { line } - { e . Message } ") ;
98100 }
99101
100102 return ;
@@ -109,7 +111,7 @@ private void LineReceived(string line)
109111
110112 if ( numServos >= 33 )
111113 {
112- Debug . WriteLine ( "Too many servos: " + numServos ) ;
114+ Log . Error ( "Too many servos: " + numServos ) ;
113115 return ;
114116 }
115117
@@ -135,7 +137,7 @@ private void LineReceived(string line)
135137 }
136138 catch ( Exception e )
137139 {
138- Debug . WriteLine ( "Bad servo data: " + line + " - " + e . Message ) ;
140+ Log . Error ( "Bad servo data: " + line + " - " + e . Message ) ;
139141 return ;
140142 }
141143 }
@@ -153,7 +155,7 @@ private void LineReceived(string line)
153155 }
154156 catch ( Exception e )
155157 {
156- Debug . WriteLine ( $ "Bad servo data: { line } - { e . Message } ") ;
158+ Log . Error ( $ "Bad servo data: { line } - { e . Message } ") ;
157159 return ;
158160 }
159161 }
@@ -166,14 +168,14 @@ private void LineReceived(string line)
166168 }
167169 else
168170 {
169- Debug . WriteLine ( $ "Ignored: { line } ") ;
171+ Log . Warn ( $ "Ignored: { line } ") ;
170172 return ;
171173 }
172174
173175 MessageReceived ? . Invoke ( this , new StringEventArgs { Message = line } ) ;
174176 }
175177
176- public ArduinoModel Model
178+ public Model Model
177179 {
178180 get => _model ;
179181 set
@@ -251,10 +253,10 @@ private void ServosOnCollectionChanged(object sender, NotifyCollectionChangedEve
251253
252254 private void ModelOnPropertyChanged ( object sender , PropertyChangedEventArgs e )
253255 {
254- if ( e . PropertyName == null || e . PropertyName == nameof ( Model . Enabled ) )
255- SendCommand ( Command . EnableRegulator , ( byte ) ( Model . Enabled ? 1 : 0 ) ) ;
256+ if ( e . PropertyName == null || e . PropertyName == nameof ( Model . PidEnabled ) )
257+ SendCommand ( Command . EnableRegulator , ( byte ) ( Model . PidEnabled ? 1 : 0 ) ) ;
256258
257- if ( e . PropertyName == null || e . PropertyName == nameof ( Model . PortName ) )
259+ if ( e . PropertyName == null || e . PropertyName == nameof ( Model . ConnectedPort ) )
258260 ConnectPort ( ) ;
259261
260262 if ( e . PropertyName == null || e . PropertyName == nameof ( Model . PollPidData ) )
@@ -270,7 +272,7 @@ private void SendCommand(Command cmd, params byte[] data)
270272 if ( _port == null || ! _port . IsOpen )
271273 return ;
272274
273- Debug . WriteLine ( $ "Sending { cmdData . Length } : { BitConverter . ToString ( cmdData ) } ") ;
275+ Log . Debug ( $ "Sending { cmdData . Length } : { BitConverter . ToString ( cmdData ) } ") ;
274276 _port . Write ( cmdData , 0 , cmdData . Length ) ;
275277 }
276278 }
@@ -303,21 +305,19 @@ private void ConnectPort()
303305 }
304306 }
305307
306- if ( Model ? . PortName == null )
308+ if ( Model ? . ConnectedPort == null )
307309 return ;
308310
309311 try
310312 {
311- _port = new SerialPort ( Model . PortName )
313+ _port = new SerialPort ( Model . ConnectedPort )
312314 {
313315 BaudRate = 115200 ,
314- NewLine = "\n " ,
315- ReadBufferSize = 4096 ,
316- WriteBufferSize = 4096 ,
316+ NewLine = "\n "
317317 } ;
318318 _port . Open ( ) ;
319319
320- Debug . WriteLine ( "Sending: RST" ) ;
320+ Log . Info ( "Sending: RST" ) ;
321321 _port . WriteLine ( "RST" ) ;
322322 _port . DataReceived += PortOnDataReceived ;
323323
0 commit comments