"无法打开端口!"在DLL项目中

时间:2015-03-23 22:11:59

标签: c dll serial-port port

我在Visual Studio中创建了一个C控制台应用程序,以便使用以下代码访问其中一个COM端口,一切顺利。

#include <windows.h>
#include <stdio.h>
#include <conio.h>


/*DWORD dwBytesWrite = 25;
WriteFile(hSerial, "LOOOOOL", n, &dwBytesWrite, NULL);*/
/*
int main(void)
{

int n = 25;
char szBuff[25 + 1] = { 0 };

HANDLE hSerial;
DCB dcbSerialParams = { 0 };
COMMTIMEOUTS timeouts = { 0 };
DWORD dwBytesRead = 25;

dcbSerialParams.DCBlength = sizeof(DCB);

hSerial = CreateFile("COM6",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);


if (hSerial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
puts("cannot open port!");
return;
}

puts("invalid handle value!");
return;
}

if (!GetCommState(hSerial, &dcbSerialParams))
{
puts("error getting state");
return;
}

dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;

if (!SetCommState(hSerial, &dcbSerialParams))
{
puts("error setting port state");
return;
}

timeouts.ReadIntervalTimeout = 30;
timeouts.ReadTotalTimeoutMultiplier = 100;
timeouts.ReadTotalTimeoutConstant = 100;

if (!SetCommTimeouts(hSerial, &timeouts))
{
puts("timeouts setting fail!");
}

while (1){
if (!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL)){
puts("serial read error fail!");
return;
}

else
{
printf("%s\n", szBuff);
}
}


getchar();
return 0;

}

现在我需要创建一个DLL项目,它给了我以下错误:&#34;无法打开端口!&#34;

知道为什么吗? 感谢

0 个答案:

没有答案
相关问题