LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-27-2002, 03:08 PM   #1
togunter
LQ Newbie
 
Registered: Apr 2002
Location: Los Angeles
Distribution: Red Hat 7.0 & 7.2
Posts: 4

Rep: Reputation: 0
writing to serial port, /dev/ttyS0


Hi all-
I'm trying to write some data to a device on the com1 port on my Redhat 7.2 box, but when I write data to /dev/ttyS0, the characters I write appear to get garbled(and i dont even seem to get the same number of characters).
The same code works fine under windows using cygwin to write to /dev/com1 (after using stty to determine that the serial port is setup the same as in linux).

the code i am using is below.

any ideas?
thanks for any help
--tim

//
// begin 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 */

main()
{
int fd;
struct termios options;

int i ;
unsigned char data[20] = { 0x40, 0x28, 0x6b, 0xfe,
0x00, 0x00, 0x00, 0x00,
0x00, 0x23, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00,
0xd9, 0xff, 0xff, 0xff} ;

// linux
//fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);

// cygwin
fd = open("/dev/com1", O_RDWR | O_NOCTTY | O_NONBLOCK);
fcntl(fd, F_SETFL, 0);

/* get the current options */
tcgetattr(fd, &options);

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

/* set raw input, 1 second timeout */
options.c_cflag |= (CLOCAL | CREAD);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;


options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

options.c_cflag &= ~CRTSCTS;

// options.c_iflag &= ~(IXON | IXOFF | IXANY); /* no flow control */

options.c_oflag &= ~(IXON | IXOFF | IXANY); /* no flow control */

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; /* No output processing */
options.c_oflag &= ~ONLCR; /* Don't convert linefeeds */

/* Miscellaneous stuff */
options.c_cflag |= (CLOCAL | CREAD); /* Enable receiver, set local */
/* Linux seems to have problem with the following ??!! */
options.c_cflag |= (IXON | IXOFF); /* Software flow control */
options.c_lflag = 0; /* no local flags */
options.c_cflag |= HUPCL; /* Drop DTR on close */

/* Setup non blocking, return on 1 character */
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 1;

/* Clear the line */
tcflush(fd,TCIFLUSH);

/* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0) {
perror("SetupSerial 3");
return(0);
}

write(fd, data, 20 ) ;
close(fd);
return 0 ;
}
 
Old 06-08-2008, 09:26 AM   #2
sieira
Member
 
Registered: Dec 2007
Location: Alcalá de Henares (Madrid)
Distribution: Debian
Posts: 40

Rep: Reputation: 16
¿How do you read them?
 
Old 06-08-2008, 09:27 AM   #3
sieira
Member
 
Registered: Dec 2007
Location: Alcalá de Henares (Madrid)
Distribution: Debian
Posts: 40

Rep: Reputation: 16
Heheeee i'm six years late...
 
  


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
Using serial port card(PCMCIA) with IPAQ running Linux, can't find ttyS0 port d2army Linux - Laptop and Netbook 0 11-12-2005 08:07 PM
Unable to communicate using serial port (COM1) /dev/ttys0 vinaybms Linux - Networking 1 03-11-2005 08:08 AM
speed/specs for ttyS0 or some other standard serial port? BigDawg Programming 3 01-23-2004 11:47 AM
Serial comms - script to poll /dev/ttyS0 chr15t0 Linux - Networking 1 07-08-2003 10:18 PM
Telnet into Cisco router via /dev/ttyS0 (aka COM1 serial port) naesyllek Linux - Networking 2 06-25-2003 05:33 PM

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

All times are GMT -5. The time now is 05:37 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
Open Source Consulting | Domain Registration