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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
03-21-2005, 12:08 PM
#1
Member
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206
Rep:
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
03-22-2005, 06:33 AM
#2
Member
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282
Rep:
03-22-2005, 09:34 AM
#3
Member
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206
Original Poster
Rep:
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?
03-22-2005, 09:44 AM
#4
Senior Member
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145
Rep:
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 09:48 AM .
03-22-2005, 11:34 AM
#5
Member
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206
Original Poster
Rep:
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
03-23-2005, 10:33 AM
#6
Senior Member
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145
Rep:
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 10:32 PM .
03-23-2005, 11:34 AM
#7
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
Did you check the value for sizeof(msg) in your writeopen(char msg[]) function ?
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 03:54 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News