線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1109
推到 Plurk!
推到 Facebook!

RS-232

 
eileenkuo
一般會員


發表:1
回覆:0
積分:0
註冊:2003-11-18

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-12-09 13:57:45 IP:61.222.xxx.xxx 未訂閱
之前一直都是用RS-232與C++ BUILDER那本書附的元件寫程式 但是發現有使用上的障礙  所以把自已寫的程式放上去 供大家參考 //---------------------------------------------------------------------------    #include  #pragma hdrstop #include #include #include #include #include #include "rs232_2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "Comm" #pragma resource "*.dfm" TForm1 *Form1; /* open and read file */ char *data_buf; int data_length; /* com port */ HANDLE hComm; DCB dcb; COMSTAT cs; COMMTIMEOUTS CommTimeOuts; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { char *ComNo; String Temp; if(hComm==INVALID_HANDLE_VALUE) { ShowMessage("port had been open"); } else { Temp="COM" IntToStr(port->ItemIndex 1); ComNo=Temp.c_str(); hComm=CreateFile(ComNo,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0); //hComm=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,0); if(hComm==INVALID_HANDLE_VALUE) { MessageBox(0,"Error","Comm error",MB_OK); return; } else { #ifdef DEBUG MessageBox(0, "Comm Open OK!", "Status", MB_OK); #endif } SetCommMask(hComm, EV_RXCHAR); PurgeComm(hComm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF; CommTimeOuts.ReadTotalTimeoutMultiplier = 0; CommTimeOuts.ReadTotalTimeoutConstant = 1000; CommTimeOuts.WriteTotalTimeoutMultiplier = 0; CommTimeOuts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(hComm, &CommTimeOuts); GetCommState(hComm,&dcb); dcb.BaudRate=CBR_9600; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=ONESTOPBIT; // other various settings dcb.fBinary = TRUE; dcb.fParity = TRUE; if(SetCommState(hComm, &dcb)) { #ifdef DEBUG MessageBox(0, "Comm DCB Set OK!", "Status", MB_OK); #endif } else { MessageBox(0, "Comm DCB Set Fail!", "Error", MB_OK); } } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { CloseHandle(hComm); Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { int fd; struct stat stat_buf; if(Form1->OpenDialog1->Execute()) { //ShowMessage(Form1->OpenDialog1->FileName); //開檔 fd=open(Form1->OpenDialog1->FileName.c_str(), O_RDONLY); if(fd != -1) { //ShowMessage("open ok!"); stat(Form1->OpenDialog1->FileName.c_str(), &stat_buf); data_buf=(char *)malloc(stat_buf.st_size 1); memset(data_buf, 0, stat_buf.st_size 1); data_length=read(fd, data_buf, stat_buf.st_size); close(fd); //ShowMessage(data_buf); mSend->Lines->Add(AnsiString(data_buf)); } else { if(errno==EACCES) { ShowMessage("Permission denied!"); } else if(errno==EINVACC){ ShowMessage("Invalid access code"); } else if(errno==EMFILE) { ShowMessage("Too many open files"); } else if(errno==ENOENT) { ShowMessage("No such file or directory"); } else { ShowMessage("open ng!"); } } } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button4Click(TObject *Sender) { int i=0; /* for writefile */ BOOL fWriteStat; DWORD dwBytesWritten; DWORD dwBytesSent=0; DWORD dwBytesToWrite; DWORD dwLength; DWORD dwBytesRead=0; DWORD dwBytesAlreadyRead=0; DWORD dwErrorFlags; char read_buf[32*1024]; char bufc[6],start[6],dscp_packet[6],end[6]; dwBytesToWrite=6; /* start command */ start[0]=0x12; start[1]=0x13; start[2]=0x12; start[3]=0x12; start[4]=0x12; start[5]=start[0]^start[1]^start[2]^start[3]^start[4]; fWriteStat = WriteFile(hComm, start, dwBytesToWrite, &dwBytesWritten, NULL) ; ClearCommError(hComm, &dwErrorFlags, &cs); dwLength=cs.cbInQue; ReadFile(hComm, bufc, dwBytesToWrite, &dwBytesRead, NULL); if((start[0]==bufc[0])&& (start[1]==bufc[1])&& (start[2]==bufc[2])&& (start[3]==bufc[3])&& (start[4]==bufc[4])&& (start[5]==bufc[5])){ ShowMessage(" start commend ok!"); dscp_packet[0]=0x12; dscp_packet[1]=0x13; dscp_packet[2]=0; dscp_packet[3]=0; dscp_packet[4]=0; while((dwBytesSent 3) < data_length) { dscp_packet[2]=data_buf[dwBytesSent]; dwBytesSent ; dscp_packet[3]=data_buf[dwBytesSent]; dwBytesSent ; dscp_packet[4]=data_buf[dwBytesSent]; dwBytesSent ; #ifdef DEBUG dscp_packet[5]='Z'; #else dscp_packet[5]=dscp_packet[0]^dscp_packet[1]^dscp_packet[2]^dscp_packet[3]^dscp_packet[4]; #endif fWriteStat = WriteFile(hComm, dscp_packet, dwBytesToWrite,&dwBytesWritten, NULL) ; ClearCommError(hComm, &dwErrorFlags, &cs); dwLength=cs.cbInQue; ReadFile(hComm, read_buf, 6, &dwBytesRead, NULL); if((dscp_packet[0]!=read_buf[0])&& (dscp_packet[1]!=read_buf[1])&& (dscp_packet[2]!=read_buf[2])&& (dscp_packet[3]!=read_buf[3])&& (dscp_packet[4]!=read_buf[4])&& (dscp_packet[5]!=read_buf[5])){ i=0; while(i<10){ ShowMessage("須要重傳"); fWriteStat = WriteFile(hComm, dscp_packet, dwBytesToWrite, &dwBytesWritten, NULL) ; ClearCommError(hComm, &dwErrorFlags, &cs); dwLength=cs.cbInQue; ReadFile(hComm, read_buf, 6, &dwBytesRead, NULL); if((dscp_packet[0]==read_buf[0])&& (dscp_packet[1]==read_buf[1])&& (dscp_packet[2]==read_buf[2])&& (dscp_packet[3]==read_buf[3])&& (dscp_packet[4]==read_buf[4])&& (dscp_packet[5]==read_buf[5])){ break; } else { i=i 1; } }//while(i<10) re-try if(i>=10) { ShowMessage("System Error!"); return; } } else {//if (data ok) Memo1->Text=Memo1->Text read_buf; } }// while((dwBytesSent 3) < data_length) //-------------------------------------------------------------------------------// if((data_length)-(dwBytesSent)!= 0) { if((data_length-dwBytesSent)==2) { ShowMessage("還有2筆資料!"); dscp_packet[2]=data_buf[dwBytesSent]; dwBytesSent ; dscp_packet[3]=data_buf[dwBytesSent]; dwBytesSent ; dscp_packet[4]=0; #ifdef DEBUG dscp_packet[5]='Z'; #else dscp_packet[5]=dscp_packet[0]^dscp_packet[1]^dscp_packet[2]^dscp_packet[3]^dscp_packet[4]; #endif fWriteStat = WriteFile(hComm, dscp_packet, dwBytesToWrite,&dwBytesWritten, NULL); ClearCommError(hComm, &dwErrorFlags, &cs); dwLength=cs.cbInQue; ReadFile(hComm, read_buf,6, &dwBytesRead, NULL); if((dscp_packet[0]!=read_buf[0])&& (dscp_packet[1]!=read_buf[1])&& (dscp_packet[2]!=read_buf[2])&& (dscp_packet[3]!=read_buf[3])&& (dscp_packet[4]!=read_buf[4])&& (dscp_packet[5]!=read_buf[5])){ i=0; while(i<10) { fWriteStat = WriteFile(hComm, dscp_packet, dwBytesToWrite, &dwBytesWritten, NULL) ; ClearCommError(hComm, &dwErrorFlags, &cs); dwLength=cs.cbInQue; ReadFile(hComm, read_buf, 6, &dwBytesRead, NULL); Memo1->Text=Memo1->Text read_buf; if((dscp_packet[0]==read_buf[0])&& (dscp_packet[1]==read_buf[1])&& (dscp_packet[2]==read_buf[2])&& (dscp_packet[3]==read_buf[3])&& (dscp_packet[4]==read_buf[4])&& (dscp_packet[5]==read_buf[5])){ break; } else { i=i 1; } }//while(i<10) re-try if(i>=10) { ShowMessage("System Error!"); return; } } else { Memo1->Text=Memo1->Text read_buf; } } else if((data_length-dwBytesSent)==1) { ShowMessage("還有1筆資料!"); dscp_packet[2]=data_buf[dwBytesSent]; dwBytesSent ; dscp_packet[3]=0; dscp_packet[4]=0; #ifdef DEBUG dscp_packet[5]='Z'; #else dscp_packet[5]=dscp_packet[0]^dscp_packet[1]^dscp_packet[2]^dscp_packet[3]^dscp_packet[4]; #endif fWriteStat = WriteFile(hComm, dscp_packet, dwBytesToWrite,&dwBytesWritten, NULL); ClearCommError(hComm, &dwErrorFlags, &cs); dwLength=cs.cbInQue; ReadFile(hComm, read_buf,6, &dwBytesRead, NULL); if((dscp_packet[0]!=read_buf[0])&& (dscp_packet[1]!=read_buf[1])&& (dscp_packet[2]!=read_buf[2])&& (dscp_packet[3]!=read_buf[3])&& (dscp_packet[4]!=read_buf[4])&& (dscp_packet[5]!=read_buf[5])){ i=0; while(i<10){ fWriteStat = WriteFile(hComm, dscp_packet, dwBytesToWrite, &dwBytesWritten, NULL) ; ClearCommError(hComm, &dwErrorFlags, &cs); dwLength=cs.cbInQue; ReadFile(hComm, read_buf, 6, &dwBytesRead, NULL); Memo1->Text=Memo1->Text read_buf; if((dscp_packet[0]==read_buf[0])&& (dscp_packet[1]==read_buf[1])&& (dscp_packet[2]==read_buf[2])&& (dscp_packet[3]==read_buf[3])&& (dscp_packet[4]==read_buf[4])&& (dscp_packet[5]==read_buf[5])){ break; } else { i=i 1; } } //while(i<10) re-try if(i>=10) { ShowMessage("System Error!"); return; } } else { Memo1->Text=Memo1->Text read_buf; } } else { ShowMessage("程式瘋了!"); } } end[0]=0x12; end[1]=0x13; end[2]=0x14; end[3]=0x14; end[4]=0x14; end[5]=end[0]^end[1]^end[2]^end[3]^end[4]; fWriteStat = WriteFile(hComm, end, dwBytesToWrite, &dwBytesWritten, NULL) ; ClearCommError(hComm, &dwErrorFlags, &cs); dwLength=cs.cbInQue; ReadFile(hComm, read_buf, 6, &dwBytesRead, NULL); if((end[0]==read_buf[0])&& (end[1]==read_buf[1])&& (end[2]==read_buf[2])&& (end[3]==read_buf[3])&& (end[4]==read_buf[4])&& (end[5]==read_buf[5])){ ShowMessage("Updata completely!"); } } else{ //if (start command ok) ShowMessage("System is not ready! Try again later! "); Memo1->Text=Memo1->Text bufc; return; } } //--------------------------------------------------------------------------- 程式碼直接就可以跑 有
系統時間:2024-05-08 13:01:27
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!