廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 43757 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
阿強 手機 會員卡
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎 優秀管理員勳章
頭銜:小人物...小人物...
版主
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x0
[C/C++][精華] C++Builder 如何接收RS232資料(已解決)
小弟想要寫一個程式
可以接收RS232所傳輸到電腦的資料
並且將資料 ..

訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容



有些東西不能失去 為了保護它們
眼前這些渺小的東西 無論多少都能丟棄
但是我並不知道這樣最終會失去一切
獻花 x0 回到頂端 [樓 主] From:臺灣 | Posted:2007-01-18 15:56 |
緣道山人 會員卡
個人頭像
個人文章 個人相簿 個人日記 個人地圖 個人商品
頭銜:夷希微之徒~DDM 繼承人夷希微之徒~DDM 繼承人
驗證會員
級別: 榮譽會員 該用戶目前不上站
推文 x3 鮮花 x204
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

數位資料要畫成波形呀 表情
除非您是要將 RS-232 接收到的資料以 byte 為最小單位化成值(0~255)畫成波形 表情

以下是我八九年前在網路上找到的參考資料,原封不動附上給您參考:
我有寫成一個範例放在附件裡,請下載開啟參考,當初我是用數據機做實驗的。
如果覺得底下排版很亂,請看這裡:http://ddm.ncc.to/bcbcomm.htm

--------------------------------------------------------------------------------
Serial Communication with Borland C++ Builder

--------------------------------------------------------------------------------
Introduction...

I wish this site had been around when I was trying to figure out how to make serial communications work in Windows95. I, like many programmers, was hit with the double-whammy of having to learn Windows programming and Win95 serial comm programming at the same time. I found both tasks confusing at best. It was particularly frustrating because I had, over the years, written so much stuff (including lots of serial comm software) for the DOS environment and numerous embedded applications. Interrupt driven serial comm, DMA transfer serial comm, TSR serial comm, C, assembler, various processors... you name it, it had written it. Yet, everything I knew seemed upside-down in the message-driven-callback world of Windows.

After spending lots of money on books and seemingly endless effort, I have finally gotten enough of a handle on Win95 and serial comm programming to write something usable in this environment. Borland's C++ Builder has done a lot to help make Win95 programming easier and, once you know the tricks, the serial communications stuff is pretty easy, too.

The purpose of this site is to spare you hardship of my early efforts and get you up and running with your Win9x/NT serial comm programming as quickly as possible. If you're already familiar with using BCB to develop Windows programs, the example code should be plenty to get you going. You can also download the source code in BCBComm.zip. Good luck.



--------------------------------------------------------------------------------

The Example...

In the example that follows we're going to write a bare-bones program to do serial communication. It will consist of a Form with a Memo object (for text I/O) and a Thread object that handles incoming serial data. There are no menus or other features to distract us from focusing on the serial comm aspect of the program. Obviously, you'll want to add these and other elements to a fully functioning program.

Fire up BCB and start a New Project. Place a Memo object on Form1. Using the Object Inspector, set Memo1 properties as follows:


    [*]Alignment = alClient
    [*]MaxLength = 0
    [*]ScrollBars = ssVertical
    [*]WantReturns = true
    [*]WantTabs = false
    [*]WordWrap = true


Next, under the File | New menu, add a Thread Object. Use TRead for the class name when asked.

You should now have two Unit files: Unit1.cpp for Form1 activity and Unit2.cpp for the thread.

Using the Object Inspector again, create event handlers for the following events. The easiest way to create events handlers is as follows:


Go to the event tab sheet in Object Inspector.
Find the event of interest.
Double-click the blank space next to the event name.
If you follow this scheme, Object Inspector will create and automatically name the event handlers to the same name used in our examples. OK, here are the objects and the events we need to handle:

    [*]Form1   OnCreate
    [*]Form1   OnClose
    [*]Memo1 OnKeyPress

The framework for Unit1.cpp is now in place. Using the following listing as a guide, fill in Unit1.cpp with the following code. Be sure to note the #includes and global variables. If the framework for event handlers is missing in your program, DO NOT put it there by typing in the framework code! Go back and figure out what you missed. BCB MUST CREATE THE FRAMEWORK FOR YOU.

The Main Form...

//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "Unit1.h"

// YOU MUST INCLUDE THE HEADER FOR UNIT2 (THE THREAD UNIT)

#include "Unit2.h"

// GLOBAL VARIABLES

HANDLE hComm = NULL;
TRead *ReadThread;
COMMTIMEOUTS ctmoNew = {0}, ctmoOld;

//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
DCB dcbCommPort;

// OPEN THE COMM PORT.
// REPLACE "COM2" WITH A STRING OR "COM1", "COM3", ETC. TO OPEN
// ANOTHER PORT.

hComm = CreateFile("COM2",
              GENERIC_READ | GENERIC_WRITE,
              0,
              0,
              OPEN_EXISTING,
              0,
              0);

// IF THE PORT CANNOT BE OPENED, BAIL OUT.

if(hComm == INVALID_HANDLE_VALUE) Application->Terminate();

// SET THE COMM TIMEOUTS IN OUR EXAMPLE.

GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 100;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);

// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
// IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
// THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
// ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.

dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
BuildCommDCB("9600,N,8,1", &dcbCommPort);
SetCommState(hComm, &dcbCommPort);

// ACTIVATE THE THREAD. THE FALSE ARGUMENT SIMPLY MEANS IT HITS THE
// GROUND RUNNING RATHER THAN SUSPENDED.

ReadThread = new TRead(false);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
// TERMINATE THE THREAD.

ReadThread->Terminate();

// WAIT FOR THREAD TO TERMINATE,
// PURGE THE INTERNAL COMM BUFFER,
// RESTORE THE PREVIOUS TIMEOUT SETTINGS,
// AND CLOSE THE COMM PORT.

Sleep(250);
PurgeComm(hComm, PURGE_RXABORT);
SetCommTimeouts(hComm, &ctmoOld);
CloseHandle(hComm);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key)
{
// TRANSMITS ANYTHING TYPED INTO THE MEMO AREA.

TransmitCommChar(hComm, Key);

// THIS PREVENTS TYPED TEXT FROM DISPLAYING GARBAGE ON THE SCREEN.
// IF YOU ARE CONNECTED TO A DEVICE THAT ECHOES CHARACTERS, SET
// Key = 0 WITHOUT THE OTHER STUFF.

if(Key != 13 && (Key < ' ' || Key > 'z')) Key = 0;
}
//---------------------------------------------------------------------------

Now we turn our attention to the thread code in Unit2.cpp. The framework should already be in place. Use this listing as a guide and fill in Unit2.cpp with the following code.

The Thread...

//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

// YOU MUST INCLUDE THE HEADER FOR UNIT1

#include "Unit1.h"
#include "Unit2.h"

extern HANDLE hComm;
char InBuff[100];

