Re: Proper use of write() on RS-232 serial interface

From: Jan Panteltje (pNaonStpealmtje_at_yahoo.com)
Date: 08/17/05


Date: Wed, 17 Aug 2005 13:18:59 GMT

On a sunny day (Wed, 17 Aug 2005 14:44:03 +0200) it happened Bjoern
Schliessmann <chr0n0ss@despammed.com> wrote in
<3mgpojF165jkgU1@individual.net>:

>HASM wrote:
>> Bjoern Schliessmann <chr0n0ss@despammed.com> writes:
>
>>> write(ttyS0_fd,&cmd,strlen(cmd));
>
>> Shouldn't this be "write(ttyS0_fd,cmd,strlen(cmd));" ?
>
>I don't think so; man 2 write says
>
>| ssize_t write(int fd, const void *buf, size_t count);
> ^
>... and if I just write cmd and not &cmd, the compiler says
In many cases one would do this:
char *buffer;
...
buffer = (char *)malloc(something);

write(ttyS0_fd, buffer

If you use an array:
command is a pointer to a memory location holding the text 'do something',
and already an address.
This works, using a cast:

#include <stdio.h>
#include <stdlib.h>

main()
{
int a;
char command[] = "do something\n";
int fd = 1; // stdout
         
a = write(fd, (char *)command, strlen(command) );
}

panteltje:~# ./a.out
do something