📄 commport.c
字号:
{
SendDlgItemMessage( hDlg, IDC_BASE, WM_GETTEXT, 20, (LPARAM) ((LPSTR)szBuffer));
ComCfg.usPortAddress = (WORD)StringToDec( szBuffer, 16);
SendDlgItemMessage( hDlg, IDC_BASE, EM_SETMODIFY, FALSE, 0);
}
case IDCANCEL:
EndDialog(hDlg, 0);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
//------------------------------------------------------------------
// FUNCTION : AddControlString
//
// PURPOSE : Initializes the combobox strings in the dialog box
//
// PARAMETERS : hDlg (IN) - dialog box window handle
//
// RETURN : NONE
//
// Call/Called procedure cross reference :
// Call Called Explanation
// ---- ------- -----------
// ConfigDlgProc
//------------------------------------------------------------------
PRIVATE void AddControlString
(
HWND hDlg
)
{
int i;
// Create list string for baud rate
i = 0;
while (SpeedString[i])
{
SendDlgItemMessage(hDlg, IDC_BDRATE, CB_ADDSTRING, 0,
(LPARAM) (LPSTR) SpeedString[i]);
i++;
}
// Create list string for data bits
i = 0;
while (DataBitsString[i])
{
SendDlgItemMessage(hDlg, IDC_DATABITS, CB_ADDSTRING, 0,
(LPARAM) (LPSTR) DataBitsString[i]);
i++;
}
// Create list string for parity check
i = 0;
while (ParityString[i])
{
SendDlgItemMessage(hDlg, IDC_PARITY, CB_ADDSTRING, 0,
(LPARAM) (LPSTR) ParityString[i]);
i++;
}
// Create list string for stop bits
i = 0;
while (StopBitString[i])
{
SendDlgItemMessage(hDlg, IDC_STOPBITS, CB_ADDSTRING, 0,
(LPARAM) (LPSTR) StopBitString[i]);
i++;
}
// Create list string for transmission mode
SendDlgItemMessage(hDlg, IDC_TXMODE, CB_ADDSTRING, 0,
(LPARAM) (LPSTR) TxModeString[0]);
SendDlgItemMessage(hDlg, IDC_TXMODE, CB_ADDSTRING, 0,
(LPARAM) (LPSTR) TxModeString[1]);
}
//------------------------------------------------------------------
// FUNCTION : SetHardwareSettings
//
// PURPOSE : Initializes the dialog box
//
// PARAMETERS : hDlg (IN) - dialog box window handle
// lpDevCfg (IN) - device info. location
//
// RETURN : NONE
//
// Call/Called procedure cross reference :
// Call Called Explanation
// ---- ------- -----------
// ConfigDlgProc
//------------------------------------------------------------------
PRIVATE void SetHardwareSettings
(
HWND hDlg,
DEVCONFIG_COM FAR *lpDevCfg
)
{
// configure communication port
_itoa((int)lpDevCfg->usCommPort,(char *)szBuffer,10);
SendDlgItemMessage(hDlg,IDC_COMPORT,EM_REPLACESEL,0,
(LPARAM)((LPSTR)szBuffer));
// select baud rate
_ultoa(lpDevCfg->dwBaudRate,szBuffer,10);
SendDlgItemMessage(hDlg,IDC_BDRATE,CB_SELECTSTRING,(WPARAM)-1,
(LPARAM)((LPSTR)szBuffer));
// select parity check
SendDlgItemMessage(hDlg, IDC_PARITY, CB_SETCURSEL,
(WPARAM)lpDevCfg->usParity, (LPARAM)((LPSTR)0));
// select data bits
_itoa((int)lpDevCfg->usDataBits,szBuffer,10);
SendDlgItemMessage(hDlg,IDC_DATABITS,CB_SELECTSTRING,(WPARAM)-1,
(LPARAM)((LPSTR)szBuffer));
// select stop bits
SendDlgItemMessage(hDlg, IDC_STOPBITS, CB_SETCURSEL,
(WPARAM)lpDevCfg->usStopBits, (LPARAM)((LPSTR)0));
// select transmission mode
SendDlgItemMessage(hDlg, IDC_TXMODE, CB_SETCURSEL,
(WPARAM)lpDevCfg->usTxMode, (LPARAM)((LPSTR)0));
EnableWindow(GetDlgItem(hDlg,IDC_BASE),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_PORTADR),FALSE);
EnableWindow(GetDlgItem(hDlg,IDC_HEX),FALSE);
return;
}
//--------------------------------------------------------------------------
// FUNCTION : CommandDlgProc
//
// PURPOSE : the window function for "COMMAND" windows, which
// does the message processing, in response to
//
// WM_INITDIALOG : Setups the dialog box state
// WM_COMMAND : Passes control to a command-handling
// function.
//
// PARAMETERS : hDlg (IN) - window handle
// msg (IN) - message ID
// wParam (IN) - related to msg
// lParam (IN) - related to msg
//
// RETURNS : TRUE if processing
// FALSE if not processing
//--------------------------------------------------------------------------
BOOL FTYPE CommandDlgProc
(
HWND hDlg, // window handle
unsigned message, // type of message
WPARAM wParam, // additional information
LPARAM lParam // additional information
)
{
USHORT usLen;
USHORT usByte;
switch (message)
{
case WM_INITDIALOG :
//
// open COM port
//
ErrCde = COMOpen(ComCfg.usCommPort, (LONG far *)&hComm,
(LONG far *)&DeviceHandle);
if (ErrCde != SUCCESS)
{
strcpy(szErrMsg,"COM Open Failed!");
MessageBox(hMainWnd,(LPCSTR)szErrMsg,"Notice",MB_OK);
return TRUE;
}
//
// configure COM port
//
ErrCde = COMSetConfig(DeviceHandle, (LPDEVCONFIG_COM)&ComCfg);
if (ErrCde != SUCCESS)
{
strcpy(szErrMsg,"COM Configure Failed!");
MessageBox(hMainWnd,(LPCSTR)szErrMsg,"Notice",MB_OK);
}
// set text limit
SendDlgItemMessage(hDlg,IDC_COMMAND,EM_LIMITTEXT,30,(LPARAM)((LPSTR)0));
SendDlgItemMessage(hDlg,IDC_RESPONSE,EM_LIMITTEXT,30,(LPARAM)((LPSTR)0));
return TRUE;
case WM_COMMAND :
switch (LOWORD(wParam))
{
case IDCANCEL :
//
// close COM port
//
COMClose((LONG far *)&DeviceHandle);
EndDialog(hDlg, 0);
return TRUE;
case IDC_READ :
//
// read response from COM port
//
if ((ErrCde = COMRead(DeviceHandle, (LPSTR)szBuffer, 39,
TIMEOUT, TerminateChar, (USHORT far *)&usByte)) != SUCCESS)
{
strcpy(szErrMsg,"COM Read Failed!");
MessageBox(hDlg,(LPCSTR)szErrMsg,"Notice",MB_OK);
}
else
{
//
// display response string in IDC_RESPONSE
//
SendDlgItemMessage(hDlg, IDC_RESPONSE, WM_SETTEXT, 0,
(LPARAM)((LPSTR)szBuffer));
}
return TRUE;
case IDC_WRITE :
//
// get command string from IDC_COMMAND
//
SendDlgItemMessage(hDlg, IDC_COMMAND, WM_GETTEXT, 30,
(LPARAM)(LPSTR)szBuffer) ;
// append terminate string
usLen = strlen(szBuffer);
szBuffer[usLen] = TerminateChar;
usLen ++;
szBuffer[usLen] = '\0';
//
// write command to COM port
//
if ((ErrCde = COMWrite(DeviceHandle, (LPSTR)szBuffer, usLen))
!= (LONG)SUCCESS)
{
strcpy(szErrMsg,"COM Write Failed!");
MessageBox(hDlg,(LPCSTR)szErrMsg,"Notice",MB_OK);
}
return TRUE;
}
break;
}
return FALSE;
}
//------------------------------------------------------------------
// FUNCTION : StringToDec
//
// PURPOSE : This function is used to do string conversion to base
// by 16 or base by 10.. etc.
//
// PARAMETERS : szStr (IN) - data string to be converted
// nBase (IN) - conversion base
//
// RETURN : conversion numeric data
//
// Call/Called procedure cross reference :
// Call Called Explanation
// ---- ------ -----------
// Power
// ConfigDlgProc
//------------------------------------------------------------------
PRIVATE int StringToDec(char *szStr, int nBase)
{
int i, j, nValue, nResult;
char cNum;
nResult = 0;
for (i = strlen(szStr) - 1, j = 0; i >= 0; --i, ++j)
{
cNum = *(szStr + i);
if (cNum >= '0' && cNum <= '9')
nValue = (int)(cNum - '0');
else {
cNum = tolower(cNum);
nValue = (int)(cNum - 'a') + 10;
}
nResult = nResult + Power(nBase, j)* nValue;
}
return nResult;
}
//------------------------------------------------------------------
// FUNCTION : Power
//
// PURPOSE : This function is used to do support StringToDec
// function.
//
// PARAMETERS : nBase(IN), nPow(IN)
//
// RETURN : Value (success)
//
// Call/Called procedure cross reference :
// Call Called Explanation
// ---- ------ -----------
// StringToDec
//------------------------------------------------------------------
PRIVATE int Power(int nBase, int nPow)
{
int nValue, i;
if (nPow == 0)
return 1;
nValue = 1;
for(i = nPow; i > 0; i--)
nValue = nValue * nBase;
return nValue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -