c++ 串口开发 发送/接受数据demo


1、demo带串口发送指令,主要是电子秤开发 2、发送指令后,同时接受数据到变量里 3、同时也封装好串口通信的类 好的话 给个赞吧,一直找不到C++ 的发送同时接受的demo,最后自己写了
资源截图
代码片段和文件信息
// ceshi.cpp: 定义控制台应用程序的入口点。
//

#include “stdafx.h“
#include “SerialPort.h“
#include 

#define STX 0x0A
#define ETX 0x0D

int main()
{
CSerialPort mySerialPort;

/*UINT  portNo = 1UINT  baud = CBR_9600char  parity = ‘N‘UINT  databits = 8
UINT  stopsbits = 1DWORD dwCommEvents = EV_RXCHAR*/

if (!mySerialPort.InitPort(1 9600 ‘N‘ 8 1 EV_RXCHAR))
{
std::cout << “initPort fail !“ << std::endl;
}
else
{
std::cout << “initPort success !“ << std::endl;
}
/*
if (!mySerialPort.OpenListenThread())
{
std::cout << “OpenListenThread fail !“ << std::endl;
}
else
{
std::cout << “OpenListenThread success !“ << std::endl;
}
*/
unsigned char data[255] = { 0x52 };
unsigned char *pData = data;
unsigned int length = 1;

    mySerialPort.WriteData(pData length);


char cRecved = 0x00;
char outbuf[MAX_BUFFER_SIZE];
memset(outbuf ‘‘ sizeof(outbuf));
int i = 0;

Sleep(1000);  //延时1秒

UINT BytesInQue = mySerialPort.GetBytesInCOM();

//std::cout << BytesInQue;

if (BytesInQue > 0)
{
do
{
cRecved = 0x00;
if (mySerialPort.ReadChar(cRecved) == true)
{
if ((cRecved == 0x30) || (cRecved == 0x31) ||
(cRecved == 0x32) || (cRecved == 0x33) ||
(cRecved == 0x34) || (cRecved == 0x35) ||
(cRecved == 0x36) || (cRecved == 0x37) ||
(cRecved == 0x38) || (cRecved == 0x39) ||
(cRecved == 0x2E))
{
outbuf[i++] = cRecved;
//printf(outbuf);
}
}
} while (--BytesInQue);
}
std::cout << outbuf;
system(“pause“);
//int temp;
//std::cin >> temp;

return 0;
}


 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      59392  2019-04-09 10:59  Debugceshi.exe

     文件     459256  2019-04-09 10:59  Debugceshi.ilk

     文件     585728  2019-04-09 10:59  Debugceshi.pdb

     文件      17408  2019-04-09 18:21  x64Releaseceshi.exe

     文件      98406  2019-04-09 18:21  x64Releaseceshi.iobj

     文件      36720  2019-04-09 18:21  x64Releaseceshi.ipdb

     文件     741376  2019-04-09 18:21  x64Releaseceshi.pdb

     文件       1429  2019-04-09 10:56  ceshi.sln

     文件       3288  2019-04-09 18:20  ceshiceshi.cpp

     文件       8293  2019-04-09 10:58  ceshiceshi.vcxproj

     文件       1436  2019-04-09 10:58  ceshiceshi.vcxproj.filters

     文件        165  2019-04-09 10:56  ceshiceshi.vcxproj.user

     文件       1043  2019-04-09 10:59  ceshiDebugceshi.Build.CppClean.log

     文件        149  2019-04-09 10:59  ceshiDebugceshi.log

     文件      65010  2019-04-09 10:59  ceshiDebugceshi.obj

     文件    3014656  2019-04-09 10:59  ceshiDebugceshi.pch

     文件        215  2019-04-09 10:59  ceshiDebugceshi.tlogceshi.lastbuildstate

     文件       2462  2019-04-09 10:59  ceshiDebugceshi.tlogCL.command.1.tlog

     文件      77422  2019-04-09 10:59  ceshiDebugceshi.tlogCL.read.1.tlog

     文件       1872  2019-04-09 10:59  ceshiDebugceshi.tlogCL.write.1.tlog

     文件       1516  2019-04-09 10:59  ceshiDebugceshi.tloglink.command.1.tlog

     文件       4428  2019-04-09 10:59  ceshiDebugceshi.tloglink.read.1.tlog

     文件        732  2019-04-09 10:59  ceshiDebugceshi.tloglink.write.1.tlog

     文件      79618  2019-04-09 10:59  ceshiDebugSerialPort.obj

     文件       6134  2019-04-09 10:59  ceshiDebugstdafx.obj

     文件     609280  2019-04-09 10:59  ceshiDebugvc141.idb

     文件     471040  2019-04-09 10:59  ceshiDebugvc141.pdb

     文件       7639  2019-04-09 18:20  ceshiSerialPort.cpp

     文件       3412  2019-04-09 18:20  ceshiSerialPort.h

     文件        312  2019-04-09 11:31  ceshistdafx.cpp

............此处省略29个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)