📄 lg_comm-cpp
字号:
#include "Stdafx.h"
#include "common.h"
#include "LG_comm.h"
extern char CurPath[], InitFileName[];
extern char IniFile[];
LG_MOTOR_DEF LgPortParam;
int LgReadConfigFlag = 0;
void LgMotorReadConfig( void )
{
char szbuf[128];
MODE_DEF mode;
if ( LgReadConfigFlag )
return;
memset( (char*)&LgPortParam, 0, sizeof(LG_MOTOR_DEF) );
LgReadConfigFlag = 1;
GetCurrentDirectory( 200, CurPath );
sprintf( InitFileName, "%s\\%s", CurPath, IniFile );
GetPrivateProfileString( "LG端口设置", "起始设备号", "", szbuf, 20, InitFileName );
StringCut( (char*)szbuf );
if ( strlen(szbuf) == 0 )
{
MessageBox ( NULL, "LG变频起始设备号未设置,缺省为 0x01 !!!", "提示信息...", MB_OK | MB_ICONQUESTION );
strcpy( szbuf, "1" );
}
LgPortParam.StartDevNo = atoi( szbuf );
GetPrivateProfileString( "LG端口设置", "设备数量", "", szbuf, 20, InitFileName );
StringCut( (char*)szbuf );
if ( strlen(szbuf) == 0 )
{
MessageBox ( NULL, "LG变频设备数量未设置,缺省为 0x01 !!!", "提示信息...", MB_OK | MB_ICONQUESTION );
strcpy( szbuf, "1" );
}
LgPortParam.DevNum = atoi( szbuf );
GetPrivateProfileString( "LG端口设置", "通讯模式", "", szbuf, 20, InitFileName );
StringCut( szbuf );
if ( strlen(szbuf) == 0 )
{
MessageBox ( NULL, "LG通讯方式未设置,缺省为 9600,n,8,1 !!!", "提示信息...", MB_OK | MB_ICONQUESTION );
strcpy( szbuf, "9600,n,8,1" );
}
GetCommMode( &mode, szbuf );
LgPortParam.Mode.Baud = mode.Baud;
LgPortParam.Mode.CommBit = mode.CommBit;
LgPortParam.Mode.Parity = mode.Parity;
LgPortParam.Mode.Stop = mode.Stop;
LgPortParam.SendTimeConst = 1000000/LgPortParam.Mode.Baud/MAIN_TIMER_CONST + 2;
GetPrivateProfileString( "LG端口设置", "通讯端口", "", szbuf, 20, InitFileName );
StringCut( (char*)szbuf );
if ( strlen(szbuf) == 0 )
{
MessageBox ( NULL, "LG计算机串口号未设置,缺省为 'COM1' !!!", "提示信息...", MB_OK | MB_ICONQUESTION );
strcpy( szbuf, "1" );
}
LgPortParam.CommNo = atoi( szbuf );
}
//***************************************************************
//* 初始化 PC 串口端口 *
//***************************************************************
void LgMotorInitCommPort( void )
{
char szbuf[256];
COMMTIMEOUTS timeouts;
DCB dcb;
LgMotorReadConfig( );
if ( LgPortParam.InitOk )
return;
sprintf( szbuf, "COM%d", LgPortParam.CommNo );
LgPortParam.CommId = CreateFile(
(char*)szbuf,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL );
if ( LgPortParam.CommId == (HANDLE)(0xffffffff) )
return;
LgPortParam.Overlapped.Offset = 0;
LgPortParam.Overlapped.OffsetHigh = 0;
LgPortParam.Overlapped.hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
if ( GetCommState( LgPortParam.CommId, &dcb ) == FALSE )
return;
dcb.BaudRate = LgPortParam.Mode.Baud;
dcb.ByteSize = LgPortParam.Mode.CommBit;
dcb.StopBits = LgPortParam.Mode.Stop;
dcb.Parity = LgPortParam.Mode.Parity;
if ( SetCommState( LgPortParam.CommId, &dcb ) == FALSE )
return;
if ( SetupComm( LgPortParam.CommId, MAX_BUF_SIZE, MAX_BUF_SIZE ) == FALSE )
return;
timeouts.ReadIntervalTimeout = -1;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
SetCommTimeouts( LgPortParam.CommId, &timeouts );
LgPortParam.InitOk = TRUE;
}
int LgMotorReadCommPort( char *buf, int len )
{
DWORD rdlen;
int retval;
LgMotorInitCommPort( );
if ( LgPortParam.InitOk == FALSE )
return 0;
retval = ReadFile( LgPortParam.CommId,
buf,
len,
&rdlen,
&LgPortParam.Overlapped
);
if ( retval == TRUE )
return rdlen;
return 0;
}
int LgMotorWriteCommPort( char *buf, int len )
{
DWORD wrlen;
LgMotorInitCommPort( );
if ( LgPortParam.InitOk == 0 )
return 0;
WriteFile( LgPortParam.CommId,
buf,
len,
&wrlen,
&LgPortParam.Overlapped );
return wrlen;
}
void LgMotorDataProcess( void )
{
int devidx;
LgPortParam.SendCmdFlag = 0;
LgPortParam.SendTimeCnt = 0;
switch( LgPortParam.RecvBuf.MsgData[3] )
{
case 'R':
case 'r':
devidx = (HexCharToInt( LgPortParam.RecvBuf.MsgData[1] ) << 4)
| HexCharToInt( LgPortParam.RecvBuf.MsgData[2] );
devidx -= LgPortParam.StartDevNo;
switch( LgPortParam.CurCmdAddr )
{
case CUR_CTRL_ADDR: // 当前控制模式
if ( LgPortParam.RecvBuf.MsgCnt != 11 )
break;
LgPortParam.CurCtrlMode[devidx] = (HexCharToInt( LgPortParam.RecvBuf.MsgData[4] ) << 12)
| (HexCharToInt( LgPortParam.RecvBuf.MsgData[5] ) << 8)
| (HexCharToInt( LgPortParam.RecvBuf.MsgData[6] ) << 4)
| HexCharToInt( LgPortParam.RecvBuf.MsgData[7] );
break;
case CUR_FREQ_ADDR: // 当前设定频率
if ( LgPortParam.RecvBuf.MsgCnt != 11 )
break;
LgPortParam.CurFreq[devidx] = (HexCharToInt( LgPortParam.RecvBuf.MsgData[4] ) << 12)
| (HexCharToInt( LgPortParam.RecvBuf.MsgData[5] ) << 8)
| (HexCharToInt( LgPortParam.RecvBuf.MsgData[6] ) << 4)
| HexCharToInt( LgPortParam.RecvBuf.MsgData[7] );
break;
}
break;
case 'W':
case 'w':
switch( LgPortParam.RecvBuf.MsgData[0] )
{
case ACK:
LgPortParam.SendCmdAckFlag = 1;
break;
case NAK:
LgPortParam.SendCmdAckFlag = 0;
break;
}
break;
}
}
//***************************************************************
//* 读 PC 机端口 LGBus 数据 *
//***************************************************************
void LgMotorReadCommData( void )
{
int len, plen;
u_char c, buf[2048];
len = LgMotorReadCommPort( (char*)buf, 2000 );
if ( len <= 0 )
return;
for ( plen = 0; plen < len; plen++ )
{
c = buf[plen];
switch( LgPortParam.RevStatus )
{
case 0: //Search sync Char
LgPortParam.RecvBuf.MsgData[0] = c;
if ( (c == ACK) || (c == NAK) )
{
LgPortParam.RevStatus = 1;
LgPortParam.RecvBuf.MsgCnt = 1;
}
else
{
LgPortParam.RevStatus = 0;
LgPortParam.RecvBuf.MsgCnt = 0;
}
break;
case 1: // receive real msg body
LgPortParam.RecvBuf.MsgData[LgPortParam.RecvBuf.MsgCnt] = c;
LgPortParam.RecvBuf.MsgCnt++;
if ( c == EOT )
{
LgMotorDataProcess( );
LgPortParam.SendCmdFlag = 0;
LgPortParam.RevStatus = 0;
break;
}
if ( (c == ACK) || (c == NAK) )
{
LgPortParam.RevStatus = 0;
break;
}
break;
default:
LgPortParam.RevStatus = 0;
break;
}
}
}
u_char LgMotorCalCheckSum( u_char *buf, int len )
{
int i;
u_char sum;
sum = 0;
for ( i = 0; i < len; i++ )
{
sum += buf[i];
}
return sum;
}
void LgMotorMakeReadCmd( int devidx, int startaddr )
{
int sum, idx;
char szbuf[256];
if ( devidx >= LgPortParam.DevNum )
return;
idx = 0;
szbuf[idx++] = ENQ;
szbuf[idx++] = DecToHexChar( ((LgPortParam.StartDevNo + devidx) >> 4) & 0x0f );
szbuf[idx++] = DecToHexChar( (LgPortParam.StartDevNo + devidx) & 0x0f );
szbuf[idx++] = 'R';
szbuf[idx++] = DecToHexChar( (startaddr >> 12) & 0x0f );
szbuf[idx++] = DecToHexChar( (startaddr >> 8) & 0x0f );
szbuf[idx++] = DecToHexChar( (startaddr >> 4) & 0x0f );
szbuf[idx++] = DecToHexChar( startaddr & 0x0f );
szbuf[idx++] = '1'; // 固定为1字
sum = LgMotorCalCheckSum( (u_char*)&szbuf[1], idx - 1 );
szbuf[idx++] = DecToHexChar( (sum >> 4) & 0x0f );
szbuf[idx++] = DecToHexChar( sum & 0x0f );
szbuf[idx++] = EOT;
PutDataToBuf( &LgPortParam.PollCmd, szbuf, idx );
}
void LgMotorMakeWriteCmd( int devidx, int startaddr, char *buf, int len )
{
int sum, wlen, idx;
char szbuf[256];
if ( devidx >= LgPortParam.DevNum )
return;
idx = 0;
wlen = len <= 32 ? len : 32;
wlen = (wlen / 4) * 4;
szbuf[idx++] = ENQ;
szbuf[idx++] = DecToHexChar( ((LgPortParam.StartDevNo + devidx) >> 4) & 0x0f );
szbuf[idx++] = DecToHexChar( (LgPortParam.StartDevNo + devidx) & 0x0f );
szbuf[idx++] = 'W';
szbuf[idx++] = DecToHexChar( (startaddr >> 12) & 0x0f );
szbuf[idx++] = DecToHexChar( (startaddr >> 8) & 0x0f );
szbuf[idx++] = DecToHexChar( (startaddr >> 4) & 0x0f );
szbuf[idx++] = DecToHexChar( startaddr & 0x0f );
szbuf[idx++] = (wlen / 4) + '0';
memmove( (char*)&szbuf[idx], buf, wlen );
idx += wlen;
sum = LgMotorCalCheckSum( (u_char*)&szbuf[1], idx - 1 );
szbuf[idx++] = DecToHexChar( (sum >> 4) & 0x0f );
szbuf[idx++] = DecToHexChar( sum & 0x0f );
szbuf[idx++] = EOT;
PutDataToBuf( &LgPortParam.PollCmd, szbuf, idx );
}
void LgMotorMakeStartCmd( int devidx, int dir )
{
char szbuf[256];
if ( dir == 1 ) //
sprintf( szbuf, "%04X", 0x02 );
else if ( dir == 2 )
sprintf( szbuf, "%04X", 0x04 );
else
return;
LgMotorMakeWriteCmd( devidx, CUR_CTRL_ADDR, szbuf, 4 );
LgMotorMakeReadCmd( devidx, CUR_CTRL_ADDR );
}
void LgMotorMakeStopCmd( int devidx )
{
char szbuf[256];
sprintf( szbuf, "%04X", 0 );
LgMotorMakeWriteCmd( devidx, CUR_CTRL_ADDR, szbuf, 4 );
LgMotorMakeReadCmd( devidx, CUR_CTRL_ADDR );
}
void LgMotorMakeRstErrorCmd( int devidx )
{
char szbuf[256];
sprintf( szbuf, "%04X", 0 );
LgMotorMakeWriteCmd( devidx, CUR_CTRL_ADDR, szbuf, 4 );
LgMotorMakeReadCmd( devidx, CUR_CTRL_ADDR );
}
void LgMotorMakeFreqCmd( int devidx, int freq )
{
char szbuf[256];
sprintf( szbuf, "%04X", freq );
LgMotorMakeWriteCmd( devidx, CUR_FREQ_ADDR, szbuf, 4 );
LgMotorMakeReadCmd( devidx, CUR_FREQ_ADDR );
}
void LgMotorSendCmd( void )
{
int len;
char szbuf[256];
if ( LgPortParam.SendCmdFlag )
{
LgPortParam.SendTimeCnt++;
if ( LgPortParam.SendTimeCnt < LgPortParam.SendTimeConst )
return;
LgPortParam.SendCmdFlag = 0;
}
if ( LgPortParam.PollCmd.MsgCnt == 0 )
return;
len = GetDataFormBuf( &LgPortParam.PollCmd, szbuf, 256 );
if ( len == 0 )
return;
LgMotorWriteCommPort( szbuf, len );
if ( szbuf[3] == 'R' )
{
LgPortParam.CurCmdAddr = (HexCharToInt(szbuf[4]) << 12)
| (HexCharToInt(szbuf[5]) << 8)
| (HexCharToInt(szbuf[6]) << 4)
| HexCharToInt(szbuf[7]);
}
LgPortParam.SendCmdFlag = 1;
LgPortParam.SendTimeCnt = 0;
}
void LgMotorTimer( void )
{
int i;
LgMotorReadConfig( );
LgMotorInitCommPort( );
// 定时读当前控制字节
if ( LgPortParam.InitOk )
{
if ( !LgPortParam.ReadCtrlCnt )
{
for( i = 0; i < LgPortParam.DevNum; i++ )
{
LgMotorMakeReadCmd( i, CUR_CTRL_ADDR );
}
}
LgPortParam.ReadCtrlCnt++;
if ( (LgPortParam.ReadCtrlCnt * MAIN_TIMER_CONST) > 1000 )
LgPortParam.ReadCtrlCnt = 0;
}
LgMotorReadCommData( );
LgMotorSendCmd( );
}
u_char LgCheckMotorStatus( int devidx )
{
if ( (LgPortParam.CurCtrlMode[devidx] & 0x06) == 0x02 )
{
return 1;
}
if ( (LgPortParam.CurCtrlMode[devidx] & 0x06) == 0x04 )
{
return 2;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -