gpt4 book ai didi

c++ - 在 vc++ 中进行串行通信需要哪些函数?

转载 作者:行者123 更新时间:2023-11-30 04:34:21 25 4
gpt4 key购买 nike

我正在使用嵌入式设备。我使用 COM 端口连接到它。当我向它发送命令“LIST”时,它会给出所有文件的列表。

所以我写了一个“hello world”,它将连接到端口设备已连接并发送数据。

当我连接我的设备并运行我的程序时,它正在写入端口并且没有从该端口接收任何字节。

但是当我使用 PUTTY(用于打开端口并发送一些数据)打开 COM 端口并发送 COMMAND 时它工作,当我关闭 PUTTY 并现在运行我的程序时它工作正常,所以我需要打开第一次用腻子移植我的程序。

可能是我没有初始化一些函数...:(

任何人都可以帮我解决这个问题,我无法找到过去一天的解决方案。在此先感谢...

我的源代码是:-

#include "stdafx.h"
#include <iostream>
#include <afx.h>

int main()
{
using namespace std;
int i=0;
// cout << "Hello world!" << endl;



HANDLE hSerial;

hSerial = CreateFile("COM5",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);

if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
// TRACE("serial port does not exist for reading\n");
//serial port does not exist. Inform user.
}
// TRACE("some other error,serial port does not exist for reading\n");
//some other error occurred. Inform user.
}

DCB dcbSerialParams = {0};

dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

if (!GetCommState(hSerial, &dcbSerialParams))
{
// TRACE("error getting state for reading\n");
//error getting state
}

dcbSerialParams.BaudRate=9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
dcbSerialParams.fOutX=TRUE;
dcbSerialParams.fInX=TRUE;
if(!SetCommState(hSerial, &dcbSerialParams))
{
//TRACE("error setting state for reading\n");
//error setting serial port state
}
COMMTIMEOUTS timeouts={0};

timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;

timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;

if(!SetCommTimeouts(hSerial, &timeouts))
{
// TRACE("some error occured for reading\n");
//error occureed. Inform user
}
int n=100,n1=100;
char szBuff[100];
DWORD dwBytesRead = 0;
char szBuff1[100];
DWORD dwByteswrote = 0;
memset(szBuff1,0,100);
memcpy(szBuff1,"LIST\r",5);
FlushFileBuffers(hSerial);
LPDWORD uf=0;
GetCommModemStatus(hSerial,uf);
TRACE("%d\n",uf);
if(!WriteFile(hSerial, szBuff1,5, &dwByteswrote, NULL))
{
cout << "error writing" ;
}
cout << szBuff1 << endl;
cout << dwByteswrote << endl;
dwByteswrote=0;
while(1)
{
if(!ReadFile(hSerial, szBuff, n1, &dwBytesRead, NULL))
{
cout << "error reading";
break;
}
else
{
cout << dwBytesRead << endl;
szBuff[dwBytesRead]='\0';
if(dwBytesRead>0)
{
cout << (szBuff);

break;
}
else
{
}
}
}
cin >> i;
}

最佳答案

您可以尝试设置所有 DCB 选项并清除端口错误

// Common settings
dcbSerialParams.DCBlength = sizeof( dcbSerialParams );
dcbSerialParams.ByteSize = 8;
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.fParity = FALSE;
dcbSerialParams.Parity = NOPARITY;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.fDtrControl = 0;
dcbSerialParams.fRtsControl = 0;

// If you say so
dcbSerialParams.fOutX = TRUE;
dcbSerialParams.fInX = TRUE;

// Not so common settings
dcbSerialParams.fBinary = FALSE;
dcbSerialParams.fParity = FALSE;
dcbSerialParams.fOutxCtsFlow = FALSE;
dcbSerialParams.fOutxDsrFlow = FALSE;
dcbSerialParams.fDsrSensitivity = FALSE;
dcbSerialParams.fErrorChar = FALSE;
dcbSerialParams.fNull = FALSE;
dcbSerialParams.fAbortOnError = FALSE;

// Clear errors
unsigned long ulCommErr = 0;
ClearCommBreak( hSerial );
ClearCommError( hSerial, &ulCommErr, NULL );

可能不相关,但我注意到我在某个时候在我的串行代码中向 CreateFile() 添加了一个安全描述符,我相信 Windows Server 的某些配置需要它。

// Allow access
SECURITY_ATTRIBUTES sa, *pSa = NULL;
sa.nLength = sizeof( SECURITY_ATTRIBUTES );
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = (SECURITY_DESCRIPTOR*)LocalAlloc(LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH);
if ( sa.lpSecurityDescriptor
&& InitializeSecurityDescriptor( (SECURITY_DESCRIPTOR*)sa.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION )
&& SetSecurityDescriptorDacl( (SECURITY_DESCRIPTOR*)sa.lpSecurityDescriptor, TRUE, (PACL)NULL, FALSE ) )
pSa = &sa;

// Open the port
hSerial = CreateFile( x_pPort,
GENERIC_READ | GENERIC_WRITE,
0,
pSa,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );

if ( pSa )
LocalFree( pSa->lpSecurityDescriptor );

关于c++ - 在 vc++ 中进行串行通信需要哪些函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6068655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com