我正在通过串行端口与宽带卡通信。我必须通过 COM5 读取 GPS 数据,并通过执行命令并读取 COM3 上的响应来获取信号强度。每当我尝试读取/写入 COM3 时,程序都无法执行。
当我注释掉包含以下内容的行时:
if(!WriteFile(hSerial2, wzBuff, 7, &dZBytesRead, NULL))
std::cout << "Write error";
//
if(!ReadFile(hSerial2, buff2, 30, &DZBYTES, NULL))
std::cout << "Read Error";
程序运行完美。
完整代码如下:
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <time.h>
using namespace std;
vector<int> findLocation(string str, char character);
int main()
{
time_t now;
time(&now);
ofstream myfile;
myfile.open("C:\\log.txt");
HANDLE hSerial = CreateFile("COM5",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
HANDLE hSerial2 = CreateFile("COM3",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(hSerial==INVALID_HANDLE_VALUE)
std::cout << "Cannot find serial port";
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams))
std::cout << "COM State Error";
dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if (!SetCommState(hSerial,&dcbSerialParams))
std::cout << "COM State Error";
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts))
std::cout << "Set timeout errors";
while(1)
{
char szBuff[650+1] = {0};
char buff2[30+1] = {0};
char wzBuff[14] = {"AT+CSQ\r"};
DWORD dZBytesRead = 0;
DWORD dwBytesRead = 0;
DWORD DZBYTES = 0;
//When I uncomment these next two calls my program will neither loop nor start
//if(!WriteFile(hSerial2, wzBuff, 7, &dZBytesRead, NULL))
//std::cout << "Write error";
//
//if(!ReadFile(hSerial2, buff2, 30, &DZBYTES, NULL))
//std::cout << "Read Error";
if(!ReadFile(hSerial, szBuff, 650, &dwBytesRead, NULL))
std::cout << "Read Error";
//
//if(dwBytesRead >1){
string test = string(szBuff);
//std::cout << dwBytesRead << endl;
//std::cout << test;
//
//cout << szBuff[1] << endl << endl;
//myfile << test;
//
size_t found;
size_t secondFound;
//size_t first;
//size_t second;
//size_t third;
//size_t fourth;
//
if(dwBytesRead > 50){
myfile.open("C:\\log.txt",fstream::in | fstream::out | fstream::app);
found = test.rfind("$");
secondFound = test.find("\n");
time_t now;
time(&now);
string timenow = string(ctime(&now));
string final = test.substr(found,((secondFound - found)-1));
vector<int> locations = findLocation(final, ',');
string lat = final.substr((locations[2]+1),12);
string lon = final.substr((locations[4]+1),12);
cout << lat << " " << lon << endl;
myfile << lat << " " << lon << endl;
myfile <<timenow << endl;
//Wanting to get returned value from different COM port
/*if(DZBYTES>20)
{
std::string test2 = std::string(buff2).substr(8,3);
myfile << test2 << endl;
}*/
myfile << "________________________" << endl;
myfile.close();
}
//if (dZBytesRead > 10)
//{
//std::string test2 = std::string(buff2).substr(8,3);
//std::cout << test2 << endl;
//myfile << test2 << endl;
//}
Sleep(400);
}
//myfile.close();
return 0;
}
vector<int> findLocation(string str, char character)
{
vector<int> characterLocations;
for(int i = 0; i < str.size(); i++)
if(str[i] == character)
characterLocations.push_back(i);
return characterLocations;
}
最佳答案
您正在打开两个 COM 端口,但仅为其中一个端口设置串口参数(波特率、奇偶校验)和超时。
另一个留下了未知的设置。您无法成功读取它也就不足为奇了。如果没有超时,它可能会挂起尝试。
关于c++ - 尝试从某个 COM 端口读取/写入时程序无法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7876519/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r