RS232Example ()
{
// Declare variables for RS232 communication
char stringToSend[20] = "Hello World !"; // String to send
byte sendBuffer[20]; // Byte array to hold send data
byte receiveBuffer[20]; // Buffer for received data
int i; // Counter variable
// Register the RS232 callback functions
sysvar::VTS::ECUPowerSupply.SerialSetOnErrorHandler("OnRS232Error");
sysvar::VTS::ECUPowerSupply.SerialSetOnReceiveHandler("OnRS232Receive");
sysvar::VTS::ECUPowerSupply.SerialSetOnSendHandler("OnRS232Sent");
// Configure the serial port i.e. for communication with an external
// ECU power supply to 9600 baud, 8 data bits, 1 stop bit, no parity
sysvar::VTS::ECUPowerSupply.SerialConfigure(eVTSBaudRate9600, eVTSDataBitsEight, eVTSStopBitsOne, eVTSParityNone);
// Open the serial port i.e. for communication with an external ECU power supply
sysvar::VTS::ECUPowerSupply.SerialOpen();
// Wait briefly to make sure settings are applied and port is ready
TestWaitForTimeOut(10);
// **** Send data ****
// Copy the string to a byte array and send it
for (i=0; i<strlen(stringToSend); ++i) sendBuffer[i] = stringToSend[i];
sysvar::VTS::ECUPowerSupply.SerialSend(sendBuffer, strlen(stringToSend));
// **** Receive data ****
// For 10s output all received data to the Write Window
Write("Waiting for incoming data...");
sysvar::VTS::ECUPowerSupply.SerialReceive(receiveBuffer, elcount(receiveBuffer));
TestWaitForTimeOut(10000);
// Close the serial port
sysvar::VTS::ECUPowerSupply.SerialClose();
}
void OnRS232Error(dword flags)
{
// Write error details to the Write Window
if((flags & (dword) eVTSSerialErrorsSendOperationFailed) != 0)
{
Write("Error occurred on serial port of VT7001 module: eVTSSerialErrorsSendOperationFailed");
}
if((flags & (dword) eVTSSerialErrorsFrameError) != 0)
{
Write("Error occurred on serial port of VT7001 module: eVTSSerialErrorsFrameError");
}
}
void OnRS232Receive(byte buffer[], dword number)
{
long i;
char string[256];
// Create a string from the given byte array
for(i=0; i<elcount(buffer); ++i)
string[i] = buffer[i];
// Write received data to the Write Window
Write("Received '%s' (%d bytes) on ECU power supply port.", string, number);
}
void OnRS232Sent(byte buffer[], dword number)
{
long i;
char string[256];
// Create a string from the given byte array
for(i=0; i<elcount(buffer); ++i)
string[i] = buffer[i];
// Write sent data to the Write Window
Write("Sent '%s' (%d bytes) on ECU power supply port.", string, number);
}