求高手赐一段串口程序

来源:百度知道 编辑:UC知道 时间:2024/06/30 06:13:32
本人正在学习C编程,特向高手讨要一段串口程序
(内容不限,是串口程序就好.不要太长,小于50行即可)
用于研习,观摩

//不懂的话再问我吧.
//我给你一个我自己以前写的串口操作的函数,包括两部份,一部分主CPP文件,一部分为(头)H文件.
//以下的代码在VC++6.0下测试通过.
//将下面的两部分代码分别保存在serialport.cpp文件和serialport.h文件中.
//最后面是一个简单的使用举例.
//===========================SerialPort.cpp的开始=========================
#include "SerialPort.h"

HANDLE OpenSerialPort(string & strPort,ULONG ulBaudrate)
{
HANDLE hSerial;
hSerial = CreateFile((strPort.c_str()),
GENERIC_READ|GENERIC_WRITE,
NULL,
NULL,
OPEN_EXISTING,
NULL,
NULL);

if(hSerial == INVALID_HANDLE_VALUE)//Open serial port failed.
{
return hSerial;
}
//Config serial port
DCB PortDCB;
PortDCB.DCBlength = sizeof(DCB);
// Get the default serial parameter.
GetCommState(hSerial, &PortDCB);
PortDCB.BaudRate = ulBaudrate; // baudrate
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONESTOPBIT; <