LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-21-2005, 01:08 PM   #1
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206

Rep: Reputation: 30
Serial setup program


hi

in c programming (linux), i search a way to read the setting of the serial port (speed, parity...) and change it.

i need also to write to a led display (alpha sign communications protocole)

with bash i do:
Code:
echo -e \\000\\000\\000\\000\000\\001Z00\\002AA LINUX \\004 >/dev/ttyS1
now i need to do it in c programming

any idea?

thanks
 
Old 03-22-2005, 07:33 AM   #2
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
http://www.easysw.com/~mike/serial/serial.html
 
Old 03-22-2005, 10:34 AM   #3
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206

Original Poster
Rep: Reputation: 30
i beginning to write this code:

Code:
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>                     /*atoi, system, getenv */
#include <unistd.h>                     /*sleep, close */
#include <dirent.h>                     /*opendir, readdir, closedir */
#include <string.h>
#include <fcntl.h>                      /*open */
#include <sys/stat.h>
#include <sys/errno.h>  

char *ledisplay;
   
/* Opening port one for read and write */
int writeopen(char msg[])
{
    int fd1;
    int wr;
    fd1 = open(ledisplay, O_RDWR | O_NOCTTY | O_NDELAY );
    if (fd1 == -1)
    {
        perror("open_port: Unable to open /dev/ttyS0 - ");
    }
    else
    {
        fcntl(fd1, F_SETFL, 0);
        printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
        wr=write(fd1, msg, sizeof(msg));
        printf(" Bytes sent are %d \n",wr);
        if (wr < 0)
            fputs("write() of n bytes failed!\n", stderr);
        else
            printf(" Stuff has been written into port 1\n");
        close(fd1);
    }
    return (fd1);
} 

int readopen()
{
    int fd1;
    int rd;
    char *buff=NULL;
    fd1 = open(ledisplay, O_RDONLY | O_NOCTTY | O_NDELAY );
    if (fd1 == -1)
    {
        perror("open_port: Unable to open /dev/ttyS0 - ");
    }
    else
    {
        fcntl(fd1, F_SETFL, 0);
        printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
        rd=read(fd1, buff, 100);
        printf(" Bytes recieved are %d \n",rd);
        printf("%s",buff);
        close(fd1);
    }
    return (fd1);
} 

int readRtuConfig()
{
    /* LED_DISPLAY port */
    if(!(ledisplay = getenv("LED_DISPLAY")))
    {
        fprintf(stderr, "%s - Unable to read variable: LED_DISPLAY\n", strerror(errno));
        return -1;
    }
    return 0;
}

