广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 43752 个阅读者
 
发表文章 发表投票 回覆文章
  可列印版   加为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.062771 second(s),query:16 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言