
Эксперт
   
Профиль
Группа: Эксперт
Сообщений: 2304
Регистрация: 12.10.2003
Где: Поезд №21/22 ( ст . Прага )
Репутация: 4 Всего: 47
|
Вот, примерчик Там еще скипт есть, ноя думаю он не нужен  Можно и так понять, что к чему | Код | /*********************************************************************** Module Name: port.c
***********************************************************************/
#include <windows.h> #include <conio.h> #include <time.h> #include <stdio.h> #include "script.h" //#include "tty.h"
#define READ_TIMEOUT 500 #define STATUS_CHECK_TIMEOUT 500 // Milliseconds
HANDLE hPort,hLogFile; HANDLE hStatusThread,hTimerThread; DWORD WINAPI PortStatusThread (LPVOID lpvoid); DWORD WINAPI TimerThread (LPVOID lpvoid); cScriptInOut ScriptIO;
char alpszArg[5][10]; UINT_PTR iTimer; UINT uEl = 0; int iStep = 0; bool bStartTimer = false; char *pOut = NULL; int iLenOut;
//char pDataStr[MAX_LEN_LINE];
/***********************************************************************
PortInitialize (LPTSTR lpszPortName)
***********************************************************************/
BOOL PortInitialize (LPTSTR lpszPortName) { DWORD dwError, dwThreadID; DCB PortDCB; COMMTIMEOUTS CommTimeouts; char * pStopStr;
hPort = CreateFile (lpszPortName, // Pointer to the name of the port GENERIC_READ | GENERIC_WRITE, // Access (read/write) mode 0, // Share mode NULL, // Pointer to the security attribute OPEN_EXISTING,// How to open the serial port FILE_FLAG_OVERLAPPED, // Port attributes NULL); // Handle to port with attribute // to copy
if ( hPort == INVALID_HANDLE_VALUE ) { cprintf("Unable to open the port"); dwError = GetLastError (); return FALSE; }
PortDCB.DCBlength = sizeof (DCB);
GetCommState (hPort, &PortDCB);
PortDCB.BaudRate = strtol(alpszArg[3],&pStopStr,10);//9600; // Current baud PortDCB.fBinary = TRUE; // Binary mode; no EOF check PortDCB.fParity = TRUE; // Enable parity checking. PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx PortDCB.fOutX = FALSE; // No XON/XOFF out flow control PortDCB.fInX = FALSE; // No XON/XOFF in flow control PortDCB.fErrorChar = FALSE; // Disable error replacement. PortDCB.fNull = FALSE; // Disable null stripping. PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on // error. PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8 PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
if (!SetCommState (hPort, &PortDCB)) { cprintf("Unable to configure the serial port"); dwError = GetLastError (); return FALSE; }
GetCommTimeouts (hPort, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD; CommTimeouts.ReadTotalTimeoutMultiplier = 0; CommTimeouts.ReadTotalTimeoutConstant = 0; CommTimeouts.WriteTotalTimeoutMultiplier = 10; CommTimeouts.WriteTotalTimeoutConstant = 1000;
if (!SetCommTimeouts (hPort, &CommTimeouts)) { cprintf("Unable to set the time-out parameters"); dwError = GetLastError (); return FALSE; }
EscapeCommFunction (hPort, SETDTR); EscapeCommFunction (hPort, SETRTS);
if (hStatusThread = CreateThread (NULL, 0, PortStatusThread, 0, 0, &dwThreadID)) { CloseHandle (hStatusThread); } else { cprintf("Unable to create the read thread"); dwError = GetLastError (); return FALSE; }
return TRUE; }
/***********************************************************************
PortWrite (BYTE Byte)
***********************************************************************/ BOOL PortWrite (char *pByte,int len) { DWORD dwNumBytesWritten; char szOut[256]; char szbuf[10]; //Overlapped OVERLAPPED osWrite = {0}; DWORD dwWritten; DWORD dwRes; BOOL fRes;
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (osWrite.hEvent == NULL) return FALSE;
if (!WriteFile (hPort, // Port handle pByte, // Pointer to the data to write len, // Number of bytes to write &dwNumBytesWritten, // Pointer to the number of bytes // written &osWrite)) // Must be NULL for Windows CE { if (GetLastError() != ERROR_IO_PENDING) { fRes = FALSE; } else dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE); switch(dwRes) { case WAIT_OBJECT_0: if (!GetOverlappedResult(hPort, &osWrite, &dwWritten, FALSE)) fRes = FALSE; else fRes = TRUE; break; default: fRes = FALSE; break; } } else fRes = TRUE;
CloseHandle(osWrite.hEvent); if(fRes) { sprintf(szOut,"Out "); for(int i = 0;i<len;i++) { sprintf(szbuf,"%02X ",(unsigned char)pByte[i]); strcat(szOut,szbuf); if(strlen(szOut)>250) break; } strcat(szOut,"\r\n"); WriteFile(hLogFile,szOut,strlen(szOut),&dwWritten,NULL); cprintf("%s",szOut); } return fRes;
}
void ReportStatusEvent(DWORD dwCommModemStatus) { BYTE Byte; DWORD dwBytesTransferred,dwWr; char szIn[256]; char szbuf[10]; int iRecLineLen = 0; //FileRead DWORD dwRes; BOOL fWaitingOnRead = FALSE; OVERLAPPED osReader = {0};
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (osReader.hEvent == NULL) return ;
if (dwCommModemStatus & EV_RXCHAR) { byte fPrint = false; sprintf(szIn,"In "); do { if((!fWaitingOnRead)&&(!ReadFile (hPort, &Byte, 1, &dwBytesTransferred, &osReader))) { if (GetLastError() == ERROR_IO_PENDING) // read not delayed? fWaitingOnRead = TRUE; } if (fWaitingOnRead) { dwRes=WaitForSingleObject(osReader.hEvent, READ_TIMEOUT); if(dwRes== WAIT_OBJECT_0) { fWaitingOnRead = FALSE; } } if (dwBytesTransferred == 1) { fPrint = true; sprintf(szbuf,"%02X ",Byte); if(strlen(szIn)<250) strcat(szIn,szbuf); iRecLineLen = 0; if((pOut=ScriptIO.GetOutForIn(Byte,&iRecLineLen))!=NULL) { strcat(szIn," +++\r\n"); WriteFile(hLogFile,szIn,strlen(szIn),&dwWr,0); cprintf("%s",szIn); fPrint= false; sprintf(szIn,"In "); PortWrite(pOut,iRecLineLen); } } } while (dwBytesTransferred == 1); if(fPrint) { strcat(szIn,"\r\n"); WriteFile(hLogFile,szIn,strlen(szIn),&dwWr,0); cprintf("%s",szIn); } }
} /***********************************************************************
PortStatusThread (LPVOID lpvoid)
***********************************************************************/ DWORD WINAPI PortStatusThread (LPVOID lpvoid) { DWORD dwCommModemStatus;
//Overlapped //Status DWORD dwRes; DWORD dwOvRes; BOOL fWaitingOnStat = FALSE; OVERLAPPED osStatus = {0}; osStatus.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (osStatus.hEvent == NULL)return 0; SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
while (hPort != INVALID_HANDLE_VALUE) { if((!fWaitingOnStat)&&(!WaitCommEvent(hPort, &dwCommModemStatus, &osStatus))) { if(GetLastError() == ERROR_IO_PENDING) fWaitingOnStat = TRUE; else { ReportStatusEvent(dwCommModemStatus); } } if (fWaitingOnStat) { dwRes = WaitForSingleObject(osStatus.hEvent, STATUS_CHECK_TIMEOUT); if(dwRes==WAIT_OBJECT_0) { // Event occurred. if (GetOverlappedResult(hPort, &osStatus, &dwOvRes, FALSE)) ReportStatusEvent(dwCommModemStatus); fWaitingOnStat = FALSE; } } SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING); GetCommModemStatus (hPort, &dwCommModemStatus); }
return 0; }
/***********************************************************************
PortClose (HANDLE hCommPort)
***********************************************************************/ BOOL PortClose (HANDLE hCommPort) { DWORD dwError;
if (hCommPort != INVALID_HANDLE_VALUE) { if (!CloseHandle (hCommPort)) { dwError = GetLastError (); return FALSE; } else { hCommPort = INVALID_HANDLE_VALUE; return TRUE; } }
return FALSE; }
DWORD WINAPI TimerThread (LPVOID lpvoid) { while(1) { Sleep(*(UINT*)lpvoid); if(bStartTimer) { if(ScriptIO.GetOut(0,&pOut,&iLenOut)) { (*(WORD *)(pOut+iStep))++; PortWrite(pOut,iLenOut);
} } } }
void main(int argc, char *argv[ ], char *envp[ ]) { DWORD dwThreadID;
char bByte = 0; bool fSpKey = false; DWORD dwNumBytesWritten;
char * pStopStr;
if(argc<5) iStep = 4; else iStep = strtol(argv[6],&pStopStr,10);;
if(argc<6) uEl=1000; else uEl=strtol(argv[5],&pStopStr,10);
if(argc<5) strcpy(alpszArg[3],"9600"); else strcpy(alpszArg[3],argv[4]);
if(argc<4) strcpy(alpszArg[2],"log.txt"); else strcpy(alpszArg[2],argv[3]); if(argc<3) strcpy(alpszArg[1],"test1.txt"); else strcpy(alpszArg[1],argv[2]);
if(argc<2) strcpy(alpszArg[0],"COM1"); else strcpy(alpszArg[0],argv[1]);
hLogFile = CreateFile (alpszArg[2], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0,//FILE_ATTRIBUTE_NORMAL, NULL);
if ( hLogFile == INVALID_HANDLE_VALUE ) { cprintf("Unable to open log file"); return ; } SetFilePointer(hLogFile,0,NULL,FILE_END);
time_t ltime; time( <ime ); WriteFile(hLogFile,"\r",1,&dwNumBytesWritten,NULL); WriteFile(hLogFile,ctime( <ime ),strlen(ctime( <ime )),&dwNumBytesWritten,NULL); WriteFile(hLogFile,"\r",1,&dwNumBytesWritten,NULL);
ScriptIO.ScriptInit(alpszArg[1]);
if (hTimerThread = CreateThread (NULL, 0, TimerThread,&uEl , 0, &dwThreadID))
if(PortInitialize(alpszArg[0])) { bool bExit = true; while(bByte!=0x1b) { if(_kbhit()) { bByte=getch(); if(bByte == 0) fSpKey = true; else if(fSpKey) { int iFKey = bByte - 0x3B; if((iFKey>=0)&&(iFKey<9)) { if(ScriptIO.GetOut(iFKey,&pOut,&iLenOut)) PortWrite(pOut,iLenOut); } if(iFKey==9) { bStartTimer=!bStartTimer; } fSpKey = false; } else PortWrite (&bByte,1); } } } CloseHandle(hLogFile); }
|
--------------------
 — регистрация доменов от 150 руб.
|