admin 管理员组

文章数量: 1184232

                                         C语言  串口读写简单例子(windows)

一、简述

      记--在windows系统使用C语言读写串口的例子。(windows API)

      例子打包:下载:https://wwm.lanzouq/iyDBp08gif4h 密码:6wjt

二、测试代码

代码中读COM2数据,循环接收消息,收到消息后将消息内容打印并回复I_RECEIVE, 如果收到EXIT_STR就回复EXIT_STR并退出循环。

#include <stdio.h>
#include <windows.h>
#include <string.h>

#if 1   //开启DEBUG打印
#define LOGD(...) printf(__VA_ARGS__)
#else   //关闭DEBUG打印
#define LOGD(...)
#endif

#if 1   //开启ERROR打印
#define LOGE(...) printf(__VA_ARGS__)
#else   //关闭ERROR打印
#define LOGE(...)
#endif

//缓冲区大小
#define BUF_SIZE    2048
#define EXIT_STR    "exit"
#define I_EXIT      "I exit.\r\n"
#define I_RECEIVE   "I receive.\r\n"

//打开串口
HANDLE OpenSerial(const char *com, //串口名称,如COM1,COM2
    int baud,       //波特率:常用取值:CBR_9600、CBR_19200、CBR_38400、CBR_115200、CBR_230400、CBR_460800
    int byteSize,   //数位大小:可取值7、8;
    int parity,     //校验方式:可取值NOPARITY、ODDPARITY、EVENPARITY、MARKPARITY、SPACEPARITY
    int stopBits)   //停止位:ONESTOPBIT、ONE5STOPBITS、TWOSTOPBITS;
{    
    DCB dcb;
    BOOL b = FALSE;
	COMMTIMEOUTS CommTimeouts;	
    HANDLE comHandle = INVALID_HANDLE_VALUE;

    //打开串口
	comHandle = CreateFile(com,            //串口名称
		GENERIC_READ | GENERIC_WRITE,      //可读、可写   				 
		0,            // No Sharing                               
		NULL,         // No Security                              
		OPEN_EXISTING,// Open existing port only                     
		FILE_ATTRIBUTE_NORMAL,            // Non Overlapped I/O                           
		NULL);        // Null for Comm Devices

	if (INVALID_HANDLE_VALUE == comHandle) {
        LOGE("CreateFile fail\r\n");
        return comHandle;
    }

    // 设置读写缓存大小
	b = SetupComm(comHandle, BUF_SIZE, BUF_SIZE);
    if (!b) {
        LOGE("SetupComm fail\r\n");
    }

    //设定读写超时
	CommTimeouts.ReadIntervalTimeout = MAXDWORD;//读间隔超时
	CommTimeouts.ReadTotalTimeoutMultiplier = 0;//读时间系数
	CommTimeouts.ReadTotalTimeoutConstant = 0;//读时间常量
	CommTimeouts.WriteTotalTimeoutMultiplier = 1;//写时间系数
	CommTimeouts.WriteTotalTimeoutConstant = 1;//写时间常量
	b = SetCommTimeouts(comHandle, &CommTimeouts); //设置超时
    if (!b) {
        LOGE("SetCommTimeouts fail\r\n");
    }

    //设置串口状态属性
    GetCommState(comHandle, &dcb);//获取当前
	dcb.BaudRate = baud; //波特率
	dcb.ByteSize = byteSize; //每个字节有位数
	dcb.Parity   = parity; //无奇偶校验位
	dcb.StopBits = stopBits; //一个停止位
	b = SetCommState(comHandle, &dcb);//设置
    if (!b) {
        LOGE("SetCommState fail\r\n");
    }

    return comHandle;
}

int main(int argc, char *argv[])
{
    BOOL b = FALSE;
    DWORD wRLen = 0;
    DWORD wWLen = 0;
    char buf[BUF_SIZE] = {0};
	HANDLE comHandle = INVALID_HANDLE_VALUE;//串口句柄    	

    //打开串口
    const char *com = "COM2";
	comHandle = OpenSerial(com, CBR_9600, 8, NOPARITY, ONESTOPBIT);
	if (INVALID_HANDLE_VALUE == comHandle) {
		LOGE("OpenSerial COM2 fail!\r\n");
		return -1;
	}
	LOGD("Open COM2 Successfully!\r\n");

    //循环接收消息,收到消息后将消息内容打印并回复I_RECEIVE, 如果收到EXIT_STR就回复EXIT_STR并退出循环
	while (1) {
		wRLen = 0;
        //读串口消息
		b = ReadFile(comHandle, buf, sizeof(buf)-1, &wRLen, NULL);
		if (b && 0 < wRLen) {//读成功并且数据大小大于0
            buf[wRLen] = '\0';
            LOGD("[RECV]%s\r\n", buf);//打印收到的数据
            if (0 == strncmp(buf, EXIT_STR, strlen(EXIT_STR))) {
                //回复
                b = WriteFile(comHandle, TEXT(I_EXIT), strlen(I_EXIT), &wWLen, NULL);
                if (!b) {
                    LOGE("WriteFile fail\r\n");
                }
                break;
            }
            //回复
			b = WriteFile(comHandle, TEXT(I_RECEIVE), strlen(I_RECEIVE), &wWLen, NULL);
            if (!b) {
                LOGE("WriteFile fail\r\n");
            }
		}
	}

    //关闭串口
	b = CloseHandle(comHandle);
    if (!b) {
        LOGE("CloseHandle fail\r\n");
    }

    LOGD("Program Exit.\r\n");
    return 0;
}

三、测试效果

其中com1和com2是使用串口虚拟工具Virtual Serial Port Driver虚拟出来的一对串口。也就是COM1 《==》COM2,两个串口时连通的,COM1发的数据COM2会收到,反之亦然。

 

本文标签: 串口 例子 语言 简单 Windows