serial comport

davoren

New member
serial comport

שלום לכם, איך אני יכול לשלוח \ לקבל בתים דרך RS232? מנסה לכתוב משהו פשוט ב visual C לתקשורת דרך ה - COM PORT תודה, אורן
 

yair24

Member
אתה מוסיף COMPONENT

של MSCOMM ומשתמש בAPI שלו. אם אתה צריך עוד עזרה בנושא , קוד לדוגמא או כל דבר אחר. אתה מוזמן לבקש. יאיר
 

ברנדל

New member
תשובה.

הדבר הכי גרוע שאתה יכול לעשות זה להוסיף 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​
 

ברנדל

New member
ושוב הפונקציה האחרונה

שלא נכנסה טוב
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) { ++ichar; TotalAnswerchar[ichar] = chRead; } } else break; } while (dwRead); ++ichar; TotalAnswerchar[ichar] = '\0'; strcpy(strAnswer,TotalAnswerchar); return 0L; };​
אגב כתבתי את זה לבד (זה לא מועתק) וזה היה בתחילת דרכי. אז אם אתה או אחרים מוצאים אי אלו טעויות של כתיבה מלוכלכת אז סליחה בכל אופן העסק עובד פיצוץ ואני משתמש בו עד היום. בהצלחה.
 

davoren

New member
וואו, עוד לא הספקתי לשתות קפה וכבר

יש כאלה תשובות מפורטות! תודה רבה ברנדל, יאיר וגל ה. כמובן שיקח לי זמן לעקל את החומר החדש -> אני עוד מהדור של הדוס...בלי מילים מפוצצות כמו API וכו' אני מקווה להסתדר עם זה אורן
 

yair24

Member
קיבינימאט

באמת הסתבכתי כשעשיתי את הפרויייקט עם הCOMPONENT וכל פעם שאני מעביר את הפרוייקט אני צריך להעביר גם את הקומוננט הזה... ולרשום אותו עם REGSVR32 איזה בעסה, אם הייית יודע על האופצייה הזאת כשכתבתי את האפליקציה שלי אז.... יאיר
 

galh

New member
הי, למיטב זכרוני...

אם תחפש בארכיון הפורום את השאלה שלך בנושא תגלה שגם אז נתתי לך קישור לרשימת ה- API-ים הזו. אתה זה שהעדפת את MSCOMM.
 

yair24

Member
כן הא?

אני חושב שאני יודע למה... רציתי לדעת איך עובדים עם הפקד הזה.... ולדעת איך עובדים עם פקדים באופן כללי... אני זוכר שבאיזשהו שלב כשהוספתי את הקומפוננט הזה היו לי מלא שגיאות קומפילציה ובאיזשהו שלב הייתי בטוח שאני יכול לקחת את כל הפרוייקט ולזרוק אותו לפח ועבדתי עליו מלא זמן זה היה איזה טופס שיש בו לפחות 100 תיבות טקסט ואיזה 10 כפתורים ותפריט וכל מה שעשיתי נהרס לי בגלל שהוספתי את הCOMPONNENT אבל בסוף מזל הכל הסתדר.... אני כבר לא זוכר איך... יאיר
 
למעלה