I am trying to create a program in c++ on xcode to communicate to the gsm modem and send sms through AT comands. I haven' t found much on the net. Here is the code that I have written till now.
#include <iostream>
#include <xlnt/xlnt.hpp>
#include <xlnt/xlnt_config.hpp>
#include <IOKitLib.h>
#include <IOTypes.h>
#include <IOReturn.h>
#include <IOKitKeys.h>
#include <iokitmig.h>
#include <OSMessageNotification.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <paths.h>
#include <termios.h>
#include <sysexits.h>
#include <sys/param.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/IOBSD.h>
#define LOCAL_ECHO
#ifdef LOCAL_ECHO
#define kOKResponseString “AT\r\r\nOK\r\n”
#else
#define kOKResponseString “\r\nOK\r\n”
#define kATCommandString "AT\r"
#define kMyErrReturn -1
enum {
kNumRetries = 3
};
static struct termios gOriginalTTYAttrs;
int main()
{
char path;
char text;
int fileDescriptor;
kern_return_t kernResult;
io_iterator_t serialPortIterator;
char deviceFilePath[MAXPATHLEN];
kernResult = MyFindModems(&serialPortIterator);
kernResult = MyGetModemPath(serialPortIterator, deviceFilePath,
sizeof(deviceFilePath));
IOObjectRelease(serialPortIterator); // Release the iterator.
// Open the modem port, initialize the modem, then close it.
if (!deviceFilePath[0])
{
printf("No modem port found.\n");
return EX_UNAVAILABLE;
}
fileDescriptor = OpenSerialPort(deviceFilePath);
if (fileDescriptor == kMyErrReturn)
{
return EX_IOERR;
}
if (MyInitializeModem(fileDescriptor))
{
printf("Modem initialized successfully.\n");
}
else {
printf("Could not initialize modem.\n");
}
MyCloseSerialPort(fileDescriptor);
printf("Modem port closed.\n");
return EX_OK;
It's giving me an error "use of undeclared identifier MyFindModems" and all the other fuctions that start with "My".
Here is the documentation to which I refer: http://mirror.informatimago.com/next/developer.apple.com/documentation/DeviceDrivers/Conceptual/WorkingWSerial/WorkingWithSerial.pdf
Related
Hi I'm using visual studio 2017 and I received that error when I compiled live555 sub-project groupsock, "fatal error C1083: Cannot open include file: 'ifaddrs.h': No such file or directory.I don't know how to deal with the problem.How do solve this problem?thanks!
GroupsockHelper.cpp header content
#include "GroupsockHelper.hh"
#if (defined(__WIN32__) || defined(_WIN32)) && !defined(__MINGW32__)
#include <time.h>
extern "C" int initializeWinsockIfNecessary();
#else
#include <stdarg.h>
#include <time.h>
#include <sys/time.h>
#if !defined(_WIN32)
#include <netinet/tcp.h>
#ifdef __ANDROID_NDK__
#include <android/ndk-version.h>
#define ANDROID_OLD_NDK __NDK_MAJOR__ < 17
#endif
#endif
#include <fcntl.h>
#define initializeWinsockIfNecessary() 1
#endif
#if defined(__WIN32__) || defined(_WIN32) || defined(_QNX4)
#else
#include <signal.h>
#define USE_SIGNALS 1
#endif
#ifndef NO_GETIFADDRS
#include <ifaddrs.h>
#include <net/if.h>
#endif
#include <stdio.h>
// By default, use INADDR_ANY for the sending and receiving interfaces (IPv4 only):
ipv4AddressBits SendingInterfaceAddr = INADDR_ANY;
ipv4AddressBits ReceivingInterfaceAddr = INADDR_ANY;
static void socketErr(UsageEnvironment& env, char const* errorMsg) {
env.setResultErrMsg(errorMsg);
}
NoReuse::NoReuse(UsageEnvironment& env)
: fEnv(env) {
`enter code here`groupsockPriv(fEnv)->reuseFlag = 0;
}
NoReuse::~NoReuse() {
groupsockPriv(fEnv)->reuseFlag = 1;
reclaimGroupsockPriv(fEnv);
}
.......
I need to implement a distributed database and I implemented based on gRPC example. The following is my code snippet.
sundial_sync_server.h
#ifndef SUNDIAL_GRPC_SYNC_SERVER_H
#define SUNDIAL_GRPC_SYNC_SERVER_H
#endif //SUNDIAL_GRPC_SYNC_SERVER_H
#include "sundial_grpc.grpc.pb.h"
#include "sundial_grpc.pb.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
using grpc::Channel;
using grpc::ServerContext;
using grpc::Status;
using sundial_rpc::SundialRequest;
using sundial_rpc::SundialResponse;
using sundial_rpc::Sundial_GRPC_SYNC;
#ifndef ABC
#define ABC
class SundialServiceImp final : public Sundial_GRPC_SYNC::Service
{
public:
Status contactRemote(::grpc::ServerContext* context, const ::sundial_rpc::SundialRequest* request, ::sundial_rpc::SundialResponse* response) override;
void run();
};
#endif
sundial_sync_server.cpp
#include "sundial_grpc.grpc.pb.h"
#include "sundial_grpc.pb.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "txn.h"
#include "global.h"
#include "manager.h"
#include "stats.h"
#include "helper.h"
#include "grpc_sync_server.h"
#include "txn_table.h"
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
using grpc::ServerContext;
using grpc::Status;
using sundial_rpc::SundialRequest;
using sundial_rpc::SundialResponse;
using sundial_rpc::Sundial_GRPC_SYNC;
using grpc::Server;
using grpc::ServerBuilder;
Status SundialServiceImp::contactRemote(::grpc::ServerContext* context, const ::sundial_rpc::SundialRequest* request, ::sundial_rpc::SundialResponse* response){
if (request->request_type() == SundialRequest::SYS_REQ) {
glob_manager->receive_sync_request();
response->set_response_type( SundialResponse::SYS_RESP );
return Status::OK;
}
uint64_t txn_id = request->txn_id();
TxnManager * txn_man = txn_table->get_txn(txn_id);
// If no TxnManager exists for the requesting transaction, create one.
if (txn_man == NULL) {
//printf("adding txnID=%ld into txn_table\n", txn_id);
assert( request->request_type() == SundialRequest::READ_REQ );
txn_man = new TxnManager();
txn_man->set_txn_id( txn_id );
txn_table->add_txn( txn_man );
}
// the transaction handles the RPC call
txn_man->process_remote_request(request, response);
// if the sub-transaction is no longer required, remove from txn_table
if (response->response_type() == SundialResponse::RESP_ABORT
|| response->response_type() == SundialResponse::PREPARED_OK_RO
|| response->response_type() == SundialResponse::PREPARED_ABORT
|| response->response_type() == SundialResponse::ACK) {
txn_table->remove_txn( txn_man );
delete txn_man;
}
return Status::OK;
}
void SundialServiceImp::run(){
std::istringstream in(ifconfig_string);
string line;
uint32_t num_nodes = 0;
string port;
while (getline (in, line)) {
if (line[0] == '#')
continue;
else {
if (num_nodes == g_node_id) {
port = line.substr(0, line.length());
break;
}
num_nodes ++;
}
}
port.append(sync_port);
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
ServerBuilder builder;
builder.AddListeningPort(port, grpc::InsecureServerCredentials());
builder.RegisterService(this);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << port << std::endl;
server->Wait();
}
`sundial_sync_client.h'
#endif //SUNDIAL_GRPC_CLIENT_H
#include "sundial_grpc.grpc.pb.h"
#include "sundial_grpc.pb.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using sundial_rpc::SundialRequest;
using sundial_rpc::SundialResponse;
using sundial_rpc::Sundial_GRPC_SYNC;
//toDo: now assume we only have 2 nodes
#ifndef SSC
#define SSC
class Sundial_Sync_Client{
public:
Sundial_Sync_Client(std::shared_ptr<Channel>* channel);
Status contactRemote(uint64_t node_id,SundialRequest& request, SundialResponse* response);
private:
std::unique_ptr<Sundial_GRPC_SYNC::Stub> stub_;
};
#endif
sundial_sync_client.cpp
#include "sundial_grpc.grpc.pb.h"
#include "sundial_grpc.pb.h"
#include "grpc_sync_client.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "txn.h"
#include "global.h"
#include "helper.h"
#include "manager.h"
#include "stats.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using sundial_rpc::SundialRequest;
using sundial_rpc::SundialResponse;
using sundial_rpc::Sundial_GRPC_SYNC;
//toDo: add more nodes to it
Sundial_Sync_Client::Sundial_Sync_Client(std::shared_ptr<Channel>* channel){
for(int i=0; i<g_num_nodes;i++){
if(i==g_node_id)
continue;
//stub_[i]=Sundial_GRPC_SYNC::NewStub(channel[i]);
stub_=Sundial_GRPC_SYNC::NewStub(channel[i]);
}
}
Status
Sundial_Sync_Client::contactRemote(uint64_t node_id, SundialRequest& request, SundialResponse* response){
//toDo: choose the right stub with node id
ClientContext context;
printf("Client sends request\n");
//Status status = stub_[node_id]->contactRemote(&context, request, &response);
Status status = stub_->contactRemote(&context, request, response);
if (status.ok()) {
//printf("status ok\n");
glob_stats->_stats[GET_THD_ID]->_resp_msg_count[ response->response_type() ] ++;
glob_stats->_stats[GET_THD_ID]->_resp_msg_size[ response->response_type() ] += response->SpaceUsedLong();
return status;
} else {
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return status;
}
}
It runs pretty smoothly at the setup. I did get the ip address and let servers run at the right address. However, when I make the client send request, the status it returns always has error code 12 without error message.
Error code 12 is "UNIMPLEMENTED". Check your proto and make sure your server is implementing the correct RPC method (correct upper/lower case for method name and things like that)
I am using Visual Studio 2010, with OpenCV 3.0. I'm trying to load some images from a folder but I am having problems.
Firstly I did not have the file dirent.h, so I downloaded it in order to get the DIR* and "dirent*" structures to access to the files. All seems to be well, but now when I get to the line
string fileName = in_file->d_name;
I have found that I don't access to the name of the file.
Anyone have any thoughts on this?
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <opencv2/core/dirent.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <conio.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")
#include <errno.h>
#include <iostream>
#include <time.h>
#include <limits.h>
#include <fstream>
#include <sys/stat.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::ml;
using namespace std;
int patchWidth = 15;
int patchHeight = 15;
int main(int, char**)
{
string imagesPath = "Images";
string resultsPath = "Patches";
DIR* FD;
struct dirent* in_file;
if (NULL == (FD = opendir (imagesPath.c_str())))
{
fprintf(stderr, "Error : Failed to open input directory\n");
return 0;
}
while ((in_file = readdir(FD)))
{
/* On linux/Unix we don't want current and parent directories
* If you're on Windows machine remove this two lines
*/
// if (!strcmp (in_file->d_name, "."))
// continue;
// if (!strcmp (in_file->d_name, ".."))
// continue;
/* Open directory entry file for common operation */
/* TODO : change permissions to meet your need! */
string fileName = in_file->d_name;
string pathFile = imagesPath;
pathFile.append("/");
pathFile.append(fileName);
//pathFile.append(".jpg");
Mat img = imread(pathFile.c_str());
Thanks in advance.
for a much simpler solution, just use cv::glob :
String imagesPath = "Images/*.png"; // it has filters, too !
vector<String> fn;
glob(path, fn, true); // recursive, if you want
for (size_t i=0; i<fn.size(); i++)
{
Mat img = imread(fn[i]);
...
}
I have a c++ program to get the local machine name using gethostbyaddr() method, the requirement is to run the program with argument which is an ip address.
#include <iostream>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <cstdlib>
using namespace std;
int main(int argc, char const *argv[]) {
struct hostent * testing_case;
struct in_addr ipv4addr;
if (argc != 2) {
cout<<"Error"<<endl;
return 0;
}
else{
inet_aton(argv[1],&ipv4addr);
testing_case = gethostbyaddr(&ipv4addr, sizeof(ipv4addr), AF_INET);
cout<<"Host Name is: "<<testing_case->h_name<<endl;
}
return 0;
}
But when I input the local ip address it returns me a segmentation fault, please tell me how to fix it?
I'm trying to compile my code in Eclipse
But it wouldnt compile my pipe use.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <signal.h>
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <sys/wait.h>
using namespace std;
int OpenPipeRead(string sinterface)
{
int ret_val,errno;
string lpipename="",lpipepath="";
lpipepath = mconfig[C_PIPEPATH];
lpipename.append(lpipepath); //Its not empty only when there is argument for parallal telepath_sniff instances.
lpipename.append(mconfig[C_PIPENAME]);
if(strcmp(sinterface.c_str(), "") != 0)
lpipename.append("_" + sinterface);
printf("Trying to open Pipe for reading\n");
syslog(LOG_INFO, "Try to open Pipe for reading\n");
/* Create the named - pipe */
ret_val = mkfifo(lpipename.c_str(), 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe");
syslog(LOG_ERR, "Error creating the named pipe");
exit(1);
}
if((pipehandler = open(lpipename.c_str(), O_RDWR)) < 1) /* Open the pipe for reading and writing , in append mode */
{
perror("Failed to open pipe file");
syslog(LOG_ERR, "Failed to open pipe file");
exit(1);
}
printf("Pipe opened.\n");
syslog(LOG_INFO, "Pipe opened.\n");
}
int main(){
OpenPipeRead("arg");
}
errors are :
../src/main.cpp:325: error: ‘EEXIST’ was not declared in this scope
../src/main.cpp:330: error: ‘O_RDWR’ was not declared in this scope
../src/main.cpp:330: error: ‘open’ was not declared in this scope
It does compile outside of Eclipse
Any includes or flags I need to compile with on Eclipse?
Thanks
The EEXIST MACRO is defined in asm-generic/errno-base.h and the O_RDWR Flag is defined in fcntl.h.
Add :
#include <asm-generic/errno-base.h>
#include <fcntl.h>
To the file containing OpenPipeRead definition and it should compile.