int main()
{
    int result;
    char msg[] = {'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0', '\2', 'A', 'A', ' ', 'T', 'R', 'I', 'P', 'L', 'E', 'X', '\4' };
      
    
    if((result=(readRtuConfig()))!=-1)
    {       
        {
        if((result=(writeopen(msg)))!=-1){
            sleep(3); 
        //readopen();   
        }
        
    }    
    return 1;
}

when i run the program, i get:
Code:
Port 1 has been sucessfully opened and 3 is the file descriptor
 Bytes sent are 4
 Stuff has been written into port 1
that seem ok but on the led display, i see nothing...

surely,
char msg[] = {'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0', '\2', 'A', 'A', ' ', 'T', 'R', 'I', 'P', 'L', 'E', 'X', '\4' };

is not equivalent to

echo -e \\000\\000\\000\\000\000\\001Z00\\002AA TRIPLEX\\004 >/dev/ttyS2

any idea?
 
Old 03-22-2005, 10:44 AM   #4
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 51
Octal numbers in C starts with a 0 (zero) (and a chat is a byte which can be represented in octal).
See this for a reference: http://www.lysator.liu.se/c/bwk-tutor.html (search for Octal there) and this: http://www.csee.umbc.edu/courses/und...C_summary.html

try
declare a string here = "\000\000\000\000\000\001\Z00\002\AA TRIPLEX\004";

or

declare a string here = "\o000\o000\o000\o000\o000\o001\oZ00\o002\AA TRIPLEX\o004";

I don't know C but I guess this should work anyways...

Last edited by gbonvehi; 03-22-2005 at 10:48 AM.
 
Old 03-22-2005, 12:34 PM   #5
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by gbonvehi
Octal numbers in C starts with a 0 (zero) (and a chat is a byte which can be represented in octal).
See this for a reference: http://www.lysator.liu.se/c/bwk-tutor.html (search for Octal there) and this: http://www.csee.umbc.edu/courses/und...C_summary.html

try
declare a string here = "\000\000\000\000\000\001\Z00\002\AA TRIPLEX\004";

or

declare a string here = "\o000\o000\o000\o000\o000\o001\oZ00\o002\AA TRIPLEX\o004";

I don't know C but I guess this should work anyways...
with the first option i get:
Code:
com.c:102:18: warning: unknown escape sequence '\Z'
com.c:102:18: warning: unknown escape sequence '\A'
and nothing is displayed on the led display

with the second option i get
Code:
com.c:106:21: warning: unknown escape sequence '\o'
com.c:106:21: warning: unknown escape sequence '\o'
com.c:106:21: warning: unknown escape sequence '\o'
com.c:106:21: warning: unknown escape sequence '\o'
com.c:106:21: warning: unknown escape sequence '\o'
com.c:106:21: warning: unknown escape sequence '\o'
com.c:106:21: warning: unknown escape sequence '\o'
com.c:106:21: warning: unknown escape sequence '\o'
com.c:106:21: warning: unknown escape sequence '\A'
com.c:106:21: warning: unknown escape sequence '\o'
and nothing is displayed on the led display
 
Old 03-23-2005, 11:33 AM   #6
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 51
It may differ on different compilers, it was a generic example.
You'll get an error because neither Z or A are octal numbers, there you should avoid the \ but as I said i don't know C to tell you exactly how to put this, but reading from those page I can see that octal numbers inside strings are escaped with \xxx being the x three numbers.

Maybe: declare a string here = "\000\000\000\000\000\001Z00\002AA TRIPLEX\004";

Edit I did:
Code:
echo -e "\\000\\000\\000\\000\000\\001Z00\\002AA TRIPLEX\\004" > octal.1
and this is what I got in HEX using: bpe octal.1
Code:
ADDRESS      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F   ASCII
-------------------------------------------------------------------------------
0x00000000   00 00 00 00 00 01 5A 30 30 02 41 41 20 54 52 49   ......Z00.AA TRI
0x00000010   50 4C 45 58 04 0A                                 PLEX..
(the latest 0A is a new line character, I'll ignore it but maybe it's necessary)

That means you could try something like:
Code:
    char msg[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01,0x54,0x30,0x30,0x02,0x41,0x41,0x20,0x54,0x52,0x49,0x50,0x4C,0x45,0x58,0x04};
or
Code:
    char msg[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01,'Z','0','0',0x02,'A','A',' ','T','R','I','P','L','E','X',0x04};
Where you use "printable" characters when you can.

Last edited by gbonvehi; 03-23-2005 at 11:32 PM.
 
Old 03-23-2005, 12:34 PM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
Did you check the value for sizeof(msg) in your writeopen(char msg[]) function ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
serial UPS setup apogee Suse/Novell 1 08-14-2005 09:52 AM
How To Setup CDMA modem with Serial Connection stpopoola Linux - Hardware 1 02-25-2005 04:34 AM
setup will not sync palmIIIxe using gnome pilot via serial cable sirius57 Linux - Hardware 0 10-25-2004 09:14 PM
C++ serial port program problem doar Programming 3 10-01-2004 11:46 AM
Controlling serial port RTS pin from 'C' program dcarter Slackware 1 09-26-2003 08:01 PM


All times are GMT -5. The time now is 12:30 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration