Esempio n. 1
0
int CDeviceRed::SendFrame(BYTE *pBuffer, DWORD pCant)
{
	DWORD	Event;
	SetParity(MARKPARITY);
	if (SendBuffer(pBuffer,1)!=0)
		AfxMessageBox("Error enviando datos");
	if (WaitCommEvent(PortHandle,&Event,NULL)==0)
		AfxMessageBox("Huye que la cosa esta fea");
	SetParity(SPACEPARITY);
	if (SendBuffer(pBuffer+1,pCant-1)!=0)
		AfxMessageBox("Error enviando datos");
	if (WaitCommEvent(PortHandle,&Event,NULL)==0)
		AfxMessageBox("Huye que la cosa esta fea");
	return true;
}
Esempio n. 2
0
/**
 *@breif     main()
 */
int main(int argc, char **argv)
{
    int fd;
    int nread;
    char buff[512], readbuff[32];

    if (argc < 2) {
        printf("Usage: %s devicefile \n", *argv);
        exit(1);
    }

    char *dev = *(argv + 1);

    printf("Device: %s \n", dev);
    fd = OpenDev(dev);
    printf("Device opened\n");

    if (fd > 0) {
        printf("Begin set speed\n");
        SetSpeed(fd, 9600);
    } else {
        printf("Can't Open Serial Port!\n");
        exit(0);
    }

    printf("Begin set parity\n");
    if (SetParity(fd,8,1,'N') == FALSE) {
        printf("Set Parity Error\n");
        exit(1);
    }

    sleep(3); // wait for Arduino

    while(1) {
        char w[] = "hello$";
        int x = write(fd, w, 6);
        if (x == -1) {
            perror("Can't write to device");
        }

        int buffi=0;
        int tempi=0;

        while((nread = read(fd,readbuff, 32)) > 0) {
            for (tempi=0; tempi<nread; tempi++) {
                buff[buffi++] = readbuff[tempi];
            }
        }

        buff[buffi]='\0';
        printf("Reply:%s\n",buff);
    }
    //close(fd);
    //exit(0);
}
Esempio n. 3
0
void CSerialPDD::SetDefaultConfiguration()
{
    // Default Value. Can be altered.
    m_CommPorp.wPacketLength       = 0xffff;
    m_CommPorp.wPacketVersion      = 0xffff;
    m_CommPorp.dwServiceMask       = SP_SERIALCOMM;
    m_CommPorp.dwReserved1         = 0;
    m_CommPorp.dwMaxTxQueue        = 16;
    m_CommPorp.dwMaxRxQueue        = 16;
    m_CommPorp.dwMaxBaud           = BAUD_115200;
    m_CommPorp.dwProvSubType       = PST_RS232;
    m_CommPorp.dwProvCapabilities  =
        PCF_DTRDSR | PCF_RLSD | PCF_RTSCTS |
        PCF_SETXCHAR |
        PCF_INTTIMEOUTS |
        PCF_PARITY_CHECK |
        PCF_SPECIALCHARS |
        PCF_TOTALTIMEOUTS |
        PCF_XONXOFF;
    m_CommPorp.dwSettableBaud      =
        BAUD_075 | BAUD_110 | BAUD_150 | BAUD_300 | BAUD_600 |
        BAUD_1200 | BAUD_1800 | BAUD_2400 | BAUD_4800 |
        BAUD_7200 | BAUD_9600 | BAUD_14400 |
        BAUD_19200 | BAUD_38400 | BAUD_56K | BAUD_128K |
        BAUD_115200 | BAUD_57600 | BAUD_USER;
    m_CommPorp.dwSettableParams    =
        SP_BAUD | SP_DATABITS | SP_HANDSHAKING | SP_PARITY |
        SP_PARITY_CHECK | SP_RLSD | SP_STOPBITS;
    m_CommPorp.wSettableData       =
        DATABITS_5 | DATABITS_6 | DATABITS_7 | DATABITS_8;
    m_CommPorp.wSettableStopParity =
        STOPBITS_10 | STOPBITS_20 |
        PARITY_NONE | PARITY_ODD | PARITY_EVEN | PARITY_SPACE |
        PARITY_MARK;
    // Setup m_DCB.
    
    // Set Detault Parameter.
    SetOutputMode(FALSE, TRUE );    // No IR.
    // For DCB. The PDD only need to take care BaudRate, ByteSize Parity & StopBit
    m_DCB.DCBlength  = sizeof(DCB);
    SetBaudRate(m_DCB.BaudRate   = 9600,FALSE);
    SetByteSize(m_DCB.ByteSize   = 8);
    SetParity(m_DCB.Parity     = NOPARITY);
    SetStopBits(m_DCB.StopBits   = ONESTOPBIT);
    
}
Esempio n. 4
0
int InitPort(int port_num, int baudrate,
             int databits, int stopbits, int parity)
{
	int fd;

	fd = OpenPort(port_num);

	if(fd == -1) {
		return -1;
	}

	SetSpeed(fd, baudrate);

	if(SetParity(fd, databits, stopbits, parity) == FALSE) {
		DEBUG(DL_ERROR, "Set Parity Error!\n");
		ClosePort(fd);
		return -1;
	}

	return fd;
}
Esempio n. 5
0
BOOL CSerialPDD::SetDCB(LPDCB lpDCB)
{
    BOOL bReturn = TRUE;
    if (IsOpen() && lpDCB && lpDCB->DCBlength == sizeof(DCB)) {
        
        m_HardwareLock.Lock();
        DCB LocalDCB = *lpDCB;
        if (lpDCB->BaudRate != m_DCB.BaudRate) {
            if (lpDCB->BaudRate == 0 || !SetBaudRate(lpDCB->BaudRate, m_fIREnable)) {
                LocalDCB.BaudRate = m_DCB.BaudRate ;
                bReturn = FALSE;
            }
        }
        if (lpDCB->ByteSize != m_DCB.ByteSize ) {
            if (!SetByteSize(lpDCB->ByteSize))  {
                LocalDCB.ByteSize = m_DCB.ByteSize;
                bReturn = FALSE;
            }
        }
        if (lpDCB->Parity != m_DCB.Parity) {
            if (!SetParity(lpDCB->Parity)) {
                LocalDCB.Parity = m_DCB.Parity ;
                bReturn=FALSE;
            }
        }
        if (lpDCB->StopBits != m_DCB.StopBits) {
            if (!SetStopBits(lpDCB->StopBits)) {
                LocalDCB.StopBits = m_DCB.StopBits;
                bReturn = FALSE;
            }
        }
        m_DCB = LocalDCB;
        m_HardwareLock.Unlock();        
    }
    else
        bReturn = FALSE;
    return bReturn;
};
Esempio n. 6
0
int	MakeAuthenticationKeyFile_V1(B2B_AUTH_INFO_T *AuthInfo)
{
    int ret = NO_ERROR;

    char    DeviceInfoBinSizeBuff[B2B_DEVINFOBIN_SIZE+1];
    char    AgencyIDSizeBuff[B2B_DEVINFOBIN_SIZE+1];
    char    SolutionIDSizeBuff[B2B_DEVINFOBIN_SIZE+1];

    //	Begin Setting AuthInfo buffers... to make security key file
    AuthInfo->RandomNumber = random() % (B2B_RANDOM_FILTER-1) + 1;
    AuthInfo->Version = B2B_AUTHENTICATION_VERSION_V1;

    if((ret = SetAuthTime(AuthInfo->AuthTimeBuff, AuthInfo->AuthTime, AuthInfo->ExpireDate)) != NO_ERROR)	{
        return	ERROR_UNABLE_CREATEKEYFILE;
    }
    if((ret = SetExpireDate(AuthInfo->ExpireDateBuff, AuthInfo->ExpireDate)) != NO_ERROR)	{
        return	ERROR_UNABLE_CREATEKEYFILE;
    }
    if((ret = SetRandomNumber(AuthInfo->RandomBuff, AuthInfo->RandomNumber)) != NO_ERROR)	{
        return	ERROR_UNABLE_CREATEKEYFILE;
    }
    if((ret = SetParity(AuthInfo->ParityBuff, AuthInfo->AuthTimeBuff, AuthInfo->ExpireDateBuff, AuthInfo->ProductID, AuthInfo->RandomNumber)) != NO_ERROR)	{
        return	ERROR_UNABLE_CREATEKEYFILE;
    }
    if((ret = SetAgencyNSolutionID_V1(AuthInfo->AgencyIDBuff, AuthInfo->AgencyID, AuthInfo->AgencyIDParityBuff, AuthInfo->AgencyIDLength)) != NO_ERROR)	{
        return	ERROR_UNABLE_CREATEKEYFILE;
    }
    sprintf(AgencyIDSizeBuff, "%02d", AuthInfo->AgencyIDLength);
    if((ret = SetAgencyNSolutionID_V1(AuthInfo->SolutionIDBuff, AuthInfo->SolutionID, AuthInfo->SolutionIDParityBuff, AuthInfo->SolutionIDLength)) != NO_ERROR)	{
        return	ERROR_UNABLE_CREATEKEYFILE;
    }
    sprintf(SolutionIDSizeBuff, "%02d", AuthInfo->SolutionIDLength);
    sprintf(DeviceInfoBinSizeBuff, "%03d", AuthInfo->DeviceInfoSize);

    SetEncriptBufferWithN28(AuthInfo->EncriptKeyBuff, B2B_SECURITY_KEYFILE_SIZE);

//	printf("BUFF(%s)(_%d_)\n", AuthInfo->EncriptKeyBuff, __LINE__);
    //	AgencyID
    memcpy(AuthInfo->EncriptKeyBuff+B2B_AGENCYID_POSITION_V1, AuthInfo->AgencyIDBuff, strlen(AuthInfo->AgencyIDBuff));
//	printf("BUFF(%s)(_%d_)AgencyID(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->AgencyIDBuff);
    //	SolutionID
    memcpy(AuthInfo->EncriptKeyBuff+B2B_SOLUTION_POSITION_V1, AuthInfo->SolutionIDBuff, strlen(AuthInfo->SolutionIDBuff));
//	printf("BUFF(%s)(_%d_)SolutionID(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->SolutionIDBuff);
    //	AgencyIDLength
    memcpy(AuthInfo->EncriptKeyBuff+B2B_AGENCYSIZE_POSITION_V1, AgencyIDSizeBuff, B2B_AGENCYSIZE_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)AgencyIDSize(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AgencyIDSizeBuff);
    //	SolutionIDLength
    memcpy(AuthInfo->EncriptKeyBuff+B2B_SOLUTIONSIZE_POSITION_V1, SolutionIDSizeBuff, B2B_SOLUTIONSIZE_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)SolutionIDSize(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, SolutionIDSizeBuff);
    //	AgencyID Parity
    memcpy(AuthInfo->EncriptKeyBuff+B2B_AGENCYPARITY_POSITION_V1, AuthInfo->AgencyIDParityBuff, B2B_AGENCYSOLUTION_PARITY_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)AgencyIDParity(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->AgencyIDParityBuff);
    //	Solution Parity
    memcpy(AuthInfo->EncriptKeyBuff+B2B_SOLUTIONPARITY_POSITION_V1, AuthInfo->SolutionIDParityBuff, B2B_AGENCYSOLUTION_PARITY_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)SolutionIDParity(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->SolutionIDParityBuff);
    //	Authentication Time
    memcpy(AuthInfo->EncriptKeyBuff+B2B_AUTHTIME_POSITION_V1, AuthInfo->AuthTimeBuff, B2B_AUTHTIME_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)AuthTime(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__,  AuthInfo->AuthTimeBuff);
    //	ExpireDateTime
    memcpy(AuthInfo->EncriptKeyBuff+B2B_EXPIREDATE_POSITION_V1, AuthInfo->ExpireDateBuff, B2B_EXPIREDATE_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)ExpireDate(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->ExpireDateBuff);
    //	ProductID
    memcpy(AuthInfo->EncriptKeyBuff+B2B_PRODUCTID_POSITION_V1, AuthInfo->ProductID, B2B_PRODUCTID_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)ProductID(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->ProductID);
    //	Random
    memcpy(AuthInfo->EncriptKeyBuff+B2B_RANDOM_POSITION_V1, AuthInfo->RandomBuff, B2B_RANDOM_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)Random(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->RandomBuff);
    //	Parity
    memcpy(AuthInfo->EncriptKeyBuff+B2B_DATETIMEPARITY_POSITION_V1, AuthInfo->ParityBuff, B2B_DATETIMEPARITY_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)Parity(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->ParityBuff);
    //	Version
    memcpy(AuthInfo->EncriptKeyBuff+B2B_VERSION_POSITION_V1, &(AuthInfo->Version), B2B_VERSION_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)Version(%c)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->Version);
    //	DeviceInfo.bin
    memcpy(AuthInfo->EncriptKeyBuff+B2B_DEVICEINFO_POSITION_V1, AuthInfo->DeviceInfo, AuthInfo->DeviceInfoSize);
//	printf("BUFF(%s)(_%d_)DeviceInfo(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, AuthInfo->DeviceInfo);
    //	DeviceInfoSize
    memcpy(AuthInfo->EncriptKeyBuff+B2B_DEVICEINFOSIZE_POSITION_V1, DeviceInfoBinSizeBuff, B2B_DEVICEINFOSIZE_ENCSIZE_V1);
//	printf("BUFF(%s)(_%d_)DeviceInfoSize(%s)\n", AuthInfo->EncriptKeyBuff, __LINE__, DeviceInfoBinSizeBuff);

    SetCheckSum(AuthInfo->EncriptKeyBuff);

    return	NO_ERROR;
}