MSegment.cpp
资源名称:DELTA_PLC.rar [点击查看]
上传用户:hailongwei
上传日期:2020-11-29
资源大小:5839k
文件大小:6k
源码类别:
单片机开发
开发平台:
Visual C++
- // MSegment.cpp : CMSegment 的实现
- #include "stdafx.h"
- #include "MSegment.h"
- // CMSegment
- //接受串口数据
- LRESULT CMSegment::OnReceivedData( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
- {
- long len;
- char str[100];
- len = comm.Read( str, 100 );
- if ( len > 0 ) doPraseString( str );
- return 0;
- }
- long CMSegment::doChartToLong(char s)
- {
- switch( s )
- {
- case '0':
- return 0X0;
- break;
- case '1':
- return 0X1;
- break;
- case '2':
- return 0X2;
- break;
- case '3':
- return 0X3;
- break;
- case '4':
- return 0X4;
- break;
- case '5':
- return 0X5;
- break;
- case '6':
- return 0X6;
- break;
- case '7':
- return 0X7;
- break;
- case '8':
- return 0X8;
- break;
- case '9':
- return 0X9;
- break;
- case 'A':
- return 0XA;
- break;
- case 'B':
- return 0XB;
- break;
- case 'C':
- return 0XC;
- break;
- case 'D':
- return 0XD;
- break;
- case 'E':
- return 0XE;
- break;
- case 'F':
- return 0XF;
- break;
- default:
- return 0X0;
- break;
- }
- }
- void CMSegment::doPraseString( char* ps )
- {
- int i = 0;
- long data0 = 0;
- char* s = ps;
- do
- {
- switch ( *s )
- {
- case 'x3A':
- bz = 1;
- m_index = 0;
- m_Char1[0] = *s;
- break;
- case 'x0A':
- if ( bz = 1 )
- {
- bz = 0;
- if ( m_Char1[1] == 'x30' && m_Char1[2] == 'x31' && m_Char1[3] == 'x30' && m_Char1[4] == 'x32' )
- {
- for ( i = 0; i < 32; i++ )
- {
- data0 = 0;
- data0 += doChartToLong( m_Char1[ 7 + 2 * i ] ) * 0X10;
- data0 += doChartToLong( m_Char1[ 8 + 2 * i ] ) ;
- data[ 8 * i + 7 ] =data0 & 0X80;
- data[ 8 * i + 6 ] =data0 & 0X40;
- data[ 8 * i + 5 ] =data0 & 0X20;
- data[ 8 * i + 4 ] =data0 & 0X10;
- data[ 8 * i + 3 ] =data0 & 0X8;
- data[ 8 * i + 2 ] =data0 & 0X4;
- data[ 8 * i + 1 ] =data0 & 0X2;
- data[ 8 * i + 0 ] =data0 & 0X1;
- }
- //if ( m_DataReceived )
- //{
- //通知信号变化
- for ( i = 0; i < 256; i++ )
- {
- if ( m_notify[i] )
- {
- if ( data[i] ^ m_data[i] )
- {
- ::PostMessage( m_hWndCD, WM_MYSELF, 1, i );
- }
- }
- m_data[ i ] = data[ i ];
- }
- //}
- //for ( i = 0; i < 256; i++ )
- //{
- // m_data[ i ] = data[ i ];
- //}
- m_DataReceived = true;
- }
- }
- break;
- default:
- if ( bz = 1 )
- {
- bz = 0;
- m_index++;
- if ( m_index < 300 ) m_Char1[m_index] = *s;
- }
- break;
- }
- s++;
- }
- while( *s );
- }
- //延时
- void CMSegment::doDelay( long TimeSpan )
- {
- long dwStart = GetTickCount();
- long dwEnd = dwStart;
- do
- {
- MSG msg;
- if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
- {
- GetMessage( &msg, NULL, 0, 0 );
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- dwEnd = GetTickCount() - dwStart;
- } while( dwEnd < TimeSpan );
- }
- //请求联机用的延时判断
- bool CMSegment::doWaitOpenLink( void )
- {
- bool waitresult = false;
- m_DataReceived = false;
- DWORD dwStart = GetTickCount();
- DWORD dwEnd = dwStart;
- do
- {
- if ( m_DataReceived )
- {
- waitresult = true;
- break;
- }
- MSG msg;
- if ( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
- {
- GetMessage( &msg, NULL, 0, 0 );
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- dwEnd = GetTickCount() - dwStart;
- } while ( dwEnd < 2000 );
- return waitresult;
- }
- //初始化变量
- void CMSegment::doinit( void )
- {
- for ( int i = 0; i < 256; i++ )
- {
- data[i] = false;
- m_data[i] = false;
- }
- bz = 0;
- m_index = 0;
- m_link = false;
- m_DataReceived = false;
- m_allowsend = true;
- m_NoneInterval = 0;
- }
- //联机
- STDMETHODIMP CMSegment::OpenLink(LONG Comport, BSTR CommString)
- {
- //取串口通讯协议
- CString str1( CommString );
- SysFreeString( CommString );
- KillTimer( IDT_TIMER1 );
- if ( comm.IsOpen() ) comm.Close();
- doinit();
- if ( comm.Open( Comport, ( char* )( LPCTSTR )str1 ) )//打开串口,通讯协议9600,e,7,1
- {
- comm.SetWnd( m_hWndCD );
- comm.Write( ":0102080000FFF6x0Dx0A" );//发送读取状态
- if( doWaitOpenLink() )
- {
- m_link = true;
- SetTimer( IDT_TIMER1 , 500 , NULL );
- }
- }
- Fire_OnLink( m_link ? VARIANT_TRUE : VARIANT_FALSE );
- return S_OK;
- }
- //脱机
- STDMETHODIMP CMSegment::CloseLink(void)
- {
- KillTimer( IDT_TIMER1 );
- comm.Close();
- doinit();
- ::PostMessage( m_hWndCD, WM_MYSELF, 2, 0 );
- return S_OK;
- }
- //读取联机状态
- STDMETHODIMP CMSegment::GetLink(VARIANT_BOOL* pVal)
- {
- * pVal = m_link ? VARIANT_TRUE : VARIANT_FALSE;
- return S_OK;
- }
- //读取寄存器
- STDMETHODIMP CMSegment::GetBit(LONG Addr, VARIANT_BOOL* pVal)
- {
- if ( Addr < 0X0 || Addr > 0XFF )
- {
- *pVal = VARIANT_FALSE;
- }
- else
- {
- *pVal = data[Addr] ? VARIANT_TRUE : VARIANT_FALSE;
- }
- return S_OK;
- }
- //写入寄存器
- STDMETHODIMP CMSegment::PutBit(LONG Addr, VARIANT_BOOL OnOff)
- {
- if ( Addr < 0X0 || Addr > 0XFF ) return S_OK;
- Addr += 0X0800;
- //MODBUSASCII LRC校验码计算
- long checkcode = 0X01 + 0X05 + ( ( Addr & 0XFF00 ) >> 8 ) + ( Addr & 0XFF ) + ( OnOff ? 0XFF : 0X00 ) + 0X00;
- checkcode = ~checkcode;
- checkcode += 0X01;
- checkcode &= 0XFF;
- char s[32];
- sprintf( s, ":0105%04X%02X00%02Xx0Dx0A", Addr, OnOff ? 0XFF : 0X00, checkcode );
- m_allowsend = false;
- doDelay( 20 );
- comm.Write( s );
- doDelay( 20 );
- m_allowsend = true;
- //MessageBox(s);
- return S_OK;
- }
- //通知信号变化
- STDMETHODIMP CMSegment::SetNotify(LONG Addr, VARIANT_BOOL AllowNotify)
- {
- if ( Addr < 0X0 || Addr > 0XFF )
- {
- return S_OK;
- }
- else
- {
- m_notify[Addr] = AllowNotify;
- }
- return S_OK;
- }
- //延时
- STDMETHODIMP CMSegment::Delay( LONG TimeSpan )
- {
- doDelay( TimeSpan );
- return S_OK;
- }
- LRESULT CMSegment::OnTimer(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
- {
- if ( m_allowsend )
- {
- comm.Write( ":0102080000FFF6x0Dx0A" );//发送读取状态
- }
- //检查联机
- if ( !m_DataReceived )
- {
- m_NoneInterval++;
- if ( m_NoneInterval > 20 ) CloseLink();
- }
- else
- {
- m_NoneInterval = 0;
- }
- m_DataReceived = false;
- return 0;
- }
- //MySelf事件
- LRESULT CMSegment::OnMySelf( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
- {
- //::PostMessage( m_hWndCD, WM_MYSELF, 5, DMC_SERVO_OFF);
- switch( wParam )
- {
- case 1:
- Fire_OnNotify( (LONG)lParam );
- break;
- case 2:
- Fire_OnLink( lParam == 0 ? VARIANT_FALSE : VARIANT_TRUE );
- break;
- default:
- break;
- }
- return 0;
- }