Beispiel #1
0
int InitDXL(int deviceIndex, int baudnum ) {
  if(dxl_hal_open(deviceIndex, GetBaudRate(baudnum)) == 0) {
    return 0;
  }

  return 1;
}
Beispiel #2
0
void CSerialConfig::OnApply485() 
{
	if (!UpdateData(TRUE))
	{
		return;
	}
	m_nChanSel = m_chlnosel.GetCurSel();
	if(bGet485Serial[m_nChanSel])
	{
		decodercfg[m_nChanSel].dwSize = sizeof(NET_DEV_DECODERCFG);
		decodercfg[m_nChanSel].dwBaudRate = GetBaudRate(m_baud485sel.GetCurSel());
		decodercfg[m_nChanSel].byDataBit = GetDatabit(m_databit485sel.GetCurSel());
		decodercfg[m_nChanSel].byStopBit = m_stopbit485sel.GetCurSel();
		decodercfg[m_nChanSel].byParity = m_checkbit485sel.GetCurSel();
		decodercfg[m_nChanSel].byFlowcontrol = m_flow485sel.GetCurSel();
		decodercfg[m_nChanSel].wDecoderType = m_decodetypesel.GetCurSel();
		decodercfg[m_nChanSel].wDecoderAddress = m_decodeAddress;
		TRACE("m_nChanSel = %d m_decodeaddr = %d", m_nChanSel, m_decodeAddress);
		bSetSerial485[m_nChanSel] = TRUE;
	}
	for(int i=0; i < int(m_Device->Info.byChanNum); i++)
	{
		if (bSetSerial485[m_nChanSel])
		{
			int m_nChanNum = i;
			if(!CLIENT_SetDevConfig(m_Device->LoginID, DEV_SET_DECODERCFG, m_nChanNum,&decodercfg[i], sizeof(NET_DEV_DECODERCFG), CONFIG_WAITTIME))
			{		
				AfxMessageBox(ConvertString("rs485 config save failed!"));		
				return;
			}
		}	
	}
}
void Cedrus::XIDDevice::ConnectToMpod(unsigned char mpod, unsigned char action)
{
    if (!m_config->IsXID2())
        return;

    int rate = 4;

    if (action > 0)
    {
        m_baudRatePriorToMpod = GetBaudRate();
        // m-pods operate at 19200 only
        SetBaudRate(1);
    }
    else
    {
        switch (m_baudRatePriorToMpod)
        {
        case 9600:
            rate = 0;
            break;
        case 19200:
            rate = 1;
            break;
        case 38400:
            rate = 2;
            break;
        case 57600:
            rate = 3;
            break;
        case 115200:
            rate = 4;
            break;
        default:
            break;
        }
    }

    static unsigned char ctm_cmd[4] = {'a','q'};
    ctm_cmd[2] = '0' + mpod;
    ctm_cmd[3] = '0' + action;

    DWORD bytes_written = 0;
    m_xidCon->Write(ctm_cmd, 4, &bytes_written);

    SLEEP_FUNC(150 * SLEEP_INC);

    m_podHostConfig = (action == 0 ? nullptr : m_config);
    MatchConfigToModel(-1);

    m_curMinorFwVer = GetMinorFirmwareVersion();

    if (action == 0)
        SetBaudRate(rate);
}
Beispiel #4
0
int CommSetSpeed(int handle, unsigned long speed)
{
    struct termios tio;

    if (handle < 0 || handle >= MAX_PORTS)
        return -1;

    tcgetattr(HANDLE(handle).fd, &tio);
    cfsetospeed(&tio, GetBaudRate(speed));
    tcsetattr(HANDLE(handle).fd, TCSANOW, &tio);

    return 0;
}
Beispiel #5
0
int CConfig::GetVarAsBaudRate( const char * p_szVarName, int * p_pnValue, int p_nPos )
{
     char szBaudRate[16];
     if( !GetVar( p_szVarName, szBaudRate, sizeof(szBaudRate), p_nPos ) )
          return 0;

     int  nBaudRate = GetBaudRate( szBaudRate );
     if( nBaudRate == -1 )
          return 0;

     *p_pnValue = nBaudRate;
     return 1;
}
Beispiel #6
0
void CSerialConfig::OnApply232() 
{
	if (!UpdateData(TRUE))
	{
		return;
	}
	
	if(bGet232Serial)
	{		
		rs232cfg.dwSize = sizeof(NET_DEV_RS232CFG);
		rs232cfg.dwBaudRate = GetBaudRate(m_baud232sel.GetCurSel());
		rs232cfg.byDataBit = GetDatabit(m_databit232sel.GetCurSel());
		rs232cfg.byStopBit = m_stopbit232sel.GetCurSel();
		rs232cfg.byParity = m_checkbit232sel.GetCurSel();
		rs232cfg.byFlowcontrol = m_flow232sel.GetCurSel();
		rs232cfg.dwWorkMode = m_operatemodesel.GetCurSel();

		if(0 == m_operatemodesel.GetCurSel())
		{
			if(m_verifypsw != m_userpsw)
			{
				AfxMessageBox(MSG_SERIALCFG_PSWCHECKERROR);
				return ;
			}
			rs232cfg.struPPPConfig.byPPPMode = m_PPPmodesel.GetCurSel();
			rs232cfg.struPPPConfig.byRedialMode = m_dbmodesel.GetCurSel();
			rs232cfg.struPPPConfig.byRedial = (BYTE)m_bDialBack;
			rs232cfg.struPPPConfig.byDataEncrypt = (BYTE)m_bEncrypt;
			memcpy(rs232cfg.struPPPConfig.sUsername,m_username,NAME_LEN);
			memcpy(rs232cfg.struPPPConfig.sPassword, m_userpsw, PASSWD_LEN);
			memcpy(rs232cfg.struPPPConfig.sRemoteIP, m_remoteIP, 16);
			memcpy(rs232cfg.struPPPConfig.sLocalIP, m_localIP, 16);
			memcpy(rs232cfg.struPPPConfig.sLocalIPMask, m_IPmask, 16);
			memcpy(rs232cfg.struPPPConfig.sTelephoneNumber, m_phoneNo, PHONENUMBER_LEN);
		}
	}
//	bSetSerial232 = TRUE;
	if(!CLIENT_SetDevConfig(m_Device->LoginID, DEV_SET_RS232CFG, 0, &rs232cfg, sizeof(NET_DEV_RS232CFG), CONFIG_WAITTIME))
	{
		AfxMessageBox(ConvertString("rs232 config save failed!"));	
		return;
	}
	
}
/// Entry point for a CANLibDemo application
/// @param[in]    hInstance         Handle to current instance
/// @param[in]    hPrevInstance     Handle to previous instance
/// @param[in]    lpCmdLine         Pointer to command line
/// @param[in]    nCmdShow          Show state of window
/// @retval       0                 Successful exit
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
    DWORD gpioInterruptNumber = 0;      ///< Interrupt GPIO
    DWORD device = 0;                   ///< Instance number
    DWORD versionMajor, versionMinor, buildNumber;  ///< Major, minor, build number
    DWORD procType = 0;                 ///< Processer type
    UINT32 canBaudRate = 0;             ///< Keeps CAN baud rate
    BOOL successStatus = TRUE;          ///< Keeps success status
    DWORD selectedDevice = 0 ;          ///< Select the CAN1/2 in Apalis, In colibri it is always CAN (i.e device=0).
    UNREFERENCED_PARAMETER(hInstance);
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    UNREFERENCED_PARAMETER(nCmdShow);

    CANLibMCP2515GetLibVersion(&versionMajor, &versionMinor, &buildNumber);

    printf("Start CAN Test using CanLib V%d.%d.%d \n\r", versionMajor, versionMinor, buildNumber);
    printf("***************************************** \n\r");
    
    fflush(stdin);
    // choosing Apalis CAN0 or CAN1
    if(getModuleFamily()==MODULE_FAMILY_APALIS)
    {
        printf("Select CAN you want to use:\n");
        printf("1 ---->  CAN1(Upper Connector) \n"  );  
        printf("2 ---->  CAN2(Bottom Connector) \n"  );
        printf("Choose the option(1/2) and press enter:");
        scanf_s("%d", &selectedDevice);
        printf("\n");
        switch (selectedDevice){
            case 1:
                device = 0;       ///< choose the CAN 1
                break;  
            case 2:
                device = 1;       ///< choose the CAN 2
                break;
            default:
                printf("Wrong entry, selecting CAN1 !\n");
                device = 0; 
            }
         }
    else
    {
            device = 0;    // In case of Colibri
    }


    successStatus = GetInterruptGpio(device, &gpioInterruptNumber, &procType);

    if (successStatus)
    {
        /// Initalizes CAN Chip
        if (0 != CANLibMCP2515Init(device, gpioInterruptNumber, WATER_MARK, BUFFER_SIZE, 0))
        {
            printf("InitMcp2515() failed (See serial debug output for details)\r\n");
            successStatus = FALSE;
        }
    }
    if (successStatus)
    {
        successStatus = CANLibMCP2515WriteDescriptor(device, CAN_DESCRIPTOR, CAN_MASK);
    }

    if (successStatus)
    {
        GetBaudRate(&canBaudRate);
    }

    if (successStatus)
    {

        successStatus = CANLibMCP2515SetBaudrate(device, canBaudRate);
    }
    if (successStatus)
    {
       GetMode(device);
    }
    fflush(stdin);

    if (!successStatus)
    {
        printf("\nError!!!\n");
    }

    printf("\nPress Enter key to exit!\n\n");

    getchar();

    /// De initialises CAN Chip
    CANLibMCP2515DeInit(device);

    return 0;
}
int main()
{
    // Initialize PortHandler Structs
    // Set the port path
    // Get methods and members of PortHandlerLinux or PortHandlerWindows
    int port_num = PortHandler(DEVICENAME);

    // Initialize PacketHandler Structs 
    PacketHandler();

    int dxl_comm_result = COMM_TX_FAIL;             // Communication result

    UINT8_T dxl_error = 0;                          // Dynamixel error
    UINT8_T dxl_baudnum_read;                       // Read baudnum

    // Open port
    if (OpenPort(port_num))
    {
        printf("Succeeded to open the port!\n");
    }
    else
    {
        printf("Failed to open the port!\n");
        printf("Press any key to terminate...\n");
        _getch();
        return 0;
    }

    // Set port baudrate
    if (SetBaudRate(port_num, BAUDRATE))
    {
        printf("Succeeded to change the baudrate!\n");
    }
    else
    {
        printf("Failed to change the baudrate!\n");
        printf("Press any key to terminate...\n");
        _getch();
        return 0;
    }

    // Read present baudrate of the controller
    printf("Now the controller baudrate is : %d\n", GetBaudRate(port_num));

    // Try factoryreset
    printf("[ID:%03d] Try factoryreset : ", DXL_ID);
    FactoryReset(port_num, PROTOCOL_VERSION, DXL_ID, OPERATION_MODE);
    if ((dxl_comm_result = GetLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
    {
        printf("Aborted\n");
        PrintTxRxResult(PROTOCOL_VERSION, dxl_comm_result);
        return 0;
    }
    else if ((dxl_error = GetLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
        PrintRxPacketError(PROTOCOL_VERSION, dxl_error);

    // Wait for reset
    printf("Wait for reset...\n");
    msecSleep(2000);

    printf("[ID:%03d] FactoryReset Success!\n", DXL_ID);

    // Set controller baudrate to Dynamixel default baudrate
    if (SetBaudRate(port_num, FACTORYRST_DEFAULTBAUDRATE))
    {
        printf("Succeed to change the controller baudrate to : %d\n", FACTORYRST_DEFAULTBAUDRATE);
    }
    else
    {
        printf("Failed to change the controller baudrate\n");
        printf("Press any key to terminate...\n");
        _getch();
        return 0;
    }

    // Read Dynamixel baudnum
    dxl_baudnum_read = Read1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_PRO_BAUDRATE);
    if ((dxl_comm_result = GetLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
        PrintTxRxResult(PROTOCOL_VERSION, dxl_comm_result);
    else if ((dxl_error = GetLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
        PrintRxPacketError(PROTOCOL_VERSION, dxl_error);
    else
        printf("[ID:%03d] DXL baudnum is now : %d\n", DXL_ID, dxl_baudnum_read);

    // Write new baudnum
    Write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_PRO_BAUDRATE, NEW_BAUDNUM);
    if ((dxl_comm_result = GetLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
        PrintTxRxResult(PROTOCOL_VERSION, dxl_comm_result);
    else if ((dxl_error = GetLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
        PrintRxPacketError(PROTOCOL_VERSION, dxl_error);
    else
        printf("[ID:%03d] Set Dynamixel baudnum to : %d\n", DXL_ID, NEW_BAUDNUM);

    // Set port baudrate to BAUDRATE
    if (SetBaudRate(port_num, BAUDRATE))
    {
        printf("Succeed to change the controller baudrate to : %d\n", BAUDRATE);
    }
    else
    {
        printf("Failed to change the controller baudrate\n");
        printf("Press any key to terminate...\n");
        _getch();
        return 0;
    }

    msecSleep(200);

    // Read Dynamixel baudnum
    dxl_baudnum_read = Read1ByteTxRx(port_num, PROTOCOL_VERSION, DXL_ID, ADDR_PRO_BAUDRATE);
    if ((dxl_comm_result = GetLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
        PrintTxRxResult(PROTOCOL_VERSION, dxl_comm_result);
    else if ((dxl_error = GetLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
        PrintRxPacketError(PROTOCOL_VERSION, dxl_error);
    else
        printf("[ID:%03d] Dynamixel Baudnum is now : %d\n", DXL_ID, dxl_baudnum_read);

    // Close port
    ClosePort(port_num);

    return 0;
}
Beispiel #9
0
int CommOpen(const char *device_name, 
                         unsigned long speed,
                         unsigned data_bits,
                         char parity,
                         unsigned stop_bits)
{
    struct termios tio;
    int i = -1, fd = -1;

    Trapdoor();

    if (!CheckParity(parity) || !CheckBaudRate(speed) || !CheckStopBits(stop_bits))
        return -1;

    /* open port */
    fd = open(device_name, O_RDWR|O_NOCTTY|O_NONBLOCK);
    if (fd < 0) 
    {
#if defined(_DEBUG_SERIAL_)
        CsDebug(1, (1, "CommOpen: open failed"));
#endif
        return -1;
    }

#if defined(_DEBUG_SERIAL_)
    CsDebug(1, (1, "CommOpen: opened %s (fd:%d)", device_name, fd));
#endif

    /* find a handle */
    for (i = 0; i < MAX_PORTS; i++)
    {
        if (HANDLE(i).fd == -1)
            break;
    }

    if (i == MAX_PORTS)
        return -1;

    HANDLE(i).fd = fd;
    
    /* don't wory about these for now */
    //HANDLE(i).baud       = GetBaudRate(speed);
    //HANDLE(i).parity     = GetParity(parity);
    //HANDLE(i).stop_bits  = GetStopBits(stop_bits);
    //HANDLE(i).data_bits  = GetWordLen(data_bits);

    /* setup baudrate and other communication port configuration */
    //newtio.c_cflag = HANDLE(i).baud       |
    //                 HANDLE(i).parity     |
    //                 HANDLE(i).stop_bits  |
    //                 HANDLE(i).data_bits  |
    //                 CLOCAL|CREAD|PARENB;
    /* setup baudrate and other communication port configuration */

    /* get current attributes */
    tcgetattr(fd, &tio);

    /* set input/ouput baud rate */
    cfsetispeed(&tio, GetBaudRate(speed));
    cfsetospeed(&tio, GetBaudRate(speed));

    /* reset current databits, parity and stopbits settings */
    tio.c_cflag &= ~(CSIZE | PARENB | PARODD | CSTOPB);
    /* set new settings */
    tio.c_cflag |= (GetWordLen(data_bits) | GetParity(parity) | GetStopBits(stop_bits) | CREAD | CLOCAL);

    tio.c_iflag     = 0;
    tio.c_oflag     = 0;
    tio.c_lflag     = 0;
    tio.c_cc[VMIN]  = 1;
    tio.c_cc[VTIME] = 0;

    /* write out the attributes */
    tcsetattr(fd, TCSAFLUSH, &tio);

    CsDebug(1, (1, "CommOpen: opened handle %d(%d)", i, fd));

    return i;
}