Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PSVR2_RS485.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <iostream>
- #include <fstream>
- using std::ifstream;
- using std::ofstream;
- using std::ios;
- HANDLE createPortHandle(const char* port, int baudrate)
- {
- HANDLE portHandle = CreateFile(port, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (portHandle == INVALID_HANDLE_VALUE)
- {
- printf("Cannot open port!\n");
- return INVALID_HANDLE_VALUE;
- }
- DCB params = { 0 };
- params.DCBlength = sizeof(params);
- GetCommState(portHandle, ¶ms);
- params.BaudRate = baudrate;
- params.ByteSize = 8;
- params.StopBits = 0;
- params.Parity = 0;
- SetCommState(portHandle, ¶ms);
- COMMTIMEOUTS timeouts = { 0 };
- timeouts.ReadIntervalTimeout = 10;
- timeouts.ReadTotalTimeoutConstant = 1;
- timeouts.ReadTotalTimeoutMultiplier = 1;
- timeouts.WriteTotalTimeoutConstant = 50;
- timeouts.WriteTotalTimeoutMultiplier = 50;
- SetCommTimeouts(portHandle, &timeouts);
- return portHandle;
- }
- static bool outterminate = false;
- DWORD _stdcall readFunction(void* params)
- {
- HANDLE portHandle = *(HANDLE*)params;
- char input[2048];
- DWORD bytesRead;
- while (!outterminate)
- {
- ReadFile(portHandle, input, 256, &bytesRead, 0);
- if (bytesRead > 0)
- {
- /*printf("received message: %s\n", output);*/
- ofstream outputBuffer("C:/Users/Administrator/Documents/text.txt", ios::out | ios::binary);
- }
- Sleep(100);
- }
- return 0;
- }
- int main()
- {
- printf("enter port: ");
- char portName[100];
- scanf("%s", portName);
- HANDLE portHandle = createPortHandle(portName, 115200);
- if (portHandle == INVALID_HANDLE_VALUE) {
- return -1;
- }
- DWORD threadId;
- HANDLE threatHandle = CreateThread(NULL, 0, readFunction, &portHandle, 0, &threadId);
- ifstream fileBuffer("C:/Users/DELL/Documents/text.txt", ios::in | ios::binary);
- ofstream outputBuffer("C:/Users/Administrator/Documents/text.txt", ios::out | ios::binary);
- char input[2048];
- char output[2048];
- if (fileBuffer.is_open())
- {
- fileBuffer.seekg(0, ios::beg);
- fileBuffer.getline(input, 2048);
- }
- outputBuffer.write(input, sizeof(input));
- char message[256];
- do
- {
- /* fgets(message, 256, stdin);
- message[strlen(message) - 1] = '\0'; */
- /* if (strlen(message) > 0)
- {
- DWORD bytesSent;
- WriteFile(portHandle, message, strlen(message) + 1, &bytesSent, 0);
- printf("Number of bytes sent: %ld\n", bytesSent);
- }
- */
- if (strlen(input) > 0)
- {
- DWORD bytesSent;
- WriteFile(portHandle, input, strlen(input) + 1, &bytesSent, 0);
- printf("Number of bytes sent: %ld\n", bytesSent);
- }
- }
- while (strcmp(input, "x") != 0);
- outputBuffer.close();
- fileBuffer.close();
- outterminate = true;
- WaitForSingleObject(threatHandle, INFINITE);
- CloseHandle(threatHandle);
- CloseHandle(portHandle);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement