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



Relevant Pages

  • Re: Cannot return values of char variable
    ... - buffer = ... Since you seem to be trying to return a char pointer ... int id = random; ... content is interpreted as a string. ...
    (comp.lang.c)
  • [KGDB PATCH][2/7] Serial updates, take 2
    ... Also make put_packet look at the char it reads, ... * Empty the receive buffer first, then look at the interface hardware. ... * This is the receiver interrupt routine for the GDB stub. ... -extern volatile int kgdb_connected; ...
    (Linux-Kernel)
  • Re: Write to file
    ... fwrite, outfile); ... This will repeatedly try to print the first character in buffer as ... int main(int argc, char *argv) ...
    (comp.lang.c)
  • Re: Write to file
    ... int main(int argc, char *argv) ... I intended you you use c here not read another character! ... You reuse the buffer. ...
    (comp.lang.c)
  • Re: newbie question: whats wrong with my code?
    ... why not declare buffer as a char * or unsigned char *? ... int clean_up; ... unsigned char buf[BUFSIZ]; ...
    (comp.lang.c)