没有合适的资源?快使用搜索试试~ 我知道了~
首页Win32串口通讯编程(不借助MSCOMM控件也能行)
不借助MSCOMM控件进行串口编程,因为资源分没有0分,所以只能这样。如果不想下载,可以看原始网址:http://xanthium.in/Serial-Port-Programming-using-Win32-API,我打印的是网页,广告什么的都直接打印上了,浏览了一下,应该没有什么不好的内容。如果有人觉得不好,请联系我删除。This file need 1 resource charge. because there is no "Zero" selection. If someone thinks this file should be upload, please contact me , I will delete it When I see your request. Thanks。
资源详情
资源评论
资源推荐
2018/5/16 Serial Port Programming on Windows using Win32 API | xanthium enterprises
http://xanthium.in/Serial-Port-Programming-using-Win32-API 1/11
xanthium enterprises
xanthium enterprises
embedded systems and robotics for all
Products
RS485/Serial/I2C/SPI Controlled 6
Channel Relay Control Board
ATtiny2313 Development Board with
Quad Motor Control and
Serial/RS485 Interface
Tweet
7
Share
Home
»
»
Serial Port Programming using Win32 API
Serial Port Programming using Win32 API
Serial Port Programming using Win32 API
Submitted by Rahul.Sreedharan on 7 June 2015 - 10:33am
In this tutorial we will learn How to communicate with an external device like a microcontroller board or
modem using the Serial port of a windows PC(Windows XP,7). The software is written using C
language and communicates with the Serial Port using Win32 API.
In Windows ,Serial ports are named as COM1,COM2 ,COM3.. etc .COM1 and COM2 usually refer to
the hardware serial ports present in the PC while COM numbers in double digits like
COM32,COM54,COM24.. etc are given to USB to Serial Converters or PCI serial port extenders.
If your PC does not have any hardware serial ports (RS232 DB9 ports), you can use USB to Serial
Converter's like USB2SERIAL.
If you are interested in setting up an RS485 network controlled by your PC to control a robot or a
sensor network ,you can use USB2SERIAL board (buy here).
The Board can also be used as an FT232 development board.
Sourcecodes
All the C sourefiles used in this tutorial can be downloaded from our GitHub Page.
Please note that the sourcecodes on the website show only the relevant sections to highlight the
process of programming the serial port.
Please use the complete sourcecodes from our github repo when building your own program.
If you are new to Github Check this article to download code .
Compilers and IDE's used
To Compile the C file you can use either Visual Studio Express edition from Microsoft or MinGW
(Windows port of GCC).
Visual Studio 2013 Express Edition for Desktop can be freely downloaded (its a large download) from
Microsoft's website. Here is a short tutorial on how to setup a C project in VS 2013.
Home
Home
Store
Store
Products
Products
Tutorials
Tutorials
Source Codes
Source Codes
Contact
Contact
2018/5/16 Serial Port Programming on Windows using Win32 API | xanthium enterprises
http://xanthium.in/Serial-Port-Programming-using-Win32-API 2/11
MSP430 Motor Control Booster Pack
USB to Serial/RS232/RS485
Converter
Contact Us
Phone
+91 81055 65924
Address
#5,Near Fidelity Financial
Electronics City Phase 2
Hosur Road
Bangalore 560100
India
Email
Contact Us
User login
Username *
Password *
Create new account
Request new password
If you don’t want to download the Visual Studio and want something simpler you can use MinGW,
which is an open source port of GCC for windows environment. After you have installed the MinGW,
package make sure to add gcc to system path so that you can call it from anywhere.
Finding out your COM port Number
To find out the COM number corresponding to your serial port, Open Device Manager by right clicking
on My Computer icon and selecting Manage → Device Manager.Under Ports(COM & LPT) you can
see the parallel and serial ports (COM) detected by your system.
If your PC has any hardware ports, it will be shown either as COM1 or COM2 under the Ports Section. I
am using a FTDI based USB to Serial Converter (USB2SERIAL)which is recognized as COM24 (this
may be different under your system). If you double click on COM24,you can see the details of the
corresponding port.
Opening and Closing a Serial Port
In Windows we use the CreateFile() function to open a serial port.
CreateFile() is a Win 32 function which is used to create or open a file, stream or an IO device like
serial port.On success CreateFile() will return a handle which is then used to refer the connection in all
subsequent operations.
After opening a serial port using the CreateFile() function you should close it with CloseHandle()
function, otherwise port will become unavailable to other programs.
Now let's write a small program to open and close a serial port on Windows. Open a text editor like
notepad or Notepad++ and type the below code and save it as “serial.c”.If you are using IDE like VS
Express, use the one integrated with it.
#include<windows.h>
#include<stdio.h>
int main()
{
HANDLE hComm;
hComm = CreateFile(“\\\\.\\COM24”, //port name
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING,// Open existing port only
0, // Non Overlapped I/O
NULL); // Null for Comm Devices
if (hComm == INVALID_HANDLE_VALUE)
printf(“Error in opening serial port”);
Log in
Log in
2018/5/16 Serial Port Programming on Windows using Win32 API | xanthium enterprises
http://xanthium.in/Serial-Port-Programming-using-Win32-API 3/11
else
printf(“opening serial port successful”);
CloseHandle(hComm);//Closing the Serial Port
return 0;
}
Find out the COM port corresponding to your system and substitute in CreateFile() instead of
COM24.
Now compile and run the program by pressing F5 in Visual Studio
or
by running the following command for gcc (MingW).Please make sure that gcc is added to you system
path.
D:\> gcc -o serial serial.c
Now let me explain the code ,
windows.h header file contain all the definitions, function prototypes and constants required by the
program.
In Windows everything is controlled by using handles.In the first line
HANDLE hComm;
we declare a handle hcomm to access and control the serial port.
Next we open a connection to serial port using CreateFile() function. The CreateFile() function on
success, returns a valid handle to the hComm variable.
CreateFile() function takes 7 arguments,
1. Name of the serial port to be opened here \\\\.\\COM24.
2. Mode of access, here Read and Write
3. Sharing options, Serial ports can't be shared so 0
4. NULL for Serial ports, used for File operations
5. Open the existing port, OPEN_EXISTING
6. Overlapped IO or Non overlapped IO, here 0 means we are using NonOverlapped IO. Overlapped
IO is used for multithreaded programs where
several threads can interact with the port simultaneously.
7. NULL for Serial port, used for file operations
If the function succeeds in opening the serial port, it returns a valid handle to hcomm which is then used
for error checking.
After that the connection to the port is closed using
CloseHandle(hComm);
Please note that in Windows, COM port numbers from COM1 to COM9 are reserved by the system.
If you are using a serial port whose COM port number falls in that range, you don’t need the back
slashes (\\\\.\\)shown in the above code.
You can access the serial port like this,
hComm = CreateFile(“COM1”, // for COM1—COM9 only
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
剩余10页未读,继续阅读
haimian520
- 粉丝: 37
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 2023年中国辣条食品行业创新及消费需求洞察报告.pptx
- 2023年半导体行业20强品牌.pptx
- 2023年全球电力行业评论.pptx
- 2023年全球网络安全现状-劳动力资源和网络运营的全球发展新态势.pptx
- 毕业设计-基于单片机的液体密度检测系统设计.doc
- 家用清扫机器人设计.doc
- 基于VB+数据库SQL的教师信息管理系统设计与实现 计算机专业设计范文模板参考资料.pdf
- 官塘驿林场林防火(资源监管)“空天地人”四位一体监测系统方案.doc
- 基于专利语义表征的技术预见方法及其应用.docx
- 浅谈电子商务的现状及发展趋势学习总结.doc
- 基于单片机的智能仓库温湿度控制系统 (2).pdf
- 基于SSM框架知识产权管理系统 (2).pdf
- 9年终工作总结新年计划PPT模板.pptx
- Hytera海能达CH04L01 说明书.pdf
- 数据中心运维操作标准及流程.pdf
- 报告模板 -成本分析与报告培训之三.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0