1. 程式人生 > >c++ windows串口傳輸數據

c++ windows串口傳輸數據

ring stat 代碼 value fail ret use getc wait

用虛擬串口軟件 測試

/////////////////////////////////////////////////////SerialPort.h////////////////////////////////////////////////////////

#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;
if (!SetCommState(hSerial, &PortDCB))
{
//config the serial port failed.
CloseHandle(hSerial);
return (INVALID_HANDLE_VALUE);
}
//Config the serial port timeout property.
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(hSerial, &CommTimeouts);//Get the serial port default timeout.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 10;
CommTimeouts.ReadTotalTimeoutConstant = 10;
CommTimeouts.WriteTotalTimeoutMultiplier = 50;
CommTimeouts.WriteTotalTimeoutConstant = 100;
if (!SetCommTimeouts(hSerial, &CommTimeouts))
{
//config the serial port timeout failed;
CloseHandle(hSerial);
return (INVALID_HANDLE_VALUE);
}
return (hSerial);
};
void CloseSerialPort(HANDLE hSerial)
{
CloseHandle(hSerial);
};
bool WriteSerial(char* btData, ULONG ulSize, HANDLE hSerial)
{
DWORD dwNumBytes, dwRet;
dwRet = WriteFile(hSerial, //Handle
btData, //Data buffer
ulSize, //Buffer size
&dwNumBytes, //written bytes
NULL); //don‘t support
return (dwRet != 0);
};
bool ReadSerial(BYTE *btData, ULONG *ulSize, HANDLE hSerial)
{
//SetCommMask (hSerial, EV_RXCHAR);
ULONG ulRdSize = 0;
BYTE Byte;
DWORD dwBytes;
bool blRd = false;
DWORD dwMask = EV_RXCHAR;
//WaitCommEvent(hSerial,&dwMask,NULL);
SetCommMask(hSerial, EV_RXCHAR);
while (ReadFile(hSerial, &Byte, 1, &dwBytes, 0) != 0) {
if (dwBytes>0) {
btData[ulRdSize++] = Byte;
if (ulRdSize == *ulSize) {
blRd = true;
break;
}
}
else {
blRd = true;
break;
}
}

*ulSize = ulRdSize;
return (blRd);
};


//===========================SerialPort.H的開始=========================

#pragma once
#include <windows.h>
#include <string>

using namespace std;
HANDLE OpenSerialPort(string & strPort, ULONG ulBaudrate);
void CloseSerialPort(HANDLE hSerial);
bool WriteSerial(char* btData, ULONG ulSize, HANDLE hSerial);
bool ReadSerial(BYTE *btData, ULONG *ulSize, HANDLE hSerial);
//===========================SerialPort.H的結束=========================

////////////////////////////////////////////////////////////////main.cpp////////////////////////////////////////////////////////////////////

//使用代碼示例:
#include "SerialPort.h"
#include <iostream>
using namespace std;

HANDLE hSerialPort;

int main()
{
string portname = "COM4:";
string buff = "HELLO!";
char c[20];
strcpy_s(c, buff.c_str());

hSerialPort = OpenSerialPort( portname, 9600);//Open serial port com1

if (hSerialPort != INVALID_HANDLE_VALUE)
{
if (!WriteSerial(c, 7, hSerialPort))//Send data hello to com1
//MessageBox(0, "ERROR", "ERROR", MB_ICONWARNING);//show error message
printf("ERROR!!");
//CloseSerialPort(hSerialPort);
}
system("pause");
}

c++ windows串口傳輸數據