bool _testStatus = false ;
private void btnOpsStart_Click ( object sender, EventArgs e)
{ int delay = Convert. ToInt32 ( txtdelay. Text. Trim ( ) ) ; txtView. Clear ( ) ; txtView. AppendText ( "******************************************開始烤機********************************" + "\r\n" ) ; Task. Factory. StartNew ( ( ) => { Socket client = new Socket ( AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp) ; _testStatus = true ; try { client. Connect ( new IPEndPoint ( IPAddress. Parse ( txtIp. Text) , Convert. ToInt32 ( txtPort. Text) ) ) ; SetOpsBtnEnable ( false ) ; while ( _testStatus) { for ( int i = 0 ; i <= 4 ; i++ ) { for ( int j = 0 ; j <= 4 ; j++ ) { for ( int k = 0 ; k <= 4 ; k++ ) { for ( int l = 0 ; l <= 4 ; l++ ) { if ( ! _testStatus) { return ; } string send = $"Configure:WorkChannelAll { i } { j } { k } { l } " ; ShowMsg ( send) ; client. Send ( Encoding. ASCII. GetBytes ( send + "\n" ) ) ; Thread. Sleep ( delay) ; string select = $"Configure:WorkChannelAll?" ; ShowMsg ( select ) ; client. Send ( Encoding. ASCII. GetBytes ( select + "\n" ) ) ; string result = string . Empty; string errMsg = string . Empty; ReadResult ( client, ref result, ref errMsg) ; ShowMsg ( result) ; result = result. Replace ( "\n" , "" ) ; string [ ] resArr = result. Split ( ' ' ) ; if ( i!= Convert. ToInt32 ( resArr[ 0 ] ) ) { _testStatus = false ; ShowMsgBox ( $"配置 { send } 和查詢 { result } 不一致!" ) ; } if ( j != Convert. ToInt32 ( resArr[ 1 ] ) ) { _testStatus = false ; ShowMsgBox ( $"配置 { send } 和查詢 { result } 不一致!" ) ; } if ( k != Convert. ToInt32 ( resArr[ 2 ] ) ) { _testStatus = false ; ShowMsgBox ( $"配置 { send } 和查詢 { result } 不一致!" ) ; } if ( l != Convert. ToInt32 ( resArr[ 3 ] ) ) { _testStatus = false ; ShowMsgBox ( $"配置 { send } 和查詢 { result } 不一致!" ) ; } } } } } } } catch ( Exception ex) { MessageBox. Show ( ex. ToString ( ) ) ; _testStatus = false ; } finally { client. Shutdown ( SocketShutdown. Both) ; } } ) . ContinueWith ( ( task) => { SetOpsBtnEnable ( true ) ; } ) ;
}
public bool ReadResult ( Socket client, ref string result, ref string errMsg)
{ try { lock ( _lockWr) { DateTime triggerStart = DateTime. Now; bool pass = true ; List< byte > listbyte = new List< byte > ( ) ; while ( pass) { DateTime triggerEnd = DateTime. Now; double collecTime = ( triggerEnd - triggerStart) . TotalMilliseconds; if ( collecTime >= 10000 ) { errMsg = "Read time over-limit" ; return false ; } int relength = client. Available; byte [ ] rebyte = new byte [ relength] ; client. Receive ( rebyte) ; foreach ( byte a in rebyte) { listbyte. Add ( a) ; } if ( listbyte. Count != 0 ) { if ( listbyte. Last ( ) == 10 || listbyte. Last ( ) == 13 ) { pass = false ; } } } byte [ ] readbyt1 = new byte [ listbyte. Count] ; for ( int a = 0 ; a < listbyte. Count; a++ ) { readbyt1[ a] = listbyte[ a] ; } result = System. Text. ASCIIEncoding. Default. GetString ( readbyt1) ; return true ; } } catch ( Exception ex) { errMsg = ex. ToString ( ) ; return false ; }
}
private void btnOpsStop_Click ( object sender, EventArgs e) { _testStatus = false ; } public void ShowMsg ( string msg) { this . Invoke ( new Action ( ( ) => { if ( txtView. Lines. Length > 6000 ) { txtView. Clear ( ) ; } DateTime date = DateTime. Now; this . txtView. AppendText ( $@" { date. ToString ( "yyyy-MM-dd HH:mm:ss" ) } { msg } { "\r\n" } " ) ; } ) ) ; } public void SetOpsBtnEnable ( bool isEnable) { this . Invoke ( new Action ( ( ) => { btnOpsStart. Enabled = isEnable; btnSameChannelStart. Enabled = isEnable; btnOpsStop. Enabled = ! isEnable; } ) ) ; } public void ShowMsgBox ( string ErrMsg) { this . Invoke ( new Action ( ( ) => { MessageBox. Show ( ErrMsg) ; } ) ) ; }