Go Down

Topic: How to read from serial in c ? (under linux) (Read 1 time) previous topic - next topic


Does this work in kernel mode too? I'm writing a kernel module which has to interact with arduino board..

You don't know how to access a serial interface in Linux but you wanna write a kernel module?

Start with a smaller project, build a simple kernel module. Extend it to read files. Extend it to access simple character devices (like /dev/random). If that all works try accessing a complex tty device like the serial interface.
You cannot use functionality of the GNU C library withing the kernel (fopen, fclose and the like), don't use any library at all, use the kernel calls directly. This is really different from userland programming.


it gives me "implicit declaration"!

I'm nearly certain there was more to the message than that.


Thanks guys I think I figured out the way! :)

Now I have a bigger problem anyway...in my kernel module the open() function is not recognized, it gives me "implicit declaration"!
My include part is:

Code: [Select]
#include <linux/module.h>
#include <linux/unistd.h>
#include <asm/io.h>
#include <asm/fcntl.h>
#define SERIAL_PORT "/dev/ttyACM0"

while the error is in this line:
Code: [Select]

void myfun(void){
  int fd = open(SERIAL_PORT,O_RDONLY | O_NOCTTY);

can you help me to solve this?


Oct 29, 2012, 01:25 pm Last Edit: Oct 29, 2012, 01:36 pm by pekkaa Reason: 1
If you are sending integers only, it may be easiest to send them as binary data,  though there are still some quirks you have to take care of:
- Check if the Arduino and your Linux computer share the same byte order (little endian vs. big endian). If not, you need to convert to/from the network byte order when sending/receiving the integers. (Check htons/ntohs man pages on Linux).
- "int" is 16 bits on the Arduino and probably 32 bits on your Linux computer.  Use "short" on Linux instead.


how to know if I'm reading the same integer or a new one?

You wrote the program that is sending them, so send them in a way that lets your receiving code detect the boundary between numbers. For example, if you're sending the numbers as an ASCII sequence of decimal digits, you could use any non-digit character(s) as a separator. Comma and/or a line feed would be obvious candidates.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up