Hi,
I am trying to talk to my UPS via a serial port under Linux.
I have a problem talking to it under Linux. I am unsure why the
problem exists because the same program works to a dumb terminal
(/dev/ttyS0) and is OK with a self generated DOS driver.
/dev/UPS is a symbolic link to /dev/cua1 (Com2 for ya DOS'ies). UPS
expects 1200 baud, 8 bit data, 2 stop bits and no parity.
I am using the termios.h file, and I have read and re-read the HOWTO's
and other available info on the Net. I know the port, UPS, and its
cable is OK as I am able to talk correctly to the UPS via a comms
program (Mirror) without changing any hardware.
Any help will be most appreciated.
Thanks & cheers Grahame.
------------------------- program starts here -------------------------
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <termios.h>
#define TIMEOUT 10
#define FALSE 0
#define TRUE 1
#define MAX 80
#define MYPORT "/dev/UPS"
static int timeout = FALSE;
static char *termname, *name;
static int mfd, wcount = 0, rcount = 0;
char *Mess = "OK", line[MAX]; /* Note No cr/lf needed */
struct termio tsaved, tmp;
static int settimeout()
{
fprintf(stderr, "TIMEOUT: on opening %s\n", name);
timeout = TRUE;
ttyopen(filename, flags)Quote:}
char *filename;
int flags;
{
int (* sigfn)(), mfd = -1;
termname = filename;
/*set the timeout flag*/
timeout = FALSE;
/*set SIGALRM action*/
sigfn = signal(SIGALRM, settimeout);
alarm(TIMEOUT);
mfd = open(filename, flags);
/*reset things*/
alarm(0);
signal(SIGALRM, sigfn);
return (timeout ? -1: mfd);
int main()Quote:}
{
int (* sigfn)(), status;
mfd = ttyopen (MYPORT, O_RDWR);
if (mfd > 0) {
fprintf(stdout, "TTY Device opened OK\n");
name = ttyname(mfd);
if(isatty(mfd)) {
fprintf(stderr, "fd %d =>> %s\n", mfd, name);
}else{
fprintf(stderr, "fd %d =>> Line is not a
terminal!\n", mfd);
}
ioctl(mfd, TCGETA, &tsaved); /* Get the current IOCTL
*/
tmp = tsaved; /* Copied current to
future */
tmp.c_cflag &= ~CBAUD; /* Set Buad Rate */
tmp.c_cflag |= B1200; /* Baud Rate = 1200 */
tmp.c_cflag &= ~CSIZE; /* Set Code Size */
tmp.c_cflag |= CS8; /* Codesize = 8 */
tmp.c_cflag |= CSTOPB; /* 2 Stop Bits */
tmp.c_cflag &= ~PARENB; /* Disable Parity */
tmp.c_cc[VMIN] = 10; /* Get char as they come
*/
tmp.c_cc[VTIME] = 0;
tmp.c_lflag &= ~ICANON; /* Raw Mode */
status = ioctl(mfd, TCSETA, &tmp);
if (status < 0) {
fprintf(stderr, "Error in set ioctl\n");
}
wcount = write(mfd, Mess, sizeof(Mess));
if (wcount != sizeof(Mess)) {
fprintf(stderr, "Error: Outputing Mess to %s\n",
name);
}else{
fprintf(stderr, "%s updated\n", name);
/*set the timeout flag*/
timeout = FALSE;
/*set SIGALRM action*/
sigfn = signal(SIGALRM, settimeout);
alarm(TIMEOUT);
rcount = read(mfd, &line[0], 1);
/*reset things*/
alarm(0);
signal(SIGALRM, sigfn);
line[++rcount] = '\x00';
fprintf(stderr, "The number of characters read
-> %d\n\n", rcount);
fprintf(stderr, "Input %s data ==>> %s\n", name,
&line[0]);
}
ioctl(mfd, TCSETA, &tsaved);
close(mfd);
}else{
fprintf(stderr, "Invalid %s file descriptor - Error\n",
name);
}
Quote:}