תשובה.
הדבר הכי גרוע שאתה יכול לעשות זה להוסיף component. בעיקרון commport פועל עם פקודות די מקבילות לאלו שמתעסקות עם קבצים. לדוגמא עם אתה עושה CreateFile עם שם קובץ comm1 אתה בעצם פותח את ה commהזה. כמו כן אתה צריך לבצע קישור ל DCB מתאים (struct שמחזיק לך הגדרות כדוגמת setting וכו'). וכן קישור ל timeOuts מתאים. ואז הכתיבה והקריאה היא באמצעות ReadFile ו WriteFile המוכרות. חפש communication overview ב msdn או בקישור ש galh הביא. מאחר שיש לי מצב וח טוב:
/////////////////////////////communication properties//// #define SIZEOF_READWRITE 200 #define SENDRECIEVE_TIME_OUT 5000 ///////////////////////////////////////////////////////// class CBasecomm { protected: HANDLE hCom; //handle to comm char strCommString[20]; //the name of comm ("COM1" , "COM2" etc..) DCB dcb; //the structure for settings COMMTIMEOUTS Timeouts; //determine the interval for reading comm BOOL fSuccess; DWORD dwError; public: CBasecomm (int CommNumber); int OpenComm( DWORD nBaudRate , BYTE nByteSize ,BYTE nParity , BYTE nStopBits); int CBasecomm::CloseComm(); int ClearBuffers() ; int SendRecieveDataAsccii( const char * strCommOutput , char * strAnswer); }; #define READ_TIMEOUT 500 #include "StdAfx.h" #include "Comm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // the initial CTOR get the commnumber as parameter CBasecomm::CBasecomm(int CommNumber) { sprintf(strCommString,"\\\\.\\COM%d",CommNumber); }; int CBasecomm::OpenComm( DWORD nBaudRate , BYTE nByteSize ,BYTE nParity , BYTE nStopBits) { hCom = CreateFile(strCommString, GENERIC_READ | GENERIC_WRITE, 0, //shared , comm devices must be opened w/exclusive-access NULL, // no security attributes OPEN_EXISTING, // comm devices must use OPEN_EXISTING FILE_ATTRIBUTE_NORMAL, NULL // hTemplate must be NULL for comm devices ); if (hCom == INVALID_HANDLE_VALUE) { //the comm is probably busy or not work dwError = GetLastError(); AfxMessageBox("Comm Is Busy"); return(2); } fSuccess = GetCommState(hCom, &dcb); if (!fSuccess) { // Handle the error. } dcb.BaudRate = nBaudRate; dcb.ByteSize = nByteSize; dcb.Parity = nParity; dcb.StopBits = nStopBits; fSuccess = SetCommState(hCom, &dcb); if (!fSuccess) { // Handle the error. } fSuccess = GetCommTimeouts(hCom, &Timeouts); if (!fSuccess) { // Handle the error. } //change these values to wait more/less for time out Timeouts.ReadIntervalTimeout = SENDRECIEVE_TIME_OUT; //if there is no interval of sending the read is finish Timeouts.ReadTotalTimeoutMultiplier = 0; //we will close the pointer Timeouts.ReadTotalTimeoutConstant = SENDRECIEVE_TIME_OUT; //only if the times between interval Timeouts.WriteTotalTimeoutMultiplier = 0; //are not correct Timeouts.WriteTotalTimeoutConstant = 0; fSuccess = SetCommTimeouts(hCom, &Timeouts); if (!fSuccess) { // Handle the error. } SetupComm(hCom, 1024, 1024); // the buffers for read and write return 0L; }; int CBasecomm::ClearBuffers() { PurgeComm(hCom, PURGE_TXABORT|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_RXCLEAR); return 0L; }; int CBasecomm::CloseComm() { CloseHandle(hCom); hCom = INVALID_HANDLE_VALUE; return 0L; }; int CBasecomm::SendRecieveDataAsccii(const char * strCommOutput , char * strAnswer) { char TotalAnswerchar[SIZEOF_READWRITE]; char bufout[SIZEOF_READWRITE]; DWORD countout ,countin = SIZEOF_READWRITE; DWORD realCountout; DWORD dwRead; char chRead; CString ASK = strCommOutput; int ichar = -1; strncpy(bufout,strCommOutput,SIZEOF_READWRITE); countout = strlen(bufout); // flush the comm and send the string ClearBuffers(); WriteFile(hCom ,bufout, countout, &realCountout, NULL); do { if (ReadFile(hCom, &chRead, 1, &dwRead, NULL)) { if (dwRead == 1) &nb