BCB 使用MSComm 抓取RS232資料 (從單晶片讀入電腦) |
尚未結案
|
CloudChung
一般會員 發表:1 回覆:0 積分:0 註冊:2011-01-12 發送簡訊給我 |
之前是使用VB去撰寫的程式,現在要改用BCB去撰寫,一開始的時候是用CommDCB 還有 Creat File去開啟RS232的連線,
後來趴文有發現BCB有支援MSComm,想說VB的程式是使用MSComm寫的,轉到BCB應該也不會差太多, 但是試了好幾天還是Try不出來...,請這邊的諸位先進能否幫小弟這個忙.... 這是VB的程式碼: ----------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------- Private Sub Command1_Click() 'Request data from PIC and display the results Dim i As Integer, countValue As Long Dim t, tt, QQ, ww, ss, time As Single Dim timea As Double -------------------------------------------------------------------------------------------------------------------------------------------- Private Sub Form_Load() MSComm1.CommPort = 1 'intput comm1 MSComm1.Settings = "9600,N,8,1" MSComm1.PortOpen = True MSComm1.InputMode = comInputModeBinary 'input類型Binary MSComm1.RTSEnable = False 'Set PIC to normal mode lblAcc1 = "" 'time = 0 'Open "data.txt" For Output As #1 a = 1 End Sub --------------------------------------------------------------------------------------------------------------------------------------------- ' lbltest = MSComm1.InBufferCount Do While ss = 0 If MSComm1.InBufferCount > 0 Then '大於0表示有值進來 inByte = MSComm1.Input For i = 0 To UBound(inByte) inByte(i) = 0 Next i End If Do While MSComm1.InBufferCount < 2 '等待 2 Data DoEvents Loop inByte = MSComm1.Input countValue = inByte(0) countValue = countValue * 256 inByte(1) t = countValue * 0.09765625 Label6.Caption = Format(t, "0.00") 'lblAcc1.Caption = Format(t, "0.0000") 'lblAcc1.Caption = t * 0.09765625 '0.09765625(5/1024*20) 'time = time 0.1 'Print #1, Format(Time, "0.0"), lblAcc1.Caption '(秒數) (抓取值) Do While ss = 0 Command1.Enabled = False If MSComm1.InBufferCount > 0 Then '大於0表示有值進來 inByte = MSComm1.Input For i = 0 To UBound(inByte) inByte(i) = 0 Next i End If Do While MSComm1.InBufferCount < 2 '等待 2 Data DoEvents Loop inByte = MSComm1.Input countValue = inByte(0) countValue = countValue * 256 inByte(1) tt = countValue * 0.09765625 ww = tt - t Label4.Caption = Format(ww, "0.0000") delay Loop Loop Command1.Enabled = True End Sub -------------------------------------------------------------------------------------------------------------------------------------------------------------- 而這是小弟我用BCB寫的程式碼: --------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "MSCommLib_OCX" #pragma resource "*.dfm" TForm1 *Form1; short i; int countValue; int t,tt,QQ,ww,ss,tim; double timea; byte inByte[2]; int readData1,readData2; //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { MSComm1->CommPort = 1; MSComm1->Settings = "9600,N,8,1"; MSComm1->PortOpen = true; MSComm1->InputMode = comInputModeBinary; MSComm1->RTSEnable = false; } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer2Timer(TObject *Sender) { if (MSComm1->InBufferCount ) { inByte[0] = MSComm1->Input.VOleStr; } while (MSComm1->InBufferCount < 2) { inByte[1] = MSComm1->Input.ArrayHighBound(); Label2->Caption = MSComm1->Input.ArrayHighBound() ; } readData1 = inByte[0]; readData2 = inByte[1]; Label1->Caption = readData1; Label2->Caption = readData2; } //--------------------------------------------------------------------------- 從單晶片傳來的資料為一10bit的資料 所以VB的程式碼才寫成 inbyte(0) inbyte(1)這二筆 把這二筆數據合起來後才是一筆我們要的實際數據, 現在BCB能抓的到數據,但是與VB的數據MACH不起來.... 不知道哪邊的使用上出錯了..... 請各位高手幫幫忙.... 謝謝~ |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |