Test with virtual serial port software
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);
};
The beginning of ===========================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);
The end of the ===========================serialport.h =========================
main.cpp/////////////////////////////////////// /////////////////////////////
Using code Examples:
#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 serial Port transfer data