@@ -27,81 +27,94 @@ public enum PortStatus
2727 }
2828
2929 private static readonly ILog Logger = LogManager . GetLogger ( typeof ( SerialPortReader ) ) ;
30+
3031 private readonly Config _config = Config . GetInstance ( ) ;
3132 private readonly ServerSocket _serverSocket = new ServerSocket ( ) ;
3233 private readonly Status _status = Status . GetInstance ( ) ;
34+ private readonly Watchdog _tokenWatchdog = new Watchdog ( ) ;
3335
3436 private readonly List < byte > line = new List < byte > ( ) ;
37+
38+ private readonly object SYNC = new object ( ) ;
3539 private string _lastBarcode ;
3640 private DateTime _lastBarcodeDateTime = DateTime . Now ;
3741
3842 private SerialPort _serialPort ;
3943 private int _tokenCounts ;
4044
4145 private TokenStatus _tokenStatus = TokenStatus . Waiting ;
42- private readonly Watchdog _tokenWatchdog = new Watchdog ( ) ;
4346
4447 public event NewBarcodeHandler OnNewBarcode ;
4548 public event NewStatusHandler OnNewStatus ;
4649
4750 public void Start ( )
4851 {
49- Logger . Info ( "SerialPortReader start" ) ;
52+ lock ( SYNC )
53+ {
54+ Logger . Info ( "SerialPortReader start" ) ;
5055
51- Logger . Debug ( "Preparing serial port" ) ;
52- OnNewStatus ? . Invoke ( PortStatus . Preparing ) ;
56+ Logger . Debug ( "Preparing serial port" ) ;
57+ OnNewStatus ? . Invoke ( PortStatus . Preparing ) ;
5358
54- _serialPort = new SerialPort ( _config . SerialPort )
55- {
56- BaudRate = 9600 ,
57- DataBits = 8 ,
58- Parity = Parity . None ,
59- StopBits = StopBits . One ,
60- Handshake = Handshake . None ,
61- ReadTimeout = SerialPort . InfiniteTimeout
62- } ;
63-
64- Logger . Debug ( "Opening serial port" ) ;
65- try
66- {
67- _serialPort . Open ( ) ;
68- }
69- catch ( Exception e )
70- {
71- Logger . Error ( e . Message ) ;
72- OnNewStatus ? . Invoke ( PortStatus . Closed ) ;
73- return ;
74- }
59+ _serialPort = new SerialPort ( _config . SerialPort )
60+ {
61+ BaudRate = 9600 ,
62+ DataBits = 8 ,
63+ Parity = Parity . None ,
64+ StopBits = StopBits . One ,
65+ Handshake = Handshake . None ,
66+ ReadTimeout = SerialPort . InfiniteTimeout
67+ } ;
68+
69+ Logger . Debug ( "Opening serial port" ) ;
70+ try
71+ {
72+ _serialPort . Open ( ) ;
73+ }
74+ catch ( Exception e )
75+ {
76+ Logger . Error ( e . Message ) ;
77+ OnNewStatus ? . Invoke ( PortStatus . Closed ) ;
78+ return ;
79+ }
7580
76- OnNewStatus ? . Invoke ( PortStatus . Open ) ;
81+ OnNewStatus ? . Invoke ( PortStatus . Open ) ;
7782
78- _serialPort . DataReceived += ReadData ;
83+ _serialPort . DataReceived += ReadData ;
7984
80- _tokenWatchdog . OnWatchdogTimeout += TokenRemoved ;
85+ _tokenWatchdog . OnWatchdogTimeout += TokenRemoved ;
86+ }
8187 }
8288
8389 public void Stop ( )
90+
8491 {
85- Logger . Info ( "SerialPortReader stop" ) ;
92+ lock ( SYNC )
93+ {
94+ Logger . Info ( "SerialPortReader stop" ) ;
8695
87- OnNewStatus ? . Invoke ( PortStatus . Closing ) ;
96+ OnNewStatus ? . Invoke ( PortStatus . Closing ) ;
8897
89- _tokenWatchdog . OnWatchdogTimeout -= TokenRemoved ;
98+ _tokenWatchdog . OnWatchdogTimeout -= TokenRemoved ;
9099
91- Logger . Debug ( "Closing serial port" ) ;
100+ Logger . Debug ( "Closing serial port" ) ;
92101
93- _serialPort . DataReceived -= ReadData ;
94102
95- if ( _serialPort != null && _serialPort . IsOpen )
96- try
97- {
98- _serialPort . Close ( ) ;
99- }
100- catch ( IOException )
103+ if ( _serialPort != null && _serialPort . IsOpen )
101104 {
105+ _serialPort . DataReceived -= ReadData ;
106+
107+ try
108+ {
109+ _serialPort . Close ( ) ;
110+ }
111+ catch ( IOException )
112+ {
113+ }
102114 }
103115
104- _serialPort = null ;
116+ _serialPort = null ;
117+ }
105118 }
106119
107120 private void ReadData ( object sender , SerialDataReceivedEventArgs e )
@@ -142,10 +155,7 @@ private void ReadDataLoop()
142155 continue ;
143156
144157 var barcode = ParseString ( line ) ;
145- if ( ! string . IsNullOrEmpty ( barcode ) )
146- {
147- new Thread ( ( ) => { NewSerialLine ( barcode ) ; } ) . Start ( ) ;
148- }
158+ if ( ! string . IsNullOrEmpty ( barcode ) ) new Thread ( ( ) => { NewSerialLine ( barcode ) ; } ) . Start ( ) ;
149159
150160 line . Clear ( ) ;
151161 }
0 commit comments