//---------------------------------------------------------------------------
//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//     Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//     void __fastcall TRead::UpdateCaption()
//     {
//     Form1->Caption = "Updated in a thread";
//     }
//---------------------------------------------------------------------------
__fastcall TRead::TRead(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------

void __fastcall TRead::DisplayIt()
{

// NOTE THAT IN THIS EXAMPLE, THERE IS NO EFFORT TO MONITOR
// HOW MUCH TEXT HAS GONE INTO Memo1. IT CAN ONLY HOLD ABOUT 32K.
// ALSO, NOTHING IS BEING DONE ABOUT NON-PRINTABLE CHARACTERS
// OR CR-LF'S EMBEDDED IN THE STRING.

// DISPLAY THE RECEIVED TEXT.

Form1->Memo1->SetSelTextBuf(InBuff);

}

//---------------------------------------------------------------------------
void __fastcall TRead::Execute()
{
//---- Place thread code here ----


DWORD dwBytesRead;

// MAKE THE THREAD OBJECT AUTOMATICALLY DESTROYED WHEN THE THREAD
// TERMINATES.

FreeOnTerminate = true;

while(1)
{

  // TRY TO READ CHARACTERS FROM THE SERIAL PORT.
  // IF THERE ARE NONE, IT WILL TIME OUT AND TRY AGAIN.
  // IF THERE ARE, IT WILL DISPLAY THEM.


  ReadFile(hComm, InBuff, 50, &dwBytesRead, NULL);    

  if(dwBytesRead)
  {
    InBuff[dwBytesRead] = 0; // NULL TERMINATE THE STRING
    Synchronize(DisplayIt);
  }

  }

}

//---------------------------------------------------------------------------

One last thing... To do a synchronized call to DisplayIt() from within the thread's Execute() function, DisplayIt() it must be declared as a __fastcall type in the header file. Here's how to do it.

Open the header file "unit2.h" and add the DisplayIt() line as shown below:


//---------------------------------------------------------------------------
class TRead : public TThread
{
private:
protected:
void __fastcall DisplayIt(void); // ADD THIS LINE
void __fastcall Execute();
public:
__fastcall TRead(bool CreateSuspended);
};
//---------------------------------------------------------------------------



Notes...

As mentioned earlier this example focuses strictly on the core elements that make the serial communication functions work. In its present form it's unlikely to be particularly useful or acceptable in an actual application. In other words, you need to add what's missing. If you've followed along this far, that should not be too difficult. To minimize any confusion on what's missing, I'll highlight some of the areas that should be addressed:


    [*]There is little or no provision for error handling
    [*]The 32K display limit of the Memo object is not handled
    [*]For proper text display in Memo, ignore linefeeds and replace carriage returns with a CR-LF pair
    [*]Menus
    [*]Storing incoming serial data to disk
    [*]Sending disk contents out serial port
    [*]Handshaking
    [*]Protocol (Xmodem, Zmodem, etc.)

There are several ways to test your work. One method is to perform a loop-back test by jumping pins 2 and 3 on your computer's RS-232 connector. With the loop-back connection anything you type into the Memo area will be echoed back.

Here are some online references that you might find useful:

Serial Communications in Win32 . This is a comprehensive reference.
http://www.on...net/ . Excellent example of simple serial port access.
http://www.temporaldoorway.com/program...dowsapi/index.htm . Good example of threaded serial program with overlapped I/O.
http://www.codeguru.com/m.../791.shtml . Yet another example (more for VC++).

Good luck.


本帖包含附件
zip [BCB6]Sample_RS232.rar   (2022-06-09 14:02 / 207 KB)  
說明: [BCB6]RS-232 連結通訊範例
下載次數:760

此文章被評分,最近評分記錄
財富:300 (by codeboy) | 理由: 真是有用的資料阿..^^緣道大~


Digital Download Machine (DDM) 數位下載機 v2.1 (繁)
放開一點、簡單一點、單純一點;集滿三點,就會開心一點!
연도산인 DDM2.2 版難產,趕工中,敬請期待 ^^"
獻花 x1 回到頂端 [1 樓] From:臺灣中華電信HINET | Posted:2007-01-21 10:56 |
阿強 手機 會員卡
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎 優秀管理員勳章
頭銜:小人物...小人物...
版主
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

謝謝山人大大的幫忙
小弟馬上試試看

如果有問題 可以繼續問您嗎???


有些東西不能失去 為了保護它們
眼前這些渺小的東西 無論多少都能丟棄
但是我並不知道這樣最終會失去一切
獻花 x0 回到頂端 [2 樓] From:臺灣 | Posted:2007-01-22 13:04 |
緣道山人 會員卡
個人頭像
個人文章 個人相簿 個人日記 個人地圖 個人商品
頭銜:夷希微之徒~DDM 繼承人夷希微之徒~DDM 繼承人
驗證會員
級別: 榮譽會員 該用戶目前不上站
推文 x3 鮮花 x204
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

下面是引用阿強於2007-01-22 13:04發表的 :
謝謝山人大大的幫忙
小弟馬上試試看

如果有問題 可以繼續問您嗎???
當然可以呀 表情
但...不要問太難 表情


Digital Download Machine (DDM) 數位下載機 v2.1 (繁)
放開一點、簡單一點、單純一點;集滿三點,就會開心一點!
연도산인 DDM2.2 版難產,趕工中,敬請期待 ^^"
獻花 x0 回到頂端 [3 樓] From:臺灣中華電信HINET | Posted:2007-01-23 19:37 |
緣道山人 會員卡
個人頭像
個人文章 個人相簿 個人日記 個人地圖 個人商品
頭銜:夷希微之徒~DDM 繼承人夷希微之徒~DDM 繼承人
驗證會員
級別: 榮譽會員 該用戶目前不上站
推文 x3 鮮花 x204
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

http://www.cppfans.co...trol.asp
我找到這裡有元件可以使用,有時間的話可以裝來玩玩 ^_^


Digital Download Machine (DDM) 數位下載機 v2.1 (繁)
放開一點、簡單一點、單純一點;集滿三點,就會開心一點!
연도산인 DDM2.2 版難產,趕工中,敬請期待 ^^"
獻花 x0 回到頂端 [4 樓] From:臺灣中華電信HINET | Posted:2007-02-18 09:14 |
a3680p
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

表情 下載看看,先謝謝了!


獻花 x0 回到頂端 [5 樓] From:臺灣中華電信股份有限公司 | Posted:2012-03-04 22:37 |
leo8998
數位造型
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

如果想增加COM PORT到11應該怎樣改?


獻花 x0 回到頂端 [6 樓] From:未知地址 | Posted:2012-03-20 17:20 |
iamboss123
個人文章 個人相簿 個人日記 個人地圖
小人物
級別: 小人物 該用戶目前不上站
推文 x0 鮮花 x3
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

表示 好複雜 沒看懂 慢慢學習吧


獻花 x0 回到頂端 [7 樓] From:河北 | Posted:2014-01-12 14:34 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.080263 second(s),query:16 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言