#include<stdio.h>
#include <iostream>
#include <windows.h>
#include <atltime.h>
#include"winnt.h"
using namespace std;
#define MAX_PATH 100
DWORD WINAPI Fun1Proc(LPVOID lpParameter);
DWORD WINAPI Fun2Proc(LPVOID lpParameter);
BOOL bReadStat;
BOOL bReadStatus;
HANDLE hCom;
char str[2];
char duf[2];
DWORD wCount;
HANDLE hMutex;
FILE* df;
char lpOutBuffer[1] = { 0xff };
BOOL bWriteStat;
int shi;
char lpInBuffer[2];
DWORD dwBytesRead = 2;
COMSTAT ComStat;
DWORD dwErrorFlags;
OVERLAPPED m_osRead;
DWORD dwBytesWrite = 100;
FILE* df1;
FILE* df2;
FILE* df3;
int rc;
int i = 0;
int fln = 1;
#include<stdio.h>
#include<string.h>
#include<math.h>
void pathConvert_Single2Double(string& s) {
string::size_type pos = 0;
while ((pos = s.find('\\', pos)) != string::npos) {
s.insert(pos, "\\");
pos = pos + 2;
}
}
std::string TCHAR2STRING(TCHAR* str)
{
std::string strstr;
try
{
int iLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
char* chRtn = new char[iLen * sizeof(char)];
WideCharToMultiByte(CP_ACP, 0, str, -1, chRtn, iLen, NULL, NULL);
strstr = chRtn;
}
catch (std::exception e)
{
}
return strstr;
}
int main(int argc, char** argv)
{
DCB dcb;
dcb.DCBlength = sizeof(DCB);
char a[] = "";
char b[] = "";
printf("%s\n", argv[1]);
strcat(a, argv[1]);
WCHAR wsz[64];
swprintf(wsz, L"%S", a);
LPCWSTR m_szFilename = wsz;
hCom = CreateFile(m_szFilename,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
printf("打开COM失败!");
return FALSE;
}
printf("打开串口成功\n");
string lol= argv[2];
pathConvert_Single2Double(lol);
cout << lol << endl;
GetCommState(hCom, &dcb);
SetupComm(hCom, 2, 2);
COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout = 0;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.WriteTotalTimeoutMultiplier = 0;
TimeOuts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hCom, &TimeOuts);
dcb.BaudRate = 115200;
dcb.ByteSize =8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
SetCommState(hCom, &dcb);
GetCommState(hCom, &dcb);
printf("BaudRate is %ld\n", dcb.BaudRate);
PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
memset(&m_osRead, 0, sizeof(OVERLAPPED));
CTime curTime = CTime::GetCurrentTime();
int nYear = curTime.GetYear();
int nMonth = curTime.GetMonth();
int nDay = curTime.GetDay();
int nHour = curTime.GetHour();
int nMin = curTime.GetMinute();
int nSec = curTime.GetSecond();
char buffer[80];
TCHAR szFilePath[MAX_PATH + 1];
GetModuleFileName(NULL, szFilePath, MAX_PATH);
string d = TCHAR2STRING(szFilePath);
string mystr = d.substr(0, d.length() - 24);
const char* wzly = lol.c_str();
df1 = fopen(wzly, "wb");
if (df1 == NULL)
printf("df1\n");
bWriteStat = WriteFile(hCom, lpOutBuffer, 1, &dwBytesWrite, NULL);
if (!bWriteStat)
{
printf("写串口失败!");
}
printf("\n开始");
while (1)
{
bReadStat = ReadFile(hCom, str, 2, &wCount, NULL);
if (!bReadStat)
{
printf("du串口失败!");
}
fwrite(str, 1, 2, df1);
}
}
批处理 多个串口使用上述工具
```bash
@echo off
::start /D "C:\Users\intern\source\repos\ConsoleApplication8\Release" ConsoleApplication8.exe com9 C:\Users\intern\Desktop\12345\12.pcm
start /D "C:\Users\intern\source\repos\ConsoleApplication8\Release" ConsoleApplication8.exe com3 C:\Users\intern\Desktop\12345\13.pcm