467,210 Members | 1,323 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,210 developers. It's quick & easy.

Help with serial port.

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??
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> /* Standard input/output
  2. definitions */
  3. #include <string.h> /* String function definitions */
  4. #include <unistd.h> /* UNIX standard function
  5. definitions */
  6. #include <fcntl.h> /* File control definitions */
  7. #include <errno.h> /* Error number definitions */
  8. #include <termios.h> /* POSIX terminal control
  9. definitions */
  10.  
  11. /* insert the brackets for each include */
  12.  
  13. int fd;
  14.  
  15. int open_port()
  16. {
  17.  
  18. struct termios options;
  19.  
  20. fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY
  21. );
  22.  
  23. if (fd == -1)
  24. {
  25. fprintf(stderr, "open_port: Unable to open /dev/ttyS1
  26. - %s\n",
  27. strerror(errno));
  28. }
  29. else
  30. {
  31. fcntl(fd, F_SETFL, FNDELAY );
  32.  
  33. if ( tcgetattr(fd, &options ) )
  34. {
  35. fputs("fehler\n", stderr); exit (-1);
  36. }
  37.  
  38. cfsetispeed(&options, B2400);
  39. cfsetospeed(&options, B2400);
  40.  
  41.  
  42. options.c_cflag &= ~PARENB;
  43. options.c_cflag &= ~CSTOPB;
  44. options.c_cflag &= ~CSIZE;
  45. options.c_cflag |= CS8;
  46.  
  47. options.c_lflag |= (ICANON | ECHO | ECHOE);
  48.  
  49. //options.c_cflag |= (CLOCAL | CREAD);
  50. //options.c_cflag |= PARENB; /* no parity */
  51. //options.c_cflag |= 0;
  52. //options.c_cflag |= ~CSTOPB;
  53. //options.c_cflag |= CS8; /* 7 data bits */
  54. //options.c_cflag |= CRTSCTS;
  55.  
  56. /* Enable data to be processed as raw input */
  57. //options.c_lflag &= ~(ICANON | ECHO | ISIG);
  58.  
  59. /* Set the new options for the port */
  60. if ( tcsetattr( fd, TCSANOW, &options ) )
  61. {
  62. fputs("fehler\n", stderr); exit (-1);
  63. }
  64.  
  65. }
  66. return (fd);
  67. }
  68. int main()
  69. {
  70.  
  71. int n=0;
  72.  
  73. open_port();
  74.  
  75. n = write( fd, "out_sp_00 25\n", 13 );
  76. if (n < 0)
  77. fputs("Error\n", stderr);
  78.  
  79. /* Close the serial port */
  80. close(fd);
  81.  
  82. return 1;
  83. }
  84.  
  85.  
  86.  
Nov 15 '05 #1
  • viewed: 1142
Share:
2 Replies
la******@yahoo.com wrote:
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.
Nov 15 '05 #2
<la******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tomislav Lepusic | last post: by
2 posts views Thread by willie@macleod-group.com | last post: by
3 posts views Thread by collinm | last post: by
13 posts views Thread by Al the programmer | last post: by
1 post views Thread by David | last post: by
13 posts views Thread by Rob | last post: by
4 posts views Thread by rowan@sylvester-bradley.org | last post: by
3 posts views Thread by naveen.sabapathy@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.