I want open serial port in CGI,the code is below:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include "cgic.h"
#include <string.h>
void set_speed(int fd, int speed);
int set_Parity(int fd,int databits,int stopbits,int parity);
#define FALSE -1
#define TRUE 0
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
int cgiMain(){
int fd;
int nread;
char buff[512]={'h','e','l','l','o','!'};
#ifdef DEBUG
LoadEnvironment();
#endif /* DEBUG */
cgiHeaderContentType("text/html");
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>sertest</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY><H1>Serious Test</H1>\n");
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY);
if (fd == -1){
fprintf(cgiOut,"serious open error! fd= %d",fd);
exit(0);
}
set_speed(fd,19200);
if (set_Parity(fd,8,1,'N') == FALSE) {
fprintf(cgiOut,"Set Parity Error\n fd= %d",fd);
exit (0);
}
if(write(fd, buff ,512)<=0){
fprintf(cgiOut,"Can't write serious!\n");
exit(0);
}
close(fd);
fprintf(cgiOut, "<hr>\n");
fprintf(cgiOut, "</BODY></HTML>\n");
return 0;
}
void set_speed(int fd, int speed){
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
fprintf(cgiOut,"tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
fprintf(cgiOut,"SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置و•°و?®ن½?و•°*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(cgiOut,"Unsupported data size\n"); return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置ن¸؛ه¥‡و•ˆé?Œ*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; options.c_cflag &= ~PARODD; options.c_iflag |= INPCK; break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;break;
default:
fprintf(cgiOut,"Unsupported parity\n");
return (FALSE);
}
/* 设置ه?œو_¢ن½?*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(cgiOut,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超و—¶15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
fprintf(cgiOut,"SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
but I can't open the serial port,
who can tell me how can I do?
Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include "cgic.h"
#include <string.h>
void set_speed(int fd, int speed);
int set_Parity(int fd,int databits,int stopbits,int parity);
#define FALSE -1
#define TRUE 0
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
int cgiMain(){
int fd;
int nread;
char buff[512]={'h','e','l','l','o','!'};
#ifdef DEBUG
LoadEnvironment();
#endif /* DEBUG */
cgiHeaderContentType("text/html");
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>sertest</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY><H1>Serious Test</H1>\n");
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY);
if (fd == -1){
fprintf(cgiOut,"serious open error! fd= %d",fd);
exit(0);
}
set_speed(fd,19200);
if (set_Parity(fd,8,1,'N') == FALSE) {
fprintf(cgiOut,"Set Parity Error\n fd= %d",fd);
exit (0);
}
if(write(fd, buff ,512)<=0){
fprintf(cgiOut,"Can't write serious!\n");
exit(0);
}
close(fd);
fprintf(cgiOut, "<hr>\n");
fprintf(cgiOut, "</BODY></HTML>\n");
return 0;
}
void set_speed(int fd, int speed){
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
fprintf(cgiOut,"tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
fprintf(cgiOut,"SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置و•°و?®ن½?و•°*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(cgiOut,"Unsupported data size\n"); return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置ن¸؛ه¥‡و•ˆé?Œ*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; options.c_cflag &= ~PARODD; options.c_iflag |= INPCK; break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;break;
default:
fprintf(cgiOut,"Unsupported parity\n");
return (FALSE);
}
/* 设置ه?œو_¢ن½?*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(cgiOut,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超و—¶15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
fprintf(cgiOut,"SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
but I can't open the serial port,
who can tell me how can I do?
Thanks!