|
RS232Cの送受信のプログラムを作成し、コンピュータから送信されたコマンドがもう一方のコンピュータに受信されるかを調べたところうまくいきません。プログラムは以下です。(OS:Linux コンパイラ:gcc)
// 一般用ヘッダファイル
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
// RS-232C用ヘッダファイル
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
int trans(void);
int send(void);
// 定数設定
#define BAUDRATE B9600 // シリアル通信速度(ボーレート)
/**********************************************************************
* main()
* メイン関数
**********************************************************************/
int send(void)
{
int Send_Fd, Send_Res;
// char Send_Dev[50];
char buf[255];
char Send_Buf[255];
struct termios Send_Newtio;
int SendLen;
// 画面消去&タイトル表示
system( "clear" );
fprintf( stdout, "************ Com Send Program ************\n\n" );
#if 0
printf( "\nInput the Send Port Name : " );
fgets( Send_Dev, 50, stdin );
Send_Dev[strlen( Send_Dev )-1] = 0;
#endif
// シリアルデバイスオープン(/dev/ttyS0)
// Send_Fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK );
//Send_Fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
Send_Fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY );
if ( Send_Fd < 0 )
{
perror( "/dev/ttyS0" );
exit( -1 );
}
// 通信条件設定用構造体クリアー
memset( &Send_Newtio, 0x00, sizeof( Send_Newtio ) );
// 通信速度はBAUDRATE,データビットは8ビット,ストップビットは1ビット,パリティ無し,フロー制御無し
Send_Newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
Send_Newtio.c_iflag = IGNPAR | ICRNL;
Send_Newtio.c_oflag = 0;
Send_Newtio.c_lflag = ICANON;
// バッファ内残留データを消去
tcflush( Send_Fd, TCOFLUSH );
// シリアル通信条件設定
if ( tcsetattr( Send_Fd, TCSANOW, &Send_Newtio ) != 0 )
{
fprintf( stdout, "tcsetattr Error! \n " );
exit( -1 );
}
// データ書き込み
memset( buf, 0x00, sizeof(buf) );
sprintf( buf, "syuuryou" );
while( 1 )
{
printf( "Input Send Data:\n" );
memset( Send_Buf, 0x00, sizeof(Send_Buf) );
fgets( Send_Buf, 255, stdin );
SendLen = strlen( Send_Buf );
printf( "Now Sending '%s'+'CR', [%d] bytes ...\n", Send_Buf, SendLen );
Send_Buf[SendLen-1]=0x0d;
Send_Res = write( Send_Fd, Send_Buf, SendLen );
if ( Send_Res != -1 )
{
perror(NULL);
printf( "Sending is over. [%d] bytes.\n", SendLen );
// trans();
}
else
{
perror( "write error!!" );
}
if ( memcmp( Send_Buf, buf, 8 ) == 0 )
{
break;
}
// if ( Send_Buf[0] == 'z' ) break;
}
// シリアルデバイスクローズ
close( Send_Fd );
return(0);
}
int trans(void)
{
// 変数
int Read_Fd, Read_Res;
int ret;
// char Read_Dev[50];
char Read_Buf[255];
char buf[255];
struct termios Read_Newtio;
// int ReadLen;
// 画面消去&タイトル表示
system( "clear" );
fprintf( stdout, "************ Com Read Program ************\n\n" );
#if 0
printf( "\nInput the Read Port Name : " );
fgets( Read_Dev, 50, stdin );
Read_Dev[strlen( Read_Dev )-1] = 0;
#endif
// シリアルデバイスオープン(/dev/ttyS0)
Read_Fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK );
if ( Read_Fd < 0 )
{
perror( "/dev/ttyS0" );
//exit( -1 );
}
// 通信条件設定用構造体クリアー
memset( &Read_Newtio, 0x00, sizeof( Read_Newtio ) );
// 通信速度はBAUDRATE,データビットは8ビット,ストップビットは1ビット,パリティ無し,フロー制御無し
Read_Newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
Read_Newtio.c_iflag = IGNPAR | ICRNL;
Read_Newtio.c_oflag = 0;
Read_Newtio.c_lflag = ICANON;
// バッファ内残留データを消去
tcflush( Read_Fd, TCIFLUSH );
// シリアル通信条件設定
if ( tcsetattr( Read_Fd, TCSANOW, &Read_Newtio ) != 0 )
{
fprintf( stderr, "tcsetattr Error ! \n" );
exit( -1 );
}
memset( buf, 0x00, sizeof(buf) );
sprintf( buf, ">syuuryou" );
printf("now reading...\n");
// データ読みだし
while ( 1 )
{
memset( Read_Buf, 0x00, sizeof(Read_Buf) );
printf("aaaaaaaaaaaa\n");
Read_Res = read( Read_Fd, Read_Buf, 255);
perror(NULL);
printf("bbbbbbbbbbb\n");
if ( Read_Res == -1 )
{
perror(NULL);
fprintf( stderr, "Read ERROR ! \n" );
//exit( -1 );
}
if ( Read_Res == 0){
printf("read success!\n");
exit(-1);
}
Read_Buf[Read_Res] = 0;
ret = memcmp( Read_Buf, buf, 9 );
if( ret == 0 )
{
break;
}
// fprintf( stdout, "%d\n", ret );
fprintf( stdout, "%s", Read_Buf );
// if ( Read_Buf[0] == 'z' ) break;
}
// シリアルデバイスクローズ
close( Read_Fd );
return(0);
}
int main(){
send();
//trans();
}
上のプログラムの//データ読み出しのところのperroe(NULL)にリソースは一時的に利用できませんとでます。
|