1

Hi i want to pass a data from my char device driver to serial port ttyS0..

I have created a simple char driver module which reads and write and it's my first tym doing it.. i am using debian guest os...

e.g.

echo "hello" > /dev/mydev

now when /dev/mydev receives the data it will then alter the string into something like "hello too" which is passed to my serial port /dev/ttyS0..

how can i alter the string?.. is it possible to use if statement inside mydev?

e.g

if(string=="hello"){
alterstringTO: hello to;

pass "hello to" /dev/ttyS0;

like echoing in terminal..

echo "hello to" > /dev/ttyS0
}

Is that possible?... or is there any other way doing it?

Here some of the code..

ssize_t dev_read(struct file *filp, char *buf, size_t clen, loff_t *f_pos){

short cnt =0;
while(clen && (msg[Pos]!=0))
{
    put_user(msg[Pos],buf++);
    cnt++;
    clen--;
    Pos++;
}
return cnt;

    }


   ssize_t dev_write(struct file *filp, const char *buf, size_t clen, loff_t *f_pos){

short dec = clen-1;
short cnt=0;
memset(msg,0,50);
Pos=0;
while(clen>0)
{
    msg[cnt++] = buf[dec--];
    clen--;
}
return cnt;
    }

Thanks in advance..

demic0de
  • 1,313
  • 1
  • 15
  • 30

2 Answers2

1

I'm not exactly sure what you are trying to achieve here, as the question and the intent seems unclear to me. I'll provide some guidance, but recommend that you edit your question and make it more readable.

Your snippet to compare strings is not correct. You can learn more about how to compare strings in C in here.

Altering a string in C is a basic operation that you learn when you start working with strings. This should help you getting started.

As final remark, please note that programming for the kernel requires extra care. A small mistake may lead to a crash and loss of data. If you really must, then the book Linux Device Drivers 3rd Edition is freely available and can help you further.

Community
  • 1
  • 1
Goncalo
  • 362
  • 4
  • 8
1

Just a comment on writing to the serial port:

Remember the Linux foundations, everything is a file in Linux. To write to the device driver from a program you need to open the file for writing and then you can fprintf whatever data you want. You can do that from user space as well (the recommended way)

Refer to the following man pages:

  • man fopen
  • man fread/fwrite
  • man fprintf
  • man fclose
Mr_GQ
  • 56
  • 4
  • Thanks for the answer this really helped me. Yes i've created a user space app and it's now working smoothly. – demic0de Sep 25 '12 at 23:21