QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3206|回复: 1
打印 上一主题 下一主题

Send and receive data from a serial port (RS-232)

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 12:58 |只看该作者 |倒序浏览
|招呼Ta 关注Ta

! |4 r3 A& o+ c6 j$ M% ?Answer * U$ N; O. _4 A3 kDOS programmers may be familiar with how to send and receive serial data by reading and writing directly from the port's hardware. In windows, you do not interface with the hardware directly. Instead, you use the CreateFile, ReadFile, and WriteFile API functions to send and receive data. The following code snippet demonstrates how to open a serial port and send the message "hello world".

* e; i. ]: {, C4 e) V

void __fastcall TForm1::Button1Click(TObject *Sender) 8 p$ @7 ]- U5 P0 ?{ 6 y* X+ f2 |2 `# p5 ] HANDLE hCom = CreateFile("COM1", 2 \6 k' e3 E! Z; g7 P# w8 | GENERIC_READ | GENERIC_WRITE,8 T# ]( d; L8 v1 K& `( _ 0, 1 q0 P# ]) `1 j2 @) s$ t6 h NULL,! \6 Y" x1 _7 v4 o OPEN_EXISTING,. p2 y4 V) [& s1 F. l 0, % y: G% V9 ]% b NULL );

; {. w/ j& `$ W4 e/ t

if(hCom) 8 n" C; \, I% V {

( z9 Q6 Y4 h9 [9 X( B c& X

DCB dcb;6 p" h1 U$ \3 \0 P9 o6 J6 V ZeroMemory(&dcb, sizeof(dcb));% y: @1 Q6 ~$ _4 O" e6 y6 A5 Z: ^ dcb.DCBlength = sizeof(dcb);

' u# l8 ^8 `' w- M8 l

dcb.BaudRate = 57600; 8 w0 U& W3 s, Y! j" k9 s4 d- g dcb.ByteSize = 8; 6 P+ A" d2 O; W2 R dcb.Parity = NOPARITY; // NOPARITY and friends are 3 y: S/ s7 S- q: s+ R3 S: `/ k0 ^ dcb.StopBits = ONESTOPBIT; // #defined in windows.h

: c# @( S. |2 V& ]) b! v

// Set the port properties and write the string out the port. & J/ _) x$ {& j9 C( O if(SetCommState(hCom,&dcb)) ! u( d" q0 B, ~6 f- t( T { + }- q; { u6 p- @+ {, R DWORD ByteCount;( e0 L/ }. T* [# n7 P' i const char *msg = "Hello world!"; 1 N* v, R1 Y+ d/ R" S WriteFile(hCom,msg, strlen(msg),&ByteCount,NULL);7 o& n4 S* S, N+ s9 I$ y }* m8 s) v/ E" v8 n6 R) a CloseHandle(hCom);" m6 B/ E3 ?% J, a) U: K8 _' P0 I9 j3 l } ( I$ k# M- i1 x- s. m' Z}

- }9 p: }+ B I/ u

This code example demonstrates how to get started with serial port programming in windows, but it is far from being complete. For more information, read the help section entitled "Communications" in the win32.hlp help file. The API provides a host of functions for configuring the port.

: Z) |8 [, g9 k# c

Note: The communications API functions are not that easy to work with from code. You may want to explore using a class or component that encapsulates the serial port. Turbopower sells a product called AsyncPro that can be used for serial communication. There are also some freeware alternatives out there. I also have a a class that encapsulates the serial port. You can download it from the table below. My class is very simple, and probably not quite as powerful as some of the third party products that you can buy. But it does make it a lot easier to send and receive data when compared with the raw api calls. Note that my class is just that, a class. It is not a graphical component.

& ~ s5 {; v, m; r3 w7 C7 o, h& z9 B

* i# s5 a* d; M1 g$ v& O$ i/ z

zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信

0

主题

5

听众

85

积分

升级  84.21%

该用户从未签到

新人进步奖

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-18 09:44 , Processed in 0.643271 second(s), 58 queries .

回顶部