C + + Windows serial Port transfer data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.