Serial Port Programming using Win32 API(转载)

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 likeCOM32,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.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.
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");
  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
                   NULL,            // No Security
                   OPEN_EXISTING,   // Open existing port only
                   0,               // Non Overlapped I/O
                   NULL);

 
 

Configuring the DCB Structure
In Windows ,settings like Baud rate ,Number of start/Stop bits,data formats etc for the serial port are controlled by the DCB structure.

To Configure the DCB structure we use two functions,
GetCommState() function which retrieves the current control settings of the serial port and
SetCommState() function which configures the serial port with the new values in DCB structure provided by us.

First you declare a new structure of type DCB and initializes it.

DCB dcbSerialParams = { 0 }; // Initializing DCB structure
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

After that retrieve the current settings of the serial port using the GetCommState() function.

Status = GetCommState(hComm, &dcbSerialParams);

and set the values for Baud rate, Byte size, Number of start/Stop bits etc.

dcbSerialParams.BaudRate = CBR_9600;  // Setting BaudRate = 9600
dcbSerialParams.ByteSize = 8;         // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT;// Setting StopBits = 1
dcbSerialParams.Parity   = NOPARITY;  // Setting Parity = None

if you want to change the Baud rate values prefix standard values with CBR like this CBR_4800,CBR_9600,CBR_192600 etc.

Number of Stop bits can be changed to ONESTOPBIT or TWOSTOPBITS.
Parity can also be changed to EVENPARITY,ODDPARITY,NOPARITY etc.

Now its time to configure the serial port according to the DCB structure using SetCommState()function.

SetCommState(hComm, &dcbSerialParams);

Setting Timeouts
Timeouts helps to prevent your program from waiting endlessly till data arrives. It helps the read or write calls to return after a set time period has elapsed.
 

COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout         = 50; // in milliseconds
timeouts.ReadTotalTimeoutConstant    = 50; // in milliseconds
timeouts.ReadTotalTimeoutMultiplier  = 10; // in milliseconds
timeouts.WriteTotalTimeoutConstant   = 50; // in milliseconds
timeouts.WriteTotalTimeoutMultiplier = 10; // in milliseconds
 

All values are in milliseconds.

ReadIntervalTimeout Specifies the maximum time interval between arrival of two bytes. If the arrival time exceeds these limits the ReadFile() function returns.

ReadTotalTimeoutConstant is used to calculate the total time-out period for read operations. For each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier member and the requested number of bytes.

ReadTotalTimeoutMultiplier is used to calculate the total time-out period for read operations. For each read operation, this value is multiplied by the requested number of bytes to be read.

WriteTotalTimeoutConstant similar to ReadTotalTimeoutConstant but for write operation.

WriteTotalTimeoutMultiplier similar to ReadTotalTimeoutMultiplier but for writeoperation.

After this you have to set the values using SetCommTimeouts() function.

Writing Data to Serial Port

Writing data to the opened serial port is accomplished by the WriteFile() function. WriteFile() function can be used to write both into the files and I/O ports.


char lpBuffer[] = "A";
DWORD dNoOFBytestoWrite;         // No of bytes to write into the port
DWORD dNoOfBytesWritten = 0;     // No of bytes written to the port
dNoOFBytestoWrite = sizeof(lpBuffer);

Status = WriteFile(hComm,        // Handle to the Serial port
                   lpBuffer,     // Data to be written to the port
                   dNoOFBytestoWrite,  //No of bytes to write
                   &dNoOfBytesWritten, //Bytes written
                   NULL);

hComm Handle of the serial port to write.

lpBuffer[] character array containing the data to write into the serial port.

dNoOFBytestoWrite is the total number of bytes to be written into the serial port.Here we are using the sizeof() operator to find out that .

dNoOFBytestoWrite = sizeof(lpBuffer);

dNoOfBytesWritten is the total number of bytes written successfully to the port by theWriteFile() operation.

Now in the Zip File containing the source codes you can find "USB2SERIAL_Write_W32.c" which contains the complete code for writing into serial port. You can compile the code using Visual Studio Express or GCC.

If your PC does not have any hardware serial ports you can use any USB to Serial Converters(I am using USB2SERIAL).

I have interfaced a microcontroller board(MSP430G2553 on Launch Pad) to the serial port using a null modem cable like this

You can use any microcontroller of your choice like 8051,AVR or ARM(LPC2148).The Controller waits for a character to be received and lights up the corresponding LED. The code for MSP430 is included in the zip file.If you want to know how to configure the MSP430 controller UART you can check this tutorial.


Please note that if you are using a DB9 RS232 Serial Port of your PC, you will have to build a RS232 signal level converter at the microcontroller side to decode the RS232 signal.
Directly connecting the PC's RS232 Serial port to MSP430 's pins will damage the chip.
Here is the screen shot of the Program writing into serial port.

 
 

Reading from the Serial Port
Reading from the serial port is accomplished by the ReadFile() function.

One way to do that is to use polling where the ReadFile() continuously reads from the serial port and checks for any received characters.
Other way is to setup an event and let windows notify us when a character is received.

We are going to use the second method here, following are the steps.

1. Create an Event for a particular action like character reception, change in modem lines etc usingSetCommMask() function .
2. Ask windows to wait for the event set by SetCommMask() function using WaitCommEvent() and notify us when the condition happens.
3. Call ReadFile () to read the received data from the Serial port.

Functions used are

SetCommMask() is used to set the events to be monitored for a communication device. Here we are going to set the event as character received (EV_RXCHAR).The function takes two arguments, Handle of the serial port (hComm) and the code for the event (EV_RXCHAR) to be monitored.

Status = SetCommMask(hComm, EV_RXCHAR);

WaitCommEvent() is used to wait for the events set by SetCommMask() to happen, in this case reception of a character. The flow of execution of the program stops and the program waits until a character is received.   

DWORD dwEventMask; 
Status = WaitCommEvent(hComm, &dwEventMask, NULL);  

dwEventMask contains a hex value indicating the event, which has caused WaitCommEvent() to return.

After WaitCommEvent() has returned, call ReadFile() function to read the received characters from the Serial Port Buffer.


char TempChar; //Temporary character used for reading
char SerialBuffer[256];//Buffer for storing Rxed Data
DWORD NoBytesRead;
int i = 0;

do
 {
   ReadFile( hComm,           //Handle of the Serial port
             &TempChar,       //Temporary character
             sizeof(TempChar),//Size of TempChar
             &NoBytesRead,    //Number of bytes read
             NULL);

   SerialBuffer[i] = TempChar;// Store Tempchar into buffer
   i++;
  }

while (NoBytesRead > 0);

ReadFile() function is similar to the WriteFile() function we had seen earlier,instead of writing we are reading from the serial port.

&TempChar Temporary variable used to store the byte read from serial port buffer.

sizeof(TempChar) used to calculate the number of bytes to read.

&NoBytesRead Bytes successfully read by the ReadFile().

Now in the Zip File containing the source codes you can find "USB2SERIAL_Read_W32.c" which contains the complete code for writing into serial port. You can compile the code using Visual Studio Express or GCC.

On running "USB2SERIAL_Read_W32.exe" ,The code will wait for the characters to be transmitted by the microcontroller.

Reset the Micro controller to transmit the string "Hello from MSP430".

原文地址:http://xanthium.in/Serial-Port-Programming-using-Win32-API

时间: 2024-07-29 05:29:04

Serial Port Programming using Win32 API(转载)的相关文章

Serial Port Programming on Linux(转载)

This is a tutorial on how to program the Serial Ports on your Linux box.Serial Ports are nice little interfaces on the PC which helps you to interface your embedded system projects using a minimum number of wires.In this tutorial we will write a smal

使用Win32 API创建不规则形状&amp;带透明色的窗口

前一阵突然想起了9月份电面某公司实习时的二面题,大概就是说怎么用Win32 API实现一个透明的窗口,估计当时我的脑残答案肯定让面试官哭笑不得吧.所以本人决定好好研究下这个问题.经过一下午的摸索,基本掌握了使用Win32 API创建各种匪夷所思的窗口的基本思路. (以下文字基于本人的个人理解,由于本人技术和经验原因不保证正确性,希望各位不吝指正) 首先我们需要了解一些基础知识. 1.Layered Windows.这是Windows2000开始引入的新概念,重新定义了窗口的Hit Testing

android4.0下serial port给应用操作完成特殊定制

android4.0下serial port给应用操作完成特殊定制         我们在开发中,串口也就是serialport或者叫uart用的是相当频繁的,很普通的接口了,今天为什么在这提出来呢?笔者前年完成了一款android4.0平台的车载平板产品,客户外接了一个DTV,我们在android这边通过GPIO模拟IR来控制DTV盒子的.客户前面也做了特殊的一些应用,可以通过wifi跟服务器连.服务器通过wifi网络发送控制命令给车载平板机器,但是客户反馈在wifi 在heavy WiFi

dotNET C# Programmer’s Guide to the Win32 API (Win

dotNET C# Programmer's Guide to the Win32 API (Win32 API 声明C#版) 小气的神 2001.09.07 噢,对不起我用了这个标题,事实上如果我能做到10%,我就很高兴了.因为我的桌上正放着一本Dan Appleman的书,它曾伴随我许多年,也许你也有一本:1215页,蓝色书皮,机械工业出版社.英文名叫:< Dan Appleman's Visual Basic 5.0 Programmer's Guide to the Win32 API

如何在C#中使用Win32 API和其他库

C# 用户经常提出两个问题:"我为什么要另外编写代码来使用内置于 Windows 中的功能?在框架中为什么没有相应的内容可以为我完成这一任务?"当框架小组构建他们的 .NET 部分时,他们评估了为使 .NET 程序员可以使用 Win32 而需要完成的工作,结果发现 Win32 API 集非常庞大.他们没有足够的资源为所有 Win32 API 编写托管接口.加以测试并编写文档,因此只能优先处理最重要的部分.许多常用操作都有托管接口,但是还有许多完整的 Win32 部分没有托管接口. 平台

用Win32 API实现串行通信

串口是常用的计算机与外部串行设备之间的数据传输通道,由于串行通信方便易行,所以应用广泛.我们可以利用Windows API 提供的通信函数编写出高可移植性的串行通信程序. 在Win16中,可以利用OpenComm.CloseComm和WriteComm等函数打开.关闭和读写串口.但在Win32中,串口和其他通信设备均被作为文件处理,串口的打开.关闭和读写等操作所用的API函数与操作文件的函数相同.可通过CreateFile函数打开串口,通过CloseFile函数关闭串口,通过CommProp.D

深入浅出VC++串口编程之基于Win32 API

1.API描述 在WIN32 API中,串口使用文件方式进行访问,其操作的API基本上与 文件操作的API一致. 打开串口 Win32 中用于打开串口的API 函数为 CreateFile,其原型为: HANDLE CreateFile ( LPCTSTR lpFileName, //将要打开的串口逻辑名,如COM1 或COM2 DWORD dwAccess, //指定串口访问的类型 ,可以是读取.写入或两者并列 DWORD dwShareMode, //指定共享属性,由于串口不 能共享,该参数

Win32 API实现的组件对象集

本组件实现了七个COM对象: 一.COM组件,实现了两个 Dispatch 接口 Itest(默认接口): 实现了BASE64加解密,发送邮件,以给定分隔字符把字符串转换成数组,钩子安装,设置IIS默认站点的主目录,拷贝文件夹,删除文件夹,创建多级目录,以给定日期计算星期几,得到内存大小,创建快捷方式,注册COM组件,检测WINDOWS版本,得到磁盘剩余空间,得到程序组路径 IOpen: 调用"打开文件"通用对话框,并对其定制,实现了文本文件的预览.调用"浏览"通用

系统理解Win32 API和MFC(下)

二.MFC的概念模型 前面我们研究了WIN32 API的"领域模型",对它有较全面的认识.下面,对MFC概念模型的研究,我们把重点放在对app framework的研究上. app framework中的message响应/传递机制是最重要的.而Hook机制和Message响应/传递机制是密切相关的,后者以前者为基础. 1. Hook机制 也许有些程序员只知道hook机制可以编写很"牛"的应用,孰不知MFC本身也是依靠hook机制的. 从图中看到,每个hook拥有一