Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial port access in C #106

Open
simo-morra opened this issue Feb 6, 2018 · 1 comment
Open

Serial port access in C #106

simo-morra opened this issue Feb 6, 2018 · 1 comment

Comments

@simo-morra
Copy link

simo-morra commented Feb 6, 2018

Hi,
I'm implementing a HAL module and need to communicate with an arduino connected to the raspberry over usb. To test the communication, I wrote a simple C program (using termios.h and open/read/write for serial access) which sends a string to arduino. Arduino reads the message and sends it back. It all works well: I tested it with the arduino serial monitor and from a Linux pc.
So I compiled it for the raspberry (getting no errors or warnings) and installed it through adb push. This is the code to open the serial port:

int openPort() {
	int fd;
	struct termios options;

	fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_NDELAY);
	if(fd != -1) {
		fcntl(fd, F_SETFL, 0);

		//serial port settings
		tcgetattr(fd, &options);
		cfsetispeed(&options, B9600); //input baud rate 
		cfsetospeed(&options, B9600); //output baud rate
		options.c_cflag |= (CLOCAL | CREAD);
		//set data format (8N1)
		options.c_cflag &= ~PARENB;
		options.c_cflag &= ~CSTOPB;
		options.c_cflag &= ~CSIZE;
		options.c_cflag |= CS8;
		options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //raw input
		options.c_oflag &= ~OPOST; //raw output

		//confirm settings
		tcsetattr(fd, TCSAFLUSH, &options);
	}

	return fd;
}

I tried using all of the port names available under /dev/tty* but none of them corresponds to arduino. So it's like android doesn't recognize that arduino is connected, even if it does turn on when I connect the cable.

Any ideas about what could be the problem (maybe some driver/kernel stuff?) and how to solve it?

@simo-morra
Copy link
Author

I discovered that the raspberry actually recognizes the connected arduino, but as if it was an usb device (under /dev/bus/usb). So I tried to handle the communication via libusb, but it doesn't seem to work, because the arduino is indeed a serial device.

I think the problem is with some missing drivers that should recognize that arduino is a serial device, but I don't know what.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant