bcb與豐煒m系列plc通訊的問題 |
尚未結案
|
z858328y
一般會員 發表:7 回覆:9 積分:3 註冊:2006-08-29 發送簡訊給我 |
|
z858328y
一般會員 發表:7 回覆:9 積分:3 註冊:2006-08-29 發送簡訊給我 |
忘了附程式碼,請各位大大看看
[code cpp] void __fastcall TForm1::button_OpenPortClick(TObject *Sender) { DCB dcb; AnsiString strPort=""; strPort = ComboBox1->Text; hComm = CreateFile(strPort.c_str(),GENERIC_READ|GENERIC_WRITE,0, NULL,OPEN_EXISTING,0,0); if( hComm == INVALID_HANDLE_VALUE ) { MessageBox(0,"開啟錯誤!!","Error",MB_OK); return; } GetCommState(hComm,&dcb); dcb.fParity = TRUE; dcb.BaudRate = 19200; dcb.ByteSize = 7; dcb.Parity = EVENPARITY; dcb.StopBits = ONESTOPBIT; if( !SetCommState(hComm,&dcb) ) { MessageBox(0,"設定錯誤!!","Error",MB_OK); CloseHandle(hComm); return; } } int __fastcall TForm1::ResultStr(int nAck,BYTE temp[],BYTE result[]) { char cSum; int Sum[16]={0}; int nSum=0,nCount=0,nTotalByte=0; int nTen=0,nOne=0; AnsiString str=""; *(result 0) = GetFirstEndChar(nAck); *(result 1) = HexToChar(0); *(result 2) = HexToChar(0); while( temp[nCount] != '%' ) { *(result nCount 3) = temp[nCount]; nCount ; } *(result nCount 3) = GetFirstEndChar(3); nTotalByte = nCount 4; for(int i=1;i str.sprintf("0x%X",*(result i)); int a = str.ToInt(); nSum = a; } nCount = 0; str = IntToHex(nSum,2); AnsiString b; b = str.SubString(2,1); *(result nTotalByte) = StrToASCII(b); b = str.SubString(3,1); *(result nTotalByte 1) = StrToASCII(b); return nTotalByte 2; } void __fastcall TForm1::but_Y0Click(TObject *Sender) { AnsiString str; DWORD dwByte; BYTE tempresult[20]={0}; int nBitNum=0; shape_Y0->Brush->Color = clRed; BYTE temp[]={0x36,0x31,0x31,0x43,0x43,0x38,0x30,0x32,0x31,0x45,0x41,0x33,'%'}; shape_Y0->Brush->Color = clGreen; nBitNum = ResultStr(2,temp,tempresult); str.sprintf("%s",tempresult); bool bOK = WriteFile(hComm,str.c_str(),str.Length(),&dwByte,NULL); } [/code]
編輯記錄
yckuo 重新編輯於 2007-11-02 21:29:27, 註解 套用程式碼編輯‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |