简体   繁体   English

C ++ - 控制串口 - 代码无法编译

[英]C++ - controlling a serial port - code won't compile

I am trying to get an LED connected to a serial port to light up. 我正在尝试将LED连接到串行端口以点亮。 I have followed a step-by-step tutorial which can be found here . 我按照一步一步的教程,可以在这里找到。 I have followed the directions exactly but cannot get the code to compile. 我已完全按照说明进行操作但无法获取编译代码。 From the PDF guide, it is part 2 which does not compile, which I reproduce here: 从PDF指南,它是第2部分,不编译,我在这里重现:

//<Set serial port parameters>

  DCB dcbSerialParams = {0};
  dcbSerial.DCBlength=sizeof(dcbSerialParams);
  if (!GetCommState(hSerial, &dcbSerialParams))
  {
  //error getting state
  }
  dcbSerialParams.BaudRate=CBR_19200;
  dcbSerialParams.ByteSize=8;
  dcbSerialParams.StopBits=ONESTOPBIT;
  dcbSerialParams.Parity=NOPARITY;
  if(!SetCommState(hSerial, &dcbSerialParams))
  {
  //error setting serial port state
  }

//</Set serial port parameters>

I have included in the code as specified, but I receive the error message "`dcbSerial' undeclared (first use this function)". 我已经包含在指定的代码中,但是我收到错误消息“`dcbSerial'unclaclared(首先使用此函数)”。 I am using Dev-C++. 我正在使用Dev-C ++。

Essentially all I am trying to do is get a command which flashes the LED on and off, which I can put into another program I have (an eye tracking device - the idea is to get the LED to flash when your eyes are looking at it). 基本上我所要做的就是获得一个闪烁LED指示灯的命令,我可以将其放入另一个程序中(一个眼动追踪设备 - 想法是当你的眼睛看着它时让LED闪烁)。

Any help very much appreciated. 任何帮助非常感谢。

dcbSerial.DCBlength=sizeof(dcbSerialParams);

should be: 应该:

dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

Really, you are going to have to put a bit more effort into reading and understanding compiler error messages - that one was about as clear as it gets. 实际上,您将不得不花费更多的精力来阅读和理解编译器错误消息 - 这个消息就像它得到的那样清晰。

dcbSerial替换dcbSerialParams

You have declared a variable with name dcbSerialParams , but refer to it as dcbSerial 您已声明名为dcbSerialParams的变量,但将其称为dcbSerial

DCB dcbSerialParams = {0};
dcbSerial.DCBlength=sizeof(dcbSerialParams);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM