windows环境下c语言串口通信(Visual Studio上可运行)

一、准备:

1、Visual Studio等windows下可编译运行C语言程序的软件
(以下两个用于测试调试用)
2、虚拟串口工具(用于建立虚拟串口进行通信调试)
在运行程序前用虚拟串口工具把对应的com1、com2调成一对串口
3、也可以用串口助手和程序进行调试

二、文件:

1、 man.c 主程序
2、serial.h 头文件
3、serial.c 函数定义文件

三、代码

man.c

#include <stdio.h>
#include <windows.h>
#include "serial.h"

int serial_deal()
{
   
	PORT COM1;
	PORT COM2;
	char buff[1024] = {
    0 };
	int rcv_len = 0;

	printf("open com1\n");
	COM1 = serial_init(1, 115200, 8, 1, 0);
	printf("open com2\n");
	COM2 = serial_init(2, 115200, 8, 1, 0);
	while (1)
	{
   
		Serial_SendData(COM1, "i am one one fish\n", 17);
		memset(buff, 0, 1024);
		rcv_len = Serial_ReciveData(COM2, buff, 1024);
		printf("com2 rcv:%s\n", buff);
		Sleep(1000);//单位ms,共睡眠1s
	}
}
int main()
{
   
	serial_deal();
	return 0;
}

serial.h

#ifndef __SERIAL_H
#define __SERIAL_H
#include <windows.h>
typedef HANDLE PORT;

PORT OpenPort(int idx);
void ClosePort(PORT com_port);
int SetPortBoudRate(PORT com_port, int rate);
int SetPortDataBits(PORT com_port, int bits);
int SetPortStopBits(PORT com_port, int bits);
int SetPortParity(PORT com_port, int parity);
int GetPortBoudRate(PORT com_port);
int GetPortDataBits(PORT com_port);
int GetPortStopBits(PORT com_port);
int GetPortParity(PORT com_port);
int SendData(PORT com_port, const char * data);
int ReciveData(PORT com_port, char *
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值