|
|
#include<stdio.h>
#include<string.h>
#include<termios.h>
#include<fcntl.h>
#include <unistd.h>
#define BAUDRATE B38400
#define ina 1
#define MODEMDEVICE "/dev/ttyS0"
#define FALSE 0
#define TRUE 1
int main(void){
int fd0,fd1,res,bb,cc=0, ch;
int Count;
FILE * fd_2;
unsigned char aa[255], ac;
/* for(bb=0;bb<ina;bb++){
aa[bb]=0;
}*/
// char ch[255];
struct termios oldtio0,oldtio1,newtio;
/*ポートの設定*/
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = (BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD);
newtio.c_iflag = (IGNPAR | ICRNL);
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
/*ポートの確保*/
if((fd0 = open(MODEMDEVICE, O_RDWR | O_NOCTTY ))==-1){
puts("ttyS0 open error");
exit(-1);
}
tcflush(fd0, TCOFLUSH);
/*ポートの設定*/
// tcgetattr(fd0, &oldtio0);
// tcsetattr(fd0, TCSANOW, &newtio);
/*ポートの確保*/
if((fd1 = open("/dev/ttyS1", O_RDWR | O_NOCTTY ))==-1){
puts("ttyS1 open error");
exit(-1);
}
tcflush(fd1, TCIFLUSH);
/*ポートの設定*/
// tcgetattr(fd1, &oldtio1);
// tcsetattr(fd1, TCSANOW, &newtio);
/*ファイルのオープン*/
if((fd_2 = fopen("test_a.txt","r"))==NULL){
puts("FILE open error");
exit(-1);
}
/*データをポートへ送信*/
puts("write data.");
while((ch=fgetc(fd_2))!= EOF){
unsigned char cc = ch;
printf("%c",cc);
write(fd0,&cc,1);
// write(fd0,&ch,sizeof(ch));
}
// output EOF
ac = 4;
write(fd0,&ac,1);
if(fclose(fd_2)==-1){
puts("file close error");
}
// tcsetattr(fd0, TCSANOW, &oldtio0);
if(close(fd0)==-1){
puts("ttyS0 close error");
exit(-1);
}
// sleep(2);
aa[1] = 0;
cc=0;
// Count = 0;
puts("read data.");
while(cc<20){
res=read(fd1,aa,ina);
if(res == 0)
break;
if(res==0 || aa[0] == 7){
if(++Count <= 1000)
continue;
else
break;
}
// putchar('a');
// Count = 0;
printf("%c",*aa);
// printf(" res=%d Read=%d conut=%2d\n", res, strlen(aa),cc+1);
// putchar('\n');
// for(bb=0;bb<ina;bb++){
// printf("%02X ",*aa);
// }
cc++;
}
// putchar('\n');
printf("fd0=%d fd1=%d res=%d aa[4]=%c\n",fd0,fd1,res,aa[4]);
/*戸締り♪*/
// tcsetattr(fd1, TCSANOW, &oldtio1);
if(close(fd1)==-1){
puts("ttyS1 close error");
exit(-1);
}
return 0;
}
|
|