xue00010
xue00010
采纳率0%
2015-04-10 08:06 阅读 3.1k

用window API编写串口通讯程序一直报错 error C2065: “LportName”: 未声明的标识符,为什么

程序如下:
#include "stdafx.h"
#include
#include
#include
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char Ipbuffer[]="Hello world!";
HANDLE hComm;
LPCSTR portName = "COM2";
hComm = CreateFile(TEXT(portName), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hComm == INVALID_HANDLE_VALUE)
cout << "Error: Unable to open " << portName << " port." << endl;
else
cout << "Successfully connected to " << portName << " port." << endl;
Sleep(1000);
DCB PortDCB;
PortDCB.DCBlength = sizeof (DCB);
GetCommState (hComm, &PortDCB); // Get the default serial port settings DCB information. .
PortDCB.BaudRate = 230400;
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = FALSE; // Disable parity checking

PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_DISABLE; // DT flow control tRype
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity //
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_DISABLE; // Disable RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes onerror
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 // set serial port settings
if(!SetCommState(hComm,&PortDCB))
{
cout<< "Error: Unable to set comm state." << endl;
}
else
{
cout << "Comm state configured. Baud rate = " << PortDCB.BaudRate << "." << endl;
}
Sleep(1000);
if (!WriteFile(hComm, Ipbuffer, strlen(Ipbuffer), NULL, NULL))

    std::cout<<"serial write errors"<<std::endl;
return 0;

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • xue00010 xue00010 2015-04-10 08:07

    一直报错 error C2065: “LportName”: 未声明的标识符

    点赞 评论 复制链接分享
  • bdmh bdmh 2015-04-10 08:32

    LportName在哪,没有定义啊

    点赞 评论 复制链接分享
  • chen_zhi_heng 忧灵 2015-04-10 09:11

    如果你确定已经include了这个头文件,那么:
    看看是不是预编译的问题。
    #include "StdAfx.h“放到最开头了吗?、要放到include<×.h>这类之前。

    点赞 评论 复制链接分享
  • xue00010 xue00010 2015-04-10 16:32

    不知道,但是VC++ 2008编译的时候这样说,我也不知道它在那里

    点赞 评论 复制链接分享
  • u014403607 frostlihyan 2015-06-10 01:23

    因为使用_T会自动在字符串前加上L,例如_T("abc")相当于L"abc",你可以试试,
    所以当你把字符串数组名放在_T()里面,就相当于在数组名前加了个L了,
    这也是由于Unicode字符集的原因,右击项目,常规,把字符集改成多字符集,或者ANSI字符集,然后把_T去掉,就行了。

    点赞 评论 复制链接分享

相关推荐