vs c++ 串口接收代码

#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)
{
	//printf("1111\n");
	DCB dcb;
	dcb.DCBlength = sizeof(DCB);
	//GetCommState(hCom, &dcb);
	//memset(&lpDCB, 0, sizeof(LPDCB));

	char a[] = "";
	char b[] = "";
	//printf("请输入\n");
	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");
	//printf("\n输入新建文件的路径\n\n");
	string lol= argv[2];
	//cin >> lol;
	pathConvert_Single2Double(lol);
	cout << lol << endl;
	GetCommState(hCom, &dcb);
	//printf("BaudRate  is  %ld\n", dcb.BaudRate);


	SetupComm(hCom, 2, 2); //输入缓冲区和输出缓冲区的大小都是1024
	COMMTIMEOUTS TimeOuts;
	//设定读超时
	TimeOuts.ReadIntervalTimeout = 0;
	TimeOuts.ReadTotalTimeoutMultiplier = 0;
	TimeOuts.ReadTotalTimeoutConstant = 0;
	//设定写超时
	TimeOuts.WriteTotalTimeoutMultiplier = 0;
	TimeOuts.WriteTotalTimeoutConstant = 0;
	SetCommTimeouts(hCom, &TimeOuts); //设置超时


	//dcb.BaudRate =500000; // m4g
	dcb.BaudRate = 115200;
	dcb.ByteSize =8; //每个字节有8位
	//dcb.fParity = TRUE;
	dcb.Parity = NOPARITY; //无奇偶校验位
	dcb.StopBits = ONESTOPBIT; //1个停止位


	SetCommState(hCom, &dcb);
	GetCommState(hCom, &dcb);
	printf("BaudRate  is  %ld\n", dcb.BaudRate);
	PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
	//printf("BaudRate  is  %ld\n", dcb.BaudRate);



	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);
	//sprintf(buffer, "C:\\Users\\lihailiang\\Desktop\\16K_PCM\\out\\%d-%d-%d-%d-%d-%d.pcm", nYear, nMonth, nDay, nHour, nMin, nSec);
	//df1 = fopen(buffer, "wb");
	//sprintf(buffer, "C:\\Users\\lihailiang\\Desktop\\16K_PCM\\left\\%d-%d-%d-%d-%d-%d.pcm", nYear, nMonth, nDay, nHour, nMin, nSec);
	//df2 = fopen(buffer, "wb");
	//sprintf(buffer, "C:\\Users\\lihailiang\\Desktop\\16K_PCM\\right\\%d-%d-%d-%d-%d-%d.pcm", nYear, nMonth, nDay, nHour, nMin, nSec);
	//df3 = fopen(buffer, "wb");
	//str = str + "\\helo";

	//sprintf(buffer, "\\out\\%d-%d-%d-%d-%d-%d.pcm", nYear, nMonth, nDay, nHour, nMin, nSec);
	//string mystr1 = mystr + buffer;
	//
	//pathConvert_Single2Double(mystr1);
	//cout << mystr1 << endl;
	//char* p0 = (char*)mystr1.c_str();
	//df1 = fopen(p0, "wb");
	//if (df1 == NULL)
	//	printf("df1\n");
	//sprintf(buffer, "\\left\\%d-%d-%d-%d-%d-%d.pcm", nYear, nMonth, nDay, nHour, nMin, nSec);
	//string mystr2 = mystr + buffer;
	//pathConvert_Single2Double(mystr2);
	//char* p1 = (char*)mystr2.c_str();
	//
	//cout << mystr2 << endl;
	//df2 = fopen(p1, "wb");
	//if (df2 == NULL)
	//	printf("df2\n");
	//sprintf(buffer, "\\right\\%d-%d-%d-%d-%d-%d.pcm", nYear, nMonth, nDay, nHour, nMin, nSec);
	//string mystr3 = mystr + buffer;
	//pathConvert_Single2Double(mystr3);
	//cout << mystr3 << endl;
	//char* p2 = (char*)mystr3.c_str();
	//df3 = fopen(p2, "wb");
	//if (df3 == NULL)
	//	printf("df3\n");

	const char* wzly = lol.c_str();
	df1 = fopen(wzly, "wb");
	//printf("%s\n",wzly);
	if (df1 == NULL)
		printf("df1\n");
	//df2 = fopen("C:\\Users\\intern\\Desktop\\16K_PCM\\left\\1.pcm", "wb");
	//df3 = fopen("C:\\Users\\intern\\Desktop\\16K_PCM\\right\\1.pcm", "wb");
	bWriteStat = WriteFile(hCom, lpOutBuffer, 1, &dwBytesWrite, NULL);
	if (!bWriteStat)
	{
		printf("写串口失败!");
	}
	printf("\n开始");
	while (1)
	//for(int sos=1;sos<100;sos++)
	{
		//printf("1111");
		bReadStat = ReadFile(hCom, str, 2, &wCount, NULL);
		if (!bReadStat)
		{
			printf("du串口失败!");
		}
		
		

		fwrite(str, 1, 2, df1);
		//memset(str,0,2);

	}


}


批处理   多个串口使用上述工具

```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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值