📄 ticket_old.c
字号:
/*********************************************************************
* name: ticket.c
* desc: read ticket from machine, then print data.
* date: 2002.12.10
* author: wu qian
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
extern PrintPaper(char *pstr);
static void sig_alarm(int);
void turn(int ct, int a[20]);
void randnum(char *data);
/*
* open port 1
* success to return fd, fail to return -1.
*/
int open_port1(void)
{
int fd;
if (signal(SIGALRM, sig_alarm) == SIG_ERR)
{
printf("signal(SIGALRM) error\n");
return (-1);
}
alarm(3);
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY |O_NDELAY );
if (fd == -1)
{
perror("open_port1: Unable to open /dev/ttyS0 ");
return(-1);
}
else
//printf("errno=%s\n",strerror(errno));
fcntl(fd,F_SETFL, 0);
alarm(0);
return(fd);
}
int read_port1(int fd, char buffer[255])
{
int len;
memset(buffer, 0, sizeof(buffer));
if ((len=read(fd, buffer, 255)) < 0 )
{
perror("read buffer. \n");
return -2;
}
else if (len > 0)
{
buffer[len]=0;
}
return 0;
}
static void sig_alarm(int signumber)
{
printf("timeout.\n");
exit(-8);
}
/* send command to serial port */
int command(int fd, char command[2])
{
int len, try;
len = strlen(command);
if ((write(fd, command, len)) < len )
return -1; /* write port error */
//printf("command=%s\n", command);
return 0;
}
/*
* set port parameter
* on error: return -1,
* on success: return fd.
*/
int init_port1(struct termios oldtio)
{
int fd;
struct termios options;
if ((fd = open_port1()) < 0)
{
return -1;
}
/* get current port status value */
tcgetattr(fd, &oldtio);
bzero(&options, sizeof(options));
/* set init parament value, timeout is 1 second */
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= CREAD;
options.c_cflag |= CLOCAL;
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
//options.c_lflag |= ( ECHO );
options.c_lflag &= ~(ICANON | ECHO | ECHOE |ISIG);
options.c_oflag |= OPOST;
options.c_iflag |= (IGNPAR | ICRNL );
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 20;
options.c_cc[VINTR] = 0; /* Ctrl-c */
options.c_cc[VQUIT] = 0; /* Ctrl-\ */
options.c_cc[VERASE] = 0; /* del */
options.c_cc[VKILL] = 0; /* @ */
options.c_cc[VEOF] = 4; /* Ctrl-d */
options.c_cc[VSWTC] = 0; /* '\0' */
options.c_cc[VSTART] = 0; /* Ctrl-q */
options.c_cc[VSTOP] = 0; /* Ctrl-s */
options.c_cc[VSUSP] = 0; /* Ctrl-z */
options.c_cc[VEOL] = 0; /* '\0' */
options.c_cc[VREPRINT] = 0; /* Ctrl-r */
options.c_cc[VDISCARD] = 0; /* Ctrl-u */
options.c_cc[VWERASE] = 0; /* Ctrl-w */
options.c_cc[VLNEXT] = 0; /* Ctrl-v */
options.c_cc[VEOL2] = 0; /* '\0' */
tcflush(fd,TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
return(fd);
}
/****************************************************
* read ticket
* 0: success;
* 1: no ticket be checked;
* -1: open port error;
* -2: write port error;
* -3: read port error;
* -4: read ticket data error;
****************************************************/
int lottery()
{
int fd, ret, len, nbytes;
int i, j, p, cc=0, count, num;
int number[20];
char buffer[60];
char rn[4]={0x0a,0x0d};
char print_data[160] = "\0";
char data[60]="\0", tp[4]="\0", grp1[30], tknum1[30]="\0";
char str1[30]="\0", str2[30]="\0", str3[30]="\0", str4[30]="\0", str5[30]="\0";
struct termios oldset;
printf("Please insert your lottery ticket.\n");
//printf("'6':eject ticket; '8':eject ticket when error; 'a':read again; 'esc':exit.\n");
if ((fd=init_port1(oldset)) <0 )
{
printf("open port:");
return -1;
}
if ((ret=command(fd, "2")) < 0)
{
perror("write port:");
tcsetattr(fd, TCSANOW, &oldset);
close(fd);
return -2;
}
sleep(5);
while(1)
{
memset(buffer, 0, sizeof(buffer));
while ((nbytes=read(fd, buffer, 60)) > 0)
{
if ( (nbytes < 54) || (buffer[nbytes-1] != 0x02))
continue;
buffer[nbytes]=0;
//printf("nbytes=%d, buffer[1]=%x, buffer[2]=%x\n",nbytes, buffer[1],buffer[2]);
break;
}
if (nbytes < 0)
{
if (errno==EAGAIN)
{
printf("I/O is NON_BLOCKING mode.\n");
}
perror("read port");
tcsetattr(fd, TCSANOW, &oldset);
close(fd);
return -3;
}
else if (nbytes == 0)
{
sleep(2);
cc++;
printf("read null, cc=%d\n", cc);
}
else
break;
if (cc == 3)
{
printf("no ticket be inserted!\n");
tcsetattr(fd, TCSANOW, &oldset);
close(fd);
return 1;
}
}
if ( ((buffer[0]&0x01) != 0x01) && ((buffer[0]&0x71) != 0x71) )
{
printf("ticket's data error.\n");
tcsetattr(fd, TCSANOW, &oldset);
close(fd);
return -4;
}
strncpy(data,buffer+3,50);
for (i=0;i<5;i++)
{
strcpy(tknum1,"\0");
//memset(tknum1,' ',sizeof(tknum1));
count=0;
for (cc=0;cc<20;cc++)
number[cc]=0;
if ( (data[9+i*10]&0xC0) == 0xC0 )
{
printf("The %d column is be cancled.\n",i+1);
continue;
}
if ( ((data[9+i*10]<<2)&0x80) == 0x80 )
{
printf("The %d column is be random selected.\n",i+1);
strcpy(grp1,"\0");
srand(rand());
randnum(grp1);
switch(i)
{
case 0: strcpy(str1,"① ");strcat(str1,grp1); strcat(str1,rn); break;
case 1: strcpy(str2,"② ");strcat(str2,grp1); strcat(str2,rn); break;
case 2: strcpy(str3,"③ ");strcat(str3,grp1); strcat(str3,rn); break;
case 3: strcpy(str4,"④ ");strcat(str4,grp1); strcat(str4,rn); break;
case 4: strcpy(str5,"⑤ ");strcat(str5,grp1); strcat(str5,rn); break;
}
continue;
}
for (p=0;p<8;p++)
{
for (j=1;j<8;j++)
{
if ( ((data[i*10+p]<<j)&0x80) == 0x80 )
{
switch(p)
{
case 1: num=j;
break;
case 0: num=(p+1)*7+j;
break;
case 2: num=(p+1)*7+j-1;
break;
case 3: num=(p-1)*7+j-1;
break;
case 4: num=(p+1)*7+j-2;
break;
case 5: num=(p-1)*7+j-2;
break;
case 6: num=(p+1)*7+j-3;
break;
case 7: num=(p-1)*7+j-3;
break;
}
count++;
number[count]=num;
}
}
}
turn(count, number);
for (j=1;j<=count;j++)
{
if (number[j] < 10)
sprintf(tp, "0%d+", number[j]);
else
sprintf(tp, "%d+", number[j]);
strcat(tknum1,tp);
}
switch(i)
{
case 0: strcpy(str1,"① ");strcat(str1,tknum1);len=strlen(str1);
printf("len=%d\n",len);str1[len-1]='\0'; strcat(str1,rn); break;
case 1: strcpy(str2,"② ");strcat(str2,tknum1);len=strlen(str2);
str2[len-1]='\0'; strcat(str2,rn); break;
case 2: strcpy(str3,"③ ");strcat(str3,tknum1);len=strlen(str3);
str3[len-1]='\0'; strcat(str3,rn); break;
case 3: strcpy(str4,"④ ");strcat(str4,tknum1);len=strlen(str4);
str4[len-1]='\0'; strcat(str4,rn); break;
case 4: strcpy(str5,"⑤ ");strcat(str5,tknum1);len=strlen(str5);
str5[len-1]='\0'; strcat(str5,rn); break;
}
}
strcpy(print_data,str1);
strcat(print_data,str2);
strcat(print_data,str3);
strcat(print_data,str4);
strcat(print_data,str5);
printf("\n%s\n",str1);
printf("%s\n",str2);
printf("%s\n",str3);
printf("%s\n",str4);
printf("%s\n",str5);
/* while(1)
{
ch=getchar();
switch(ch)
{
case '8':
if ((ret=command(fd, "8")) < 0)
{
perror("write port:");
tcsetattr(fd, TCSANOW, &oldset);
close(fd);
return -2;
}
break;
case 'a':
if ((ret=command(fd, "A")) < 0)
{
perror("write port:");
tcsetattr(fd, TCSANOW, &oldset);
close(fd);
return -2;
}
goto reread;
case '6':
if ((ret=command(fd, "6")) < 0)
{
perror("write port:");
tcsetattr(fd, TCSANOW, &oldset);
close(fd);
return -2;
}
break;
case 27:
break;
}
if (ch!='a')
break;
}
*/
tcsetattr(fd, TCSANOW, &oldset);
printf("lisp\n");
//printf("print data = %s\n",print_data);
/* if ( (i=close(fd)) < 0 )
{
perror("close:");
printf("i=%d\n");
}
*/
//PrintPaper(print_data);
exit(0);
}
void randnum(char *data)
{
int i, count=1, a, len;
int tm[10];
char y[4]="\0";
strcpy(data,"\0");
while(count<8)
{
label: a=1+(int) (49.0*rand()/(RAND_MAX+1.0));
for(i=1;i<count;i++)
{
if (a==tm[i])
goto label;
}
tm[count]=a;
count++;
}
turn(count-1,tm);
for(i=1;i<count;i++)
{
if (tm[i]<10)
sprintf(y,"0%d+",tm[i]);
else
sprintf(y,"%d+",tm[i]);
strcat(data,y);
}
len=strlen(data);
data[len-1]='\0';
//puts(data);
return;
}
void turn(int ct, int a[20])
{
int i,j,t;
for (j=1;j<=ct-1;j++)
for (i=1;i<=ct-j;i++)
if (a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
return;
}
/*
void main()
{
char data[151] = "\0";
lottery(data);
PrintPaper(data);
return;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -