?
//*****************************************************************************
//*
//* ComOpen
//*
//*****************************************************************************
// Öffnet eine serielle Verbindung
// uNr : Ist die Nummer des Com-Ports (0=COM1 1=COM2 ...)
// iBaud : Ist die Bautrate
// iParity : 0 = kein iParity Bit (PARITY__NONE)
// 1 = ungerade (PARITY__ODD)
// 2 = gerade (PARITY__EVEN)
// 3 = immer 0 (PARITY__ZERO)
// 4 = immer 1 (PARITY__ONE)
// iStopbits : 0 = Ein Stopbit (STOPBITS_ONE)
// 1 = Zwei iStopbits (STOPBITS_TWO)
// iBits : 0 = 8 Datenbits
// 1 = 7 Datenbits
// 7 = 7 Datenbits (DATABITS_SEVEN)
// 8 = 8 Datenbits (DATABITS_EIGHT)
// iFlags : Some Flags
// 2 = DTR_HANDSHAKE : Switsches on the DTR handshake
// 4 = RTS_HANDSHAKE : Switsches on the RDS handshake
// Ergibt ein Handel wenn eine Schnittstelle geöffnet wurde sonst -1
int ComOpen(unsigned uNr,int iBaud,int iParity,int iStopbits,int iDatabits,int iFlags)
{
struct serial_struct sData;
struct termios sPortInfo;
char cName[]="/dev/ttyS0";
int iHandle,i,iBitsPerByte,iBaudNr;
if(uNr>=MAX_COM_PORTS)return 0;
cName[0]+=uNr;
iHandle=open(cName,O_RDWR|O_NONBLOCK|O_NOCTTY); // Device oeffnen
if(iHandle<0)return 0;
tcgetattr(iHandle, &sPortInfo);
cfmakeraw(&sPortInfo);
sPortInfo.c_cc[VMIN] = 0;
sPortInfo.c_cc[VTIME]= 0;
if(iStopbits!=STOPBITS_ONE) // Stopbits einstellen
sPortInfo.c_cflag |= CSTOPB;
else sPortInfo.c_cflag &= ~CSTOPB;
sPortInfo.c_cflag &= ~CSIZE; // Datenbits einstellen
sPortInfo.c_cflag |= (iDatabits&1)? CS7:CS8;
switch(iParity) // Parity-Bits einstellen
{
case PARITY__NONE: sPortInfo.c_cflag = sPortInfo.c_cflag & ~PARENB; break;
case PARITY__ODD : sPortInfo.c_cflag = sPortInfo.c_cflag | PARENB | PARODD; break;
case PARITY__EVEN: sPortInfo.c_cflag = (sPortInfo.c_cflag | PARENB) & ~PARODD; break;
default: close(iHandle);
return -1;
}
tcsetattr(iHandle,TCSANOW,&sPortInfo);
for(i=0;i<MAX_SPEED;i++)
{
if(aTcSpeeds[i].iBaud==iBaud)break;
}
if(i>=MAX_SPEED)
{
errno=-EFAULT;
close(iHandle);
return -1;
}
ioctl(iHandle,TIOCGSERIAL,&sData); // Spezial-Baudraten löschen
sData.flags &= ~ASYNC_SPD_MASK;
ioctl(iHandle,TIOCSSERIAL,&sData);
iBaudNr=aTcSpeeds[i].iCode;
tcgetattr(iHandle,&sPortInfo);
cfsetospeed(&sPortInfo, iBaudNr);
cfsetispeed(&sPortInfo, iBaudNr);
tcsetattr(iHandle,TCSANOW,&sPortInfo);
i = (iFlags&RTS_ON)? 0:TIOCM_RTS; // RTS DTR ausschalten
i |= (iFlags&DTR_ON)? 0:TIOCM_DTR;
ioctl(iHandle, TIOCMBIC,&i);
i = (iFlags&RTS_ON)? TIOCM_RTS:0; // RTS DTR einschalten
i |= (iFlags&DTR_ON)? TIOCM_DTR:0;
ioctl(iHandle, TIOCMBIS,&i);
iBitsPerByte=10; // calculate time for one byte
if(iDatabits&1)iBitsPerByte--;
if(iStopbits )iBitsPerByte++;
if(iParity!=0 )iBitsPerByte++;
iUsPerByte[uNr]=(1000*1024*iBitsPerByte)/iBaud;
hComFile [uNr]=iHandle;
bIsOpen [uNr]=1;
return iHandle;
}