一、准备:
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 *