2andnot

SerialPort_com_port_cpp

Sep 15th, 2020
737
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstring>
  2. #include <iostream>
  3. #include "windows.h"
  4. #include "SerialPort.h"
  5.  
  6. using namespace std;
  7.  
  8. HANDLE initSerial(short int port, DWORD baudrate, BYTE byteSize, BYTE stopbit, BYTE parrity){
  9.     HANDLE uartPort;
  10.     char portName[14];
  11.     char portNumber[4];
  12.     strcpy(portName, "\\\\.\\COM");
  13.     itoa(port, portNumber, 10);
  14.     strcat(portName, portNumber);
  15.     uartPort = CreateFileA(portName, GENERIC_READ | GENERIC_WRITE, NULL,0, OPEN_EXISTING, 0, NULL);
  16.     if (uartPort == INVALID_HANDLE_VALUE) {
  17.         if (GetLastError() == ERROR_FILE_NOT_FOUND) {
  18.             cout << "serial port COM"<< uartPort << " does not exist\n";
  19.         }
  20.         cout << "come other error occured.\n";
  21.         exit(1);
  22.     }
  23.     DCB dcbSerialParams = { 0 };
  24.     dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  25.     if (!GetCommState(uartPort, &dcbSerialParams))
  26.     {
  27.         cout << "getting state error\n";
  28.         exit(1);
  29.     }
  30.     dcbSerialParams.BaudRate = baudrate;
  31.     dcbSerialParams.ByteSize = byteSize;
  32.     dcbSerialParams.StopBits = stopbit;
  33.     dcbSerialParams.Parity = parrity;
  34.     if (!SetCommState(uartPort, &dcbSerialParams))
  35.     {
  36.         cout << "error setting serial port state\n";
  37.         exit(1);
  38.     }
  39.     COMMTIMEOUTS CommTimeOuts = { 0 };
  40.     if(GetCommTimeouts(uartPort, &CommTimeOuts)){
  41.         cout << "CommTimeOuts.ReadIntervalTimeout" << CommTimeOuts.ReadIntervalTimeout << endl;
  42.         cout << "CommTimeOuts.ReadTotalTimeoutConstant"<< CommTimeOuts.ReadTotalTimeoutConstant << endl;
  43.         cout << "CommTimeOuts.ReadTotalTimeoutMultiplier" << CommTimeOuts.ReadTotalTimeoutMultiplier << endl;
  44.         cout << "CommTimeOuts.WriteTotalTimeoutConstant" << CommTimeOuts.WriteTotalTimeoutConstant << endl;
  45.         cout << "CommTimeOuts.WriteTotalTimeoutMultiplier" << CommTimeOuts.WriteTotalTimeoutMultiplier << endl;
  46.     }else{
  47.         cout << "error get set COMMTIMEOUTS" << endl;
  48.     }
  49.     SetCommTimeouts(uartPort, &CommTimeOuts);
  50.  
  51.     return uartPort;
  52. }
  53.  
  54. DWORD sendData(HANDLE uPort, PCVOID data, unsigned int size){
  55.     DWORD  bytesWriten;
  56.     WriteFile(uPort, data, sizeof(data), &bytesWriten, NULL);
  57.     return bytesWriten;
  58. }
  59.  
  60. DWORD getData(HANDLE uPort, PVOID data, unsigned int size){
  61.     DWORD bytesReading = 1;
  62.     int i;
  63.     for(i = 0; bytesReading && i < size-1; i++){
  64.         cout << ReadFile(uPort, ((PVOID)(((byte*)data)+i)), 1, &bytesReading, 0) << endl;
  65.         cout << i << endl;
  66.     }
  67.     cout << "finish" << endl;
  68.     *(((byte*)data)+i) = '\0';
  69.     return i;
  70. }
  71.  
  72. void closeSerial(HANDLE uPort){
  73.     CloseHandle(uPort);
  74. }
  75.  
RAW Paste Data