Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <windows.h>
#include <stdio.h>
#include <conio.h>



// INITIALISATION FOR 25MS LOW AND 25MS HIGH WITH PREDEFINED BAUDRATE OF 200 Bps,DATA 0XF0//

int main()
{
int _kbhit();
int _getch();
// Defining hexadecimal bytes//
int bytes_to_send[1];
bytes_to_send[0] = 0xF0;



int GetPressedKey()
{
int ret = 0;
if (_kbhit())
{
ret = _getch();
}
return ret;
}

// Declare variables and structures//
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};

// Open the available serial port number//
fprintf(stderr, "Opening serial port...");
hSerial = CreateFile(
"COM3", GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hSerial == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error\n");
return 1;
}
else
fprintf(stderr, "OK\n");

// Set device parameters (10400 baud, 1 start bit,
// 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error getting device state\n");
CloseHandle(hSerial);
return 1;
}

dcbSerialParams.BaudRate = 200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error setting device parameters\n");
CloseHandle(hSerial);
return 1;
}

// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0)
{
fprintf(stderr, "Error setting timeouts\n");
CloseHandle(hSerial);
return 1;
}

// Send specified text (remaining command line arguments)
DWORD bytes_written, total_bytes_written = 0;
fprintf(stderr, "sending bytes...");





while(1)
{


if (!WriteFile(hSerial, bytes_to_send, 1, &bytes_written, NULL))
break;
fprintf(stderr, "%d bytes written\n", bytes_written);
if (GetPressedKey())
break;
// Read data here


if (GetPressedKey())

break;
}



CloseHandle(hSerial);

if(!WriteFile(hSerial, bytes_to_send, 1, &bytes_written, NULL))
{
fprintf(stderr, "End of transmission\n");
CloseHandle(hSerial);
return 1;
}
fprintf(stderr, "%d bytes written\n", bytes_written);



// Close serial port
fprintf(stderr, "Closing serial port...");
if (CloseHandle(hSerial) == 0)
{
fprintf(stderr, "Error\n");
return 1;
}
fprintf(stderr, "OK\n");

// exit normally
return 0;
}
Posted
Comments
ZurdoDev 23-Mar-15 10:15am    
Where are you stuck?
vinod b v 23-Mar-15 10:35am    
I am not understanding the way how to do it. first part of code works as i want, but once i send 1 byte of data at certain baudrate, later i need to change baudrate and send some more bytes of data to talk to ECU
Sergey Alexandrovich Kryukov 23-Mar-15 10:26am    
Why doing so?
—SA
vinod b v 23-Mar-15 10:32am    
for initialisation purpose
Sergey Alexandrovich Kryukov 23-Mar-15 10:38am    
Are you trying to negotiate boudrate by preliminary communication using some initial boudrate (matching or not?)?
Not sure it's possible in this way...
See also the comment by Jochen Arndt below.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900