Help with serial port.

L

laclac01

I need help, i can't figure out why this code doesnt work. I am using
linux.
All i want to be able to do is write some text to an LCD attached to my
serial port on a linux machine. The LCD is serial and I know it works,
baud is 2400 1N8. Attached to com 2.
can someone tell me whats up with this code??
Code:
#include <stdio.h> /* Standard input/output
definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function
definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control
definitions */

/* insert the brackets for each include */

int fd;

int open_port()
{

struct termios options;

fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY
);

if (fd == -1)
{
fprintf(stderr, "open_port: Unable to open /dev/ttyS1
- %s\n",
strerror(errno));
}
else
{
fcntl(fd, F_SETFL, FNDELAY );

if ( tcgetattr(fd, &options ) )
{
fputs("fehler\n", stderr); exit (-1);
}

cfsetispeed(&options, B2400);
cfsetospeed(&options, B2400);


options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

options.c_lflag |= (ICANON | ECHO | ECHOE);

//options.c_cflag |= (CLOCAL | CREAD);
//options.c_cflag |= PARENB; /* no parity */
//options.c_cflag |= 0;
//options.c_cflag |= ~CSTOPB;
//options.c_cflag |= CS8; /* 7 data bits */
//options.c_cflag |= CRTSCTS;

/* Enable data to be processed as raw input */
//options.c_lflag &= ~(ICANON | ECHO | ISIG);

/* Set the new options for the port */
if ( tcsetattr( fd, TCSANOW, &options ) )
{
fputs("fehler\n", stderr); exit (-1);
}

}
return (fd);
}
int main()
{

int n=0;

open_port();

n = write( fd, "out_sp_00 25\n", 13 );
if (n < 0)
fputs("Error\n", stderr);

/* Close the serial port */
close(fd);

return 1;
}
 
S

Skarmander

I need help, i can't figure out why this code doesnt work. I am using
linux.
All i want to be able to do is write some text to an LCD attached to my
serial port on a linux machine. The LCD is serial and I know it works,
baud is 2400 1N8. Attached to com 2.
<snip>

You're looking for one of the comp.os.linux.* newsgroups, possibly
comp.os.linux.development or comp.os.linux.hardware.

This newsgroup only deals with questions regarding standard C.

S.
 
A

Alexei A. Frounze

I need help, i can't figure out why this code doesnt work. I am using
linux.
All i want to be able to do is write some text to an LCD attached to my
serial port on a linux machine. The LCD is serial and I know it works,
baud is 2400 1N8. Attached to com 2.
can someone tell me whats up with this code??

No, this is wrong group for what you need. This group is dedicated to the
standard C, which for some reason knows nothing (as per the standard) of the
hardware connected to the CPU. Try a different group. Try documentation on
ports and using them in your OS.

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top