rbei-etas / busmaster

BUSMASTER is an Open Source Software tool to simulate, analyze and test data bus systems such as CAN. BUSMASTER was conceptualized, designed and implemented by Robert Bosch Engineering and Business Solutions (RBEI). Presently it is a joint project of RBEI and ETAS GmbH.
http://rbei-etas.github.com/busmaster/
GNU General Public License v3.0
932 stars 496 forks source link

How to interact with other hardware? #1168

Open Teddyz opened 6 years ago

Teddyz commented 6 years ago

It would be nice to be able to use peripherals that are connected via serial port in a node simulation. This way I could control relays to interact with the nodes I test. Or is IO-piggy supported?

I run Busmaster on Windows.

Teddyz commented 6 years ago

If this can be solved by 'simply' invoking a dll, please give me an example to start with. :)

Teddyz commented 5 years ago

Okay, I found a web page that explained how to use serial port I a way that I could understand. The code below is an example I made where you can use serial port to control a relay "ICSE013A". 'int8_t relay (int8_t rel, int8_t state) {

static bool firsttime = true;
static bool r1 = false;
static bool r2 = false;
static char ComPortName[] = "\\\\.\\COM19"; // default name
static HANDLE hComm;
char sendchar[8];

if ( rel == -1 ) {
    if ( hComm == INVALID_HANDLE_VALUE )
        return -1;
    CloseHandle(hComm);//Closing the Serial Port
    firsttime = true;
    return 0;
}
if ( firsttime || hComm == INVALID_HANDLE_VALUE ) {
    // open serial port
    if ( rel == 0)
        snprintf ( ComPortName, 10, "\\\\.\\COM%d", state);
    hComm = CreateFile(ComPortName, //port name
        GENERIC_WRITE, //  |GENERIC_READ, //Read/Write
        0,                            // No Sharing
        NULL,                         // No Security
        OPEN_EXISTING,// Open existing port only
        0,            // Non Overlapped I/O
        NULL);        // Null for Comm Devices
    if ( hComm == INVALID_HANDLE_VALUE )
        return -2;

    DCB dcbSerialParams = { 0 }; // Initializing DCB structure
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

    if ( !GetCommState(hComm, &dcbSerialParams) ) //retreives  the current settings
        return -3;

    dcbSerialParams.BaudRate = CBR_9600;      // Setting BaudRate = 9600
    dcbSerialParams.ByteSize = 8;             // Setting ByteSize = 8
    dcbSerialParams.StopBits = ONESTOPBIT;    // Setting StopBits = 1
    dcbSerialParams.Parity = NOPARITY;        // Setting Parity = None 

    if ( !SetCommState(hComm, &dcbSerialParams) )  //Configuring the port according to settings in DCB 
        return -4;

    COMMTIMEOUTS timeouts = { 0 };
    timeouts.ReadIntervalTimeout         = 50;
    timeouts.ReadTotalTimeoutConstant    = 50;
    timeouts.ReadTotalTimeoutMultiplier  = 10;
    timeouts.WriteTotalTimeoutConstant   = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;

    if (SetCommTimeouts(hComm, &timeouts) == FALSE)
        return -5;

    // send init
    if ( !WriteFile(hComm, // Handle to the Serialport
    "\x50", // Data to be written to the port 
    1,   // No of bytes to write into the port
    0,  // No of bytes written to the port
    NULL) )
        return -6;
    Sleep ( 2 );
    if ( !WriteFile(hComm, // Handle to the Serialport
    "\x51", // Data to be written to the port 
    1,   // No of bytes to write into the port
    0,  // No of bytes written to the port
    NULL) )
        return -6;
    Sleep ( 2 );
    firsttime = false;
}
if ( rel == 1 || rel == 3 ) {
    if ( state )
        r1 = true;
    else
        r1 = false;
}
if ( rel == 2 || rel == 3 ) {
    if ( state )
        r2 = true;
    else
        r2 = false;
}
snprintf ( sendchar, 8, "%d", (r2<<1) | r1 );
if ( !WriteFile(hComm, // Handle to the Serialport
sendchar, // Data to be written to the port 
1, // No of bytes to write into the port
0, // No of bytes written to the port
NULL) )
    return -7;
//Trace ( " relay is done, rel=%d state = %d", rel, state );
return 0;

}'