📄 comusart.c
字号:
//*-----------------------------------------------------------------------------
//* ATMEL Microcontroller Software Support - ROUSSET -
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name : ComUsart.c
//* Object : PC Usart Driver
//*
//* 1.0 02/05/99 JPP : Creation
//*-----------------------------------------------------------------------------
//*----- Files to be included Definition -----*/
#include <windows.h>
#include <stdio.h>
#include "resource.h"
#include "global_def.h"
#include "ComUsart.h"
#include "send.h"
//*----- Types and Constants Definition -----*/
// COM port
//*----- External Prototy -----*/
extern ComLoader ComUsed;
extern COMMCONFIG commConfig; // COM user parameters (dwSize = 0 when none)
extern void displayError(void);
//*----------------------------------------------------------------------------
//* Function Name : writeCom
//* Object : Format print info
//* Input Parameters : Address of message, Len
//* Output Parameters : FALSE or TRUE
//* Functions called : CloseHandle
//*----------------------------------------------------------------------------
void closeCom( void)
{
if(!CloseHandle(ComUsed.hCom))
displayError();
ComUsed.hCom = INVALID_HANDLE_VALUE;
}
//*----------------------------------------------------------------------------
//* Function Name : writeCom
//* Object : Format print info
//* Input Parameters : Address of message, Len
//* Output Parameters : FALSE or TRUE
//* Functions called : ReadFile, displayError,closeCom
//*----------------------------------------------------------------------------
BOOL writeCom( char *buffer,DWORD len)
{
DWORD nbWritten;
// Tranfert file (return if error)
if (!WriteFile(ComUsed.hCom, buffer, len, &nbWritten, NULL))
{
displayError();
return FALSE;
}
if ( nbWritten !=len)
{
return FALSE;
}
return TRUE;
}
//*----------------------------------------------------------------------------
//* Function Name : readCom
//* Object : Format print info
//* Input Parameters : Address of message, Len
//* Output Parameters : ERROR_NO_READ, READ_OK or TIME_OUT_REPLY
//* Functions called : ReadFile, displayError, closeCom
//*----------------------------------------------------------------------------
BOOL readCom( char *buffer,DWORD len)
{
DWORD nbRead;
// Tranfert file (return if error)
if (!ReadFile(ComUsed.hCom, buffer, len, &nbRead, NULL))
{
displayError();
return ERROR_NO_READ;
}
if ( nbRead <len)
{
return TIME_OUT_REPLY;
}
return READ_OK;
}
//*----------------------------------------------------------------------------
//* Function Name : printComInfo
//* Object : Format print info
//* Input Parameters : char * addresse of message, Len
//* Output Parameters :
//* Functions called : sprintf
//*----------------------------------------------------------------------------
void printComInfo ( char *message,int * len)
//* Begin
{
if (ComUsed.hCom != INVALID_HANDLE_VALUE)
{
*len += sprintf(message+*len,"- %s: 8 bits 1 stop no parity\n",
ComUsed.name);
*len += sprintf(message+*len,"Current baud: %d\n",
ComUsed.baud[COM_BAUD_CURRENT]);
*len += sprintf(message+*len,"First baud: %d\n",
ComUsed.baud[COM_BAUD_INIT]);
*len += sprintf(message+*len,"High baud: %d with AT91 CD:%d\n",
ComUsed.baud[COM_BAUD_LOAD], ComUsed.cd);
}
else
{
*len += sprintf(message+*len,"Invalid com\n");
}
}
//* --------------------------------------------------------------------------
//* Function Name : openCom
//* Object : Open COM port
//* Input Parameters : COM Port name
//* Output Parameters : ComUsed.hCom
//* Functions called : Windows : CreateFile,GetCommConfig,SetCommConfig
//* --------------------------------------------------------------------------
HANDLE openCom(LPCTSTR name, DWORD BaudRate)
{
DWORD sizeCommConfig = sizeof(COMMCONFIG);
COMMTIMEOUTS CommTimeouts;
// open COM
if (ComUsed.hCom != INVALID_HANDLE_VALUE)
closeCom();
ComUsed.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 ((ComUsed.hCom != INVALID_HANDLE_VALUE) )//&& !commConfig.dwSize)
{
ComUsed.baud[COM_BAUD_CURRENT]=BaudRate;
// GeT commConfig properties to this COM
if (! GetCommConfig(ComUsed.hCom, &commConfig, &sizeCommConfig))
displayError();
// memset(&commConfig,0x00,sizeof(commConfig));
// set the specifc configuration
commConfig.dcb.BaudRate= BaudRate;
commConfig.dcb.fBinary= 1; // binary mode, no EOF check
commConfig.dcb.fParity= 0; // enable parity checking
commConfig.dcb.fOutxCtsFlow=0; // CTS output flow control
commConfig.dcb.fOutxDsrFlow=0; // DSR output flow control
commConfig.dcb.fDtrControl=0; // DTR flow control type
commConfig.dcb.fDsrSensitivity=0; // DSR sensitivity
commConfig.dcb.fTXContinueOnXoff=0; // XOFF continues Tx
commConfig.dcb.fErrorChar= 0; // enable error replacement
commConfig.dcb.fNull= 0; // enable null stripping
commConfig.dcb.fRtsControl=0; // RTS flow control
commConfig.dcb.ByteSize=8; // number of bits/byte, 4-8
commConfig.dcb.Parity=0; // 0-4=no,odd,even,mark,space
// Set commConfig properties to this COM
if (! SetCommConfig(ComUsed.hCom, &commConfig, sizeof(COMMCONFIG)))
displayError();
//* Set the Timeouts
CommTimeouts.ReadIntervalTimeout = 1000; //* Time between two characters
CommTimeouts.ReadTotalTimeoutMultiplier = 40; //* Multiplier
CommTimeouts.ReadTotalTimeoutConstant= 1000; //* Constant additting
CommTimeouts.WriteTotalTimeoutMultiplier= 0; //*Total time-out period for write
CommTimeouts.WriteTotalTimeoutConstant= 0;
if (! SetCommTimeouts(ComUsed.hCom,&CommTimeouts))
displayError();
if (! PurgeComm(ComUsed.hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR))
displayError();
}
return(ComUsed.hCom);
}
//* --------------------------------------------------------------------------
//* Function Name : ChooseBox
//* Object : Get aviable COM port
//* Input Parameters : Dialogue box IDD_DIALOG1 ,ComUsed
//* Output Parameters : None
//* Functions called : Windows : SendDlgItemMessage,EndDialog,
//* Application : displayError,openCom
//* --------------------------------------------------------------------------
BOOL CALLBACK ChooseBox(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
// Messages
switch(message)
{
case WM_INITDIALOG:
SendDlgItemMessage(hwndDlg, IDC_EDIT1, WM_SETTEXT, 0,(LPARAM)ComUsed.name);
SendDlgItemMessage(hwndDlg, IDC_EDIT1, EM_LIMITTEXT, 5,0);
break;
case WM_CLOSE:
EndDialog(hwndDlg,FALSE);
break;
case WM_COMMAND:
if (wParam==IDCANCEL)
EndDialog(hwndDlg,FALSE);
else if (wParam == IDOK)
{
SendDlgItemMessage(hwndDlg, IDC_EDIT1, WM_GETTEXT, 6,(LPARAM)ComUsed.name);
// Check if COM is Aviable
if (openCom(ComUsed.name,ComUsed.baud[0]) == INVALID_HANDLE_VALUE)
{
displayError();
}
else
{
EndDialog(hwndDlg,TRUE);
}
}
break;
default:
return FALSE;
}
return 0;
}
BOOL CALLBACK comHighSpped(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
// Messages
int len;
unsigned char text_message[256];
DWORD baud;
switch(message)
{
case WM_INITDIALOG:
len =0;
printComInfo ( &text_message[0],&len);
SendDlgItemMessage(hwndDlg, IDC_COMSPEED, WM_SETTEXT, 0,(LPARAM)text_message);
EnableWindow (GetDlgItem(hwndDlg, IDC_HIGH), 1);
EnableWindow (GetDlgItem(hwndDlg, IDC_LOW), 1);
if (
( ComUsed.hCom == INVALID_HANDLE_VALUE) ||
( ComUsed.baud[COM_BAUD_INIT]==ComUsed.baud[COM_BAUD_CURRENT])
)
EnableWindow (GetDlgItem(hwndDlg, IDC_LOW), 0);
if (
( ComUsed.hCom == INVALID_HANDLE_VALUE) ||
( ComUsed.baud[COM_BAUD_LOAD]==ComUsed.baud[COM_BAUD_CURRENT])
)
EnableWindow (GetDlgItem(hwndDlg, IDC_HIGH), 0);
break;
case WM_CLOSE:
EndDialog(hwndDlg,FALSE);
break;
case WM_COMMAND:
if (wParam == IDOK)
{
EndDialog(hwndDlg,TRUE);
break;
}
if (wParam==IDC_HIGH)
baud= ComUsed.baud[COM_BAUD_LOAD];
if (wParam==IDC_LOW)
baud= ComUsed.baud[COM_BAUD_INIT];
if (openCom(ComUsed.name,baud) == INVALID_HANDLE_VALUE)
displayError();
SendMessage(hwndDlg,WM_INITDIALOG,0,0);
break;
default:
return FALSE;
}
return 0;
}
//* --------------------------------------------------------------------------
//* Function Name : configCommand
//* Object : Process configuration command
//* Input Parameters : none
//* Output Parameters : TRUE if config done, FALE if "Cancel"
//* Functions called : Windows : SendDlgItemMessage
//* dialogu box : CommConfigDialog
//* Application :openCom
//* --------------------------------------------------------------------------
BOOL configCommand(HWND hwnd)
{
// Get COM Port name
SendDlgItemMessage(hwnd, IDC_EDIT1, WM_GETTEXT, 6,(LPARAM)ComUsed.name);
// open COM, to see if it exists (return if error)
if (openCom(ComUsed.name,ComUsed.baud[0]) == INVALID_HANDLE_VALUE)
{
displayError();
return FALSE;
}
// Display dialog box (return result)
if( ! CommConfigDialog(ComUsed.name, NULL, &commConfig))
{
displayError();
return FALSE;
}
return TRUE;
}
//* End of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -