[心得] 因為在這裡找到好多資料希望對想了解RS …

看板C_and_CPP作者 (小兵)時間15年前 (2010/10/11 01:03), 編輯推噓9(902)
留言11則, 9人參與, 最新討論串1/1
分享一下 有錯請大力的噓下去謝謝你 /* This is a test program for receiving data from other computer. RS-232 Test: RS-232 male ------------- \ 1 2 3 4 5 / \ 6 7 8 9 / --------- To send and to receive data on your computer when you connect pin 2 and pin 3 togeter. To connect your RS-232 like this ----------------------- \ --- --- / \ 1 |2|--|3| 4 5 / \ --- --- / \ 6 7 8 9 / --------------- Albert Liu 10,10,2010 */ #include <windows.h> #include <stdio.h> #include <conio.h> //for _getch() and _kbhit(); #include <string> #include <cmath> using namespace std; int main(){ //To declare a handle for your comport HANDLE hComm; //To declare an object for setting the Comport DCB dcb; //To declare a string for saving temp string. string Temp; //To declare some variables for receiving massage char inbuff[1024]; DWORD nBytesRead,dwError; COMSTAT cs; //end// for receiving //To declare some variables for keybord control int ky,end=FALSE; //end// for keybord control //To declare a delay timer. int T_dela=0; //To declare some variables for sending massage const char *SendData; unsigned long lrc, BS; char str1[]="I am a test string."; printf("%s\n",str1); //end// for sending //To open COM3 for this program hComm = CreateFile("COM3", //usually "COM1" or "COM2" GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0); //It will turn off the program //when it can't find any comport to connect if (hComm == INVALID_HANDLE_VALUE){ printf("wrong port connection.\n"); return 0; } //To get COM3 state GetCommState(hComm,&dcb); //To set COM3 parameters dcb.BaudRate = CBR_9600; dcb.fOutxCtsFlow = FALSE; dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fOutxDsrFlow = FALSE; dcb.fOutX = FALSE; dcb.fInX = FALSE; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; //end// COM3 parameters //To set the parameters to COM3 now if (!SetCommState(hComm, &dcb)){ printf("setting error.\n"); return 0; } //Clearn COM3 data PurgeComm(hComm, PURGE_RXCLEAR); //To give Temp a string. //you can set input here Temp=str1; SendData = Temp.c_str(); BS=strlen(SendData); //sending data now WriteFile(hComm, SendData,BS,&lrc,NULL); //To count how many time have to delay for sending data. T_dela=(int)ceil((double)ceil((double)(BS*8)/6)*8/(dcb.BaudRate*0.001)); // 1 char = 8 bit so BS*8 Sleep(T_dela); //to find the error or receiving COM3's state. ClearCommError(hComm,&dwError,&cs); if (cs.cbInQue > sizeof(inbuff)){ PurgeComm(hComm, PURGE_RXCLEAR); printf("your massage is too big\n"); return 0; } //To read the data from COM3 when you push "Esc". while (!end){ //when you hit the key bord if (_kbhit()){ //to get the button you hit //'\x1B' = "Esc" if((ky = _getch())=='\x1B'){ //to read the data from COM3 now ReadFile(hComm, inbuff, cs.cbInQue, &nBytesRead, NULL); //break; end = TRUE; } } } //To add a char '\0' to the end of the string. inbuff[cs.cbInQue]='\0'; printf("I get this massage = %s \n",inbuff); return 0; } 我測試好多次了如果不是貼上來被換了字應該可以動 希望對你有幫助 按"Esc"之後會秀出 I get this massage = I am a test string. 記得把你的RS-232要接腳 2(RXD 接收資料)跟3(TXD傳送資料) 接起來喔(用電線接起來>.<) 確認以上(硬體軟體)都對了以後如果編譯還是不能過 MS Win2003 R2 PSDK 去 google 搜索這個安裝一下 參考資料: C++ Builder與RS-232 串列通訊控制 文魁資訊 串列資料傳送技術入門 建宏出版社 Window 下之馬達控制入門 全華科技圖書股份有限公司 PTT.cc C/C++ 板 PTT的大大 clarkman http://msdn.microsoft.com/en-us/library/ms810467.aspx -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.135.96.181 ※ 編輯: sgvacr 來自: 140.135.96.181 (10/11 01:04) ※ 編輯: sgvacr 來自: 140.135.96.181 (10/11 01:11) ※ 編輯: sgvacr 來自: 140.135.96.181 (10/11 01:17)

10/11 01:22, , 1F
推兵哥用心。
10/11 01:22, 1F

10/11 01:22, , 2F
這軟體好像可以模擬? http://com0com.sourceforge.net/
10/11 01:22, 2F

10/11 01:24, , 3F
也推~ 因為平常都是用C#來寫, 換成WIN32反而看不懂
10/11 01:24, 3F

10/11 01:24, , 4F
接電線那一步,對我來說很恐怖
10/11 01:24, 4F

10/11 01:25, , 5F
沒那麼恐怖啦 XD
10/11 01:25, 5F

10/11 02:22, , 6F
可以模擬 我之前開發都是用那個(com0com)
10/11 02:22, 6F

10/11 02:45, , 7F
推p大 自從燒壞一台4個port的示波器後 我就決定走資訊類了
10/11 02:45, 7F

10/11 08:33, , 8F
推兵哥用心
10/11 08:33, 8F

10/11 08:45, , 9F
收錄至 z-5-3-13
10/11 08:45, 9F

10/11 10:57, , 10F
推一下:)
10/11 10:57, 10F

10/11 13:03, , 11F
推兵哥,也感謝2樓p大說的com0com模擬軟體~試了一下~好用耶
10/11 13:03, 11F
文章代碼(AID): #1CiV7V5_ (C_and_CPP)