LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-02-2004, 01:24 PM   #1
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Rep: Reputation: 30
Problem to write to serial port


hi

under linux, i try to write to serial port who pluged to a monitor (del monitor)

with bash i do
Code:
echo -e \\000\\000\\000\\000\000\\001Z00\\002AA Keg Marty is in the house\\004 >/dev/ttyS2
that work fine.. Keg Marty is in the house is displayed on the panel

i would like to do the same thing in C

my c program (compile well... but don't work)
Code:
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main (void)
{
  int iOut = 0;
  int fd = 0;
  char *sCompPort="/dev/ttyS2";
  //char cmd[70];
  char cmd[]="\\000\\000\\000\\000\000\\001Z00\\002AA Keg Marty is in the house\\004";

  fd = open(sCompPort, O_RDWR | O_NOCTTY);

  if (fd >0){
    printf("port open");

    iOut = write(fd, cmd, strlen(cmd));
    close(fd);
   }
   else{
     printf("not able to open port.");
   }

   if (iOut > 0){
     printf("no commmunication.");
   }
   else{
     printf("good Communication");
   }
   return (0);
}
return me port open and no communication...


any idea?
 
Old 12-02-2004, 11:34 PM   #2
bastard23
Member
 
Registered: Mar 2003
Distribution: Debian
Posts: 275

Rep: Reputation: 30
write() returns the number of bytes and -1 on error. So if (iOut >0) is backwards.

if (iOut < 0){
printf("no commmunication.");
}
else{
printf("Wrote %d of %d bytes", iOut, sizeof(cmd));
}
return (0);

Second, you don't need the double backslashes on your string (\\001), just one (\001). And remember it's octal, so use \xnn when you mean hex. You need the double backslash on the echo command because the shell interprets the backslash as well.
 
Old 12-03-2004, 07:17 PM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
There's one more issue with that example code, the cmd[] array begins with a \0, so strlen() can't be used to calculate the number of bytes to transmit in the write() function. Previous poster correctly said sizeof(cmd) would work, but I think it should be sizeof(cmd)-1 to be most accurate, the compiler includes the implicit termination null in sizeof() a char array.
 
Old 12-22-2004, 10:59 AM   #4
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Original Poster
Rep: Reputation: 30
with this code,

Code:
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main (void)
{
  int iOut = 0;
  int fd = 0;
  char *sCompPort="/dev/ttyS2";
  //char cmd[70];
  //echo -e \\000\\000\\000\\000\000\\001Z00\\002AA LINUX \\004 >/dev/ttyS2
  
  char cmd[]="\\000\\000\\000\\000\\000\\001Z00\\002AA linux \\004";
  //return
  //port open
  //strlen(cmd) 43
  //Wrote 43 of 43 bytes
  
  //char cmd[]="\000\000\000\000\000\001Z00\002AA linux\004";
  //return
  //port open
  //strlen(cmd) 0
  //Wrote 0 of 19 bytes
  
  fd = open(sCompPort, O_RDWR | O_NOCTTY);

  if (fd >0){
    printf("port open\n");
    printf("strlen(cmd) %d\n",strlen(cmd) );
    iOut = write(fd, cmd, strlen(cmd));
    close(fd);
   }
   else{
     printf("not able to open port.\n");
   }

   if (iOut < 0){
     printf("no commmunication.\n");
   }
   else{
     printf("Wrote %d of %d bytes\n", iOut, sizeof(cmd)-1);
   }
   return (0);
}
data was sending to the serial port... the port is connected to a display pannel...
there are some code to initialize the connection, centering the text, close connection... in bash mode that work fine... but not in this c program...
nothing is interpreted... all is displayed to the display pannel...

any idea?
 
Old 12-22-2004, 02:11 PM   #5
shmonkey
Member
 
Registered: Nov 2004
Location: UK
Distribution: Ubuntu
Posts: 118

Rep: Reputation: 15
How exactly are you compiling the program ?
 
Old 12-22-2004, 02:37 PM   #6
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by shmonkey
How exactly are you compiling the program ?
my makefile
Code:
CC= /opt/cross-ppc/bin/powerpc-855-linux-gnu-gcc-3.3
DEFS=
PROGNAME= moniteur
INCLUDES=
LIBS=

# replace -O with -g in order to debug

DEFINES= $(INCLUDES) $(DEFS)
#CFLAGS= -g -Wall $(DEFINES)
CFLAGS= -O -Wall $(DEFINES)

SRCS = moniteur.c

OBJS = moniteur.o

.c.o:
        rm -f $@
        $(CC) $(CFLAGS) -c $*.c

all:    $(PROGNAME)

$(PROGNAME) :   $(OBJS)
        $(CC) $(CFLAGS) -o $(PROGNAME) $(OBJS) $(LIBS)
        cp $(PROGNAME) /opt/good/bin

clean:
        rm -f $(OBJS) $(PROGNAME) core
 
Old 05-08-2013, 09:24 PM   #7
Jack-s
LQ Newbie
 
Registered: May 2013
Posts: 3

Rep: Reputation: Disabled
Serial Port Programming in Linux

you may try to compare with this c source code

Link:
http://trainingkits.gweb.io/serial-linux.html
 
Old 05-10-2013, 04:33 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your literal strings contain double backslashes. If you intend to use the notation for octal bytes in your literal strings, remove one of the backslashes from each octal byte expression in each string:
Code:
char cmd[]="\000\000\000\000\000\001Z00\002AA linux \004";
For the full story on serial IO in Linux, see:

Serial Programming
Serial-Howto
Serial Programming Howto
Serial Programming Guide for POSIX OS's


--- rod.

Last edited by theNbomr; 05-10-2013 at 04:35 PM.
 
Old 08-23-2016, 02:48 AM   #9
Jack-s
LQ Newbie
 
Registered: May 2013
Posts: 3

Rep: Reputation: Disabled
Quote:
Originally Posted by Jack-s View Post
you may try to compare with this c source code

Link:
http://trainingkits.gweb.io/serial-linux.html
Link Update : http://training-kits.appspot.com/serial-linux.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 08:35 PM
Serial port Read Write SeanatIL Programming 2 07-14-2004 03:42 PM
serial port problem... linux4john Linux - Software 1 10-30-2003 10:02 PM
Serial port problem ruchika Linux - Networking 0 08-15-2003 04:18 PM
Problem with Serial port mritunjay Linux - Networking 1 04-22-2002 01:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration