📄 bincom.c
字号:
//* --------------------------------------------------------------------------
//* ATMEL Microcontroller Software Support - ROUSSET -
//* --------------------------------------------------------------------------
//* File Name : binCom.c
//* Treatments : Send datas to serial port
//* Translator : VISUAL C++ compiler v5
//*
//* Imported Resources : resource.h
//* binCom.rc
//* Comment:
//* CommandLine = binCom (-b<Baud rate>) (-d<Data bits number>)
//* (-c<COM port>) (-p<Parity>)
//* Parity: o =odd, e = even, n = none
//*
//* 1.0 03/03/98 SDC : Creation
//* 1.1 26/08/98 SDC : Better UI,
//* "Cancel during configuration" bug fixed
//* --------------------------------------------------------------------------
//----- Files to be included Definition -----
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "resource.h"
//----- Types and Constants Definition -----
HINSTANCE hInst; // Application instance
HWND Hwnd = NULL; // window handle
BOOL stillConfig = FALSE;// Boolean TRUE when a configuration was done
char comPort[6] = "COM1"; // COM Port name
COMMCONFIG commConfig; // COM user parameters (dwSize = 0 when none)
HANDLE hCom; // Handle to COM device
HANDLE hBin; // Handle to memory buffer
//* --------------------------------------------------------------------------
//* Function Name : displayError
//* Object : Display a message box describing a Windows error
//* Input Parameters : none
//* Output Parameters : none
//* Functions called : none
//* --------------------------------------------------------------------------
void displayError(void)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,0,NULL);
MessageBox( NULL,lpMsgBuf,"Error", MB_OK|MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
}
//* --------------------------------------------------------------------------
//* Function Name : readBinFile
//* Object : Read binary file
//* Input Parameters : ofn = OPENFILENAME structure
//* Output Parameters : Buffer handler if success, NULL else
//* Functions called : none
//* --------------------------------------------------------------------------
HLOCAL readBinFile(OPENFILENAME ofn)
{
HANDLE hf;
long taille,cbRead;
BYTE *bin;
HGLOBAL hBin;
BOOL trouve=FALSE;
// Open file (return if error)
hf=CreateFile(ofn.lpstrFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hf == INVALID_HANDLE_VALUE)
{
displayError();
return NULL;
}
// Get file size (return if error)
taille=GetFileSize(hf,NULL);
if (taille==0xFFFFFFFF)
{
displayError();
CloseHandle(hf);
return NULL;
}
// Allocate a buffer (return if error)
hBin=GlobalAlloc(GMEM_MOVEABLE,taille);
if (hBin==NULL)
{
displayError();
CloseHandle(hf);
return NULL;
}
// Fill the buffer (return NULL if error)
bin=GlobalLock(hBin);
if (!ReadFile(hf,bin,taille,&cbRead,NULL) || cbRead<taille)
{
displayError();
GlobalUnlock(hBin);
CloseHandle(hf);
return NULL;
}
// close file handle, unlock buffer and return Buffer handle
CloseHandle(hf);
GlobalUnlock(hBin);
return hBin;
}
//* --------------------------------------------------------------------------
//* Function Name : openCom
//* Object : Open COM port
//* Input Parameters : COM Port name
//* Output Parameters : hCom
//* Functions called : none
//* --------------------------------------------------------------------------
HANDLE openCom(LPCTSTR name)
{
DWORD sizeCommConfig = sizeof(COMMCONFIG);
// open COM
hCom = CreateFile(name,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attrs
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
// If commConfig never filled, try to fill it
if ((hCom != INVALID_HANDLE_VALUE) && !commConfig.dwSize)
GetCommConfig(hCom, &commConfig, &sizeCommConfig);
return hCom;
}
//* --------------------------------------------------------------------------
//* Function Name : configCommand
//* Object : Process configuration command
//* Input Parameters : none
//* Output Parameters : TRUE if config done, FALE if "Cancel"
//* Functions called : none
//* --------------------------------------------------------------------------
BOOL configCommand()
{
// Get COM Port name
SendDlgItemMessage(Hwnd, IDC_EDIT1, WM_GETTEXT, 6,
(LPARAM)comPort);
// open COM, to see if it exists (return if error)
if (openCom(comPort) == INVALID_HANDLE_VALUE)
{
displayError();
return FALSE;
}
// Close COM and set stillConfig BOOLEAN
CloseHandle(hCom);
stillConfig = TRUE;
// Display dialog box (return result)
return CommConfigDialog(comPort, NULL, &commConfig);
}
//* --------------------------------------------------------------------------
//* Function Name : sendCommand
//* Object : Send Command processing
//* Input Parameters : none
//* Output Parameters : none
//* Functions called : none
//* --------------------------------------------------------------------------
void sendCommand()
{
BYTE *bin;
DWORD nbWritten, binSize;
OPENFILENAME ofn;
hBin = NULL;
// Get COM Port name
SendDlgItemMessage(Hwnd, IDC_EDIT1, WM_GETTEXT, 6,
(LPARAM)comPort);
// Display "Open File" dialog box
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = Hwnd;
ofn.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY;
ofn.lpstrFile = "";
ofn.nMaxFile=256;
// File a buffer with file content if file selected
if (GetOpenFileName(&ofn)!=0)
hBin = readBinFile(ofn);
// return if nothing selected or error
if (!hBin)
{
MessageBox(Hwnd, "Nothing to send", "Nothing to send", MB_OK);
return;
}
// Display COM config dialog box, if no config. done
if (!stillConfig)
if (!configCommand())
return;
// open COM (return if error)
if (openCom(comPort) == INVALID_HANDLE_VALUE)
{
displayError();
return;
}
// Set commConfig properties to this COM
SetCommConfig(hCom, &commConfig, sizeof(COMMCONFIG));
// Display progress window
SendDlgItemMessage(Hwnd, IDC_STATUS, WM_SETTEXT, 0,
(LPARAM)"Transfert in progress...");
UpdateWindow(Hwnd);
// Lock memory buffer
binSize = LocalSize(hBin);
bin = LocalLock(hBin);
// Tranfert file (return if error)
if (!WriteFile(hCom, bin, binSize, &nbWritten, NULL))
{
displayError();
CloseHandle(hCom);
GlobalUnlock(hBin);
GlobalFree(hBin);
return;
}
// Parse errors ,free memory, close handle
SendDlgItemMessage(Hwnd, IDC_STATUS, WM_SETTEXT, 0, (LPARAM)".");
if (nbWritten < binSize)
{
char str[200];
sprintf(str, "%ld/%ld bytes sent", nbWritten, binSize);
MessageBox(Hwnd, str, "error", MB_OK);
}
else
MessageBox(Hwnd, "Transfert completed", "Success", MB_OK);
GlobalUnlock(hBin);
GlobalFree(hBin);
CloseHandle(hCom);
}
//* --------------------------------------------------------------------------
//* Function Name : processCmdLine
//* Object : Process command line
//* Functions called : none
//* --------------------------------------------------------------------------
void processCmdLine(LPSTR lpszCmdParam)
{
int br = -1;
int db = -1;
char p = 0;
char cp[6]="COM1";
// Parse arguments
char *token = strtok(lpszCmdParam, " \t\r\n");
while( token != NULL )
{
// Baud rate option
if (strnicmp(token, "-b", 2) == 0)
br = atoi(token+2);
// Data bits option
else if (strnicmp(token, "-d", 2) == 0)
db = atoi(token+2);
// Parity bits option
else if (strnicmp(token, "-p", 2) == 0)
p = *(token+2);
// COM port option
else if (strnicmp(token, "-c", 2) == 0)
strncpy(cp, token+2,5);
// Unknown option
else return;
// Get next token
token = strtok(NULL, " \t\r\n");
}
// Try to open COM to fill commConfig (return if error)
if (openCom(cp) == INVALID_HANDLE_VALUE)
return;
// Close COM and Fill comPort global.
strcpy(comPort, cp);
CloseHandle(hCom);
// Return if error in command line or no options
if (!strlen(lpszCmdParam) || !br || !db ||
(tolower(p)!='o' && tolower(p)!='e' && tolower(p)!='n' && p))
return;
// Update commConfig with options and set stillConfig to TRUE
if (br > 0)
commConfig.dcb.BaudRate = br;
if (db > 0)
commConfig.dcb.ByteSize =db;
commConfig.dcb.fParity = TRUE;
if (p) commConfig.dcb.Parity = (tolower(p)=='n' ? NOPARITY :
(tolower(p)=='o' ? ODDPARITY : EVENPARITY));
stillConfig = TRUE;
}
//* --------------------------------------------------------------------------
//* Function Name : Main
//* Object : Main dialog box procedure
//* Output Parameters :
//* Functions called : GetDlgItemText
//* EndDialog
//* --------------------------------------------------------------------------
BOOL CALLBACK MainBox(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
// Messages
switch(message)
{
case WM_INITDIALOG:
// Set focus on this window
Hwnd = hwndDlg;
SetFocus(hwndDlg);
// Init edit control
SendDlgItemMessage(hwndDlg, IDC_EDIT1, WM_SETTEXT, 0,
(LPARAM)comPort);
SendDlgItemMessage(hwndDlg, IDC_EDIT1, EM_LIMITTEXT, 5,
0);
break;
// process commands
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_CFG:
configCommand();
break;
case ID_SEND:
sendCommand();
break;
case ID_QUIT:
EndDialog(hwndDlg,FALSE);
break;
}
break;
case WM_CLOSE:
EndDialog(hwndDlg,FALSE);
break;
default:
return FALSE;
}
return 0;
}
//* --------------------------------------------------------------------------
//* Function Name : WinMain
//* Object : main function
//* Functions called : RegisterClass
//* CreateWindow
//* UpdateWindow
//* ShowWindow
//* LoadLibrary
//* MessageBox
//* TranslateMessage
//* DispatchMessage
//* GetMessage
//* --------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdParam,int nCmdShow)
{
// initilialisation
hInst=hInstance;
commConfig.dwSize = 0;
// Process command line
processCmdLine(lpszCmdParam);
// Display dialog box
DialogBox(hInst,"IDD_DIALOG1",NULL,MainBox);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -