cteni ze serioveho portu...(delsi)

Zdenek Rulc scipraha na applet.cz
Pondělí Duben 12 14:55:17 CEST 1999


OK, ponekud jsem vykuchal programek, nebot je v nem spousta smeti navic.
Dale jsem napsal protikus, ktery zda se funguje. Prelozeno na SuSE 5.3,
jadro 2.0.35, gcc-2.7.2.1-6.

Vypada to nasledovne:  pgm <----> /dev/cua1 -----kablik-----/dev/cua0<----->sensor
pgm a sensor jsou programky, jenz jsou v priloze.

Kablik vypada takto:      oba konektory cannon 9pin maminka

                                     1 ------              ------  1
                                                 |             |
                                     2 ------|---------|------ 3
                                                 |             |
                                     3 ------|---------|------ 2
                                                 |             |
                                     4 ------o            o----- 4
                                                 |             |
                                     5 ------|---------|----   5
                                                 |             |
                                     6 ------              ------ 6

                                     7 ----                     ---- 7
                                             |                     |
                                     8 ----                     ----  8

                                     9                                   9



------------- další část ---------------

#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <termio.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
extern errno;


#define DELKA		30		
#define PORT		"/dev/cua1"
#define RATE		B9600        

#define COJE_01  	"\x05\x89\x01\x20\x01\x00\xab\xfe"

static int desk;

int CtiLinku(int dsk, unsigned char *t)
{
	*(t)=0x00;
	*(t+1)=0xfe;

	if ((read(dsk,t,DELKA)) == -1)
	{
		perror("linka nejde cist");
		exit(1);
	};

	return(0);
};


int JakDlouhe( unsigned char pole[DELKA])
{
	int i;

	for(i=0;pole[i] != 0xfe; i++);
	return(i+1);
};

void PosliTel( int dsk, unsigned char co[DELKA])
{
	int zpatky;

	if ((zpatky = write (dsk, co,JakDlouhe(&co[0]) )) == -1 )
	{
		perror("nejde psat");
	};
};



void Tiskni (unsigned char *fr)
{
	int i;
	for( i=0; *fr != 0xfe; *fr++,i++)
	{
		if (i>17)
			break;
		printf("%02x ", *fr);

	};
	printf("%s \n", "fe");
};

void main(void)
{

	int i,How,Znova=20;
	unsigned char *frame, ble[30];
	struct termio LinePar;
        struct timeval rtime;
        fd_set rset;


frame=&ble[0];

	if ( ( desk = open ( PORT, O_RDWR | O_EXCL ) ) == -1 )
	{
		perror("Linka se neotevrela");
		return ;
	};

	LinePar.c_iflag = IGNBRK ;
	LinePar.c_oflag = 0 ;
	LinePar.c_cflag = RATE | CS8 | CREAD | CLOCAL ;
	LinePar.c_lflag = 0 ;
	LinePar.c_line = 0 ;
	LinePar.c_cc [VMIN] = 0 ;
	LinePar.c_cc [VTIME] = 1 ;
	if ( ioctl ( desk, TCSETA, &LinePar ) == -1 )
	{
		perror("Linka nejde nastavit");
		return ;
	};
while (1)
{
sleep (1);
  rtime.tv_sec=0;
  rtime.tv_usec=500000;
  FD_ZERO(&rset);
  FD_SET(desk,&rset);


  PosliTel(desk,COJE_01);
  printf("%s ", "POC>");
  Tiskni(COJE_01);
  How = select (FD_SETSIZE,&rset,NULL,NULL,&rtime);
  //printf("How>%d\n",How);

  if ( How == 0 )
   {
     if( Znova > 0)
      {  
        printf("%s %d\n", "Nejak neodpovida, zkusime to jeste ", Znova);
        Znova --;
       }
      else
       {
         printf("Koncime sensor je mrtev\n");
	 close(desk);
         exit (-1);
       }
   }
  else
   {
      Znova=20;
      CtiLinku(desk,frame);
      printf("%s ", "SEN>");
      Tiskni(frame);
    }

 }

}
------------- další část ---------------

#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <termio.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
extern errno;


#define DELKA		30		
#define PORT		"/dev/cua0"
#define RATE		B9600        



#define ODP  	"\x05\x09\x22\x12\x76\xaa\xad\xfe"


static int desk;
int CtiLinku(int dsk, unsigned char *t)
{
	*(t)=0x00;
	*(t+1)=0xfe;

	if ((read(dsk,t,DELKA)) == -1)
	{
		perror("linka nejde cist");
		exit(1);
	};

	return(0);
};


int JakDlouhe( unsigned char pole[DELKA])
{
	int i;

	for(i=0;pole[i] != 0xfe; i++);
	return(i+1);
};

void PosliTel( int dsk, unsigned char co[DELKA])
{
	int zpatky;

	if ((zpatky = write (dsk, co,JakDlouhe(&co[0]) )) == -1 )
	{
		perror("nejde psat");
	};
};



void Tiskni (unsigned char *fr)
{
	int i;
	for( i=0; *fr != 0xfe; *fr++,i++)
	{
		if (i>17)
			break;
		printf("%02x ", *fr);

	};
	printf("%s \n", "fe");
};

void main(void)
{

	int i,How,Znova=3;
	unsigned char *frame, ble[30];
	struct termio LinePar;
        struct timeval rtime;
        fd_set rset;


frame=&ble[0];

	if ( ( desk = open ( PORT, O_RDWR | O_EXCL ) ) == -1 )
	{
		perror("Linka se neotevrela");
		return ;
	};

	LinePar.c_iflag = IGNBRK ;
	LinePar.c_oflag = 0 ;
	LinePar.c_cflag = RATE | CS8 | CREAD | CLOCAL ;
	LinePar.c_lflag = 0 ;
	LinePar.c_line = 0 ;
	LinePar.c_cc [VMIN] = 0 ;
	LinePar.c_cc [VTIME] = 1 ;
	if ( ioctl ( desk, TCSETA, &LinePar ) == -1 )
	{
		perror("Linka nejde nastavit");
		return ;
	};
while (1)
{
  rtime.tv_sec=0;
  rtime.tv_usec=3000000;
  FD_ZERO(&rset);
  FD_SET(desk,&rset);


  How = select (FD_SETSIZE,&rset,NULL,NULL,&rtime);

  if ( How == 0 )
   {
      printf("%s\n", "Nic neprislo - cekame dal");
   }
  else 
   {
      CtiLinku(desk,frame);
      printf("%s ", "Vyzva>");
      Tiskni(frame);
   usleep(200000);
      PosliTel(desk,ODP);
      printf("%s ", "Odpoved>");
      Tiskni(ODP);
    }
   }
}


Další informace o konferenci Linux