xue00010 2015-04-10 08:06 采纳率: 0%
浏览 3116

用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 2015-04-10 08:07
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