Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include<process.h>


int main()
{

// Defining hexadecimal bytes to be sent a 200 bps for generation of low high sequence//
unsigned char bytes_to_send[1];
bytes_to_send[0] = 0xF0; /* 11110000b */
unsigned char databytes_send[5];
databytes_send[0] = 0x81;
databytes_send[1] = 0x11;
databytes_send[2] = 0xF1;
databytes_send[3] = 0x11;
databytes_send[4] = 0x04;


// 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(
"COM4", GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hSerial == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Check for port connections\n");
return 1;
}
else
fprintf(stderr, "OK\n");
// Set device parameters (200 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;
}

// Set baudrate to send bytes at 200 bps//
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 = 00;
timeouts.ReadTotalTimeoutConstant = 00;
timeouts.ReadTotalTimeoutMultiplier = 00;
timeouts.WriteTotalTimeoutConstant = 00;
timeouts.WriteTotalTimeoutMultiplier = 00;
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 = 0;
fprintf(stderr, "sending bytes...\n");

while(1)
{


//Transmit slow initialization data
if (!ReadFile(hSerial, bytes_to_send, 1,&bytes_written, NULL))

// break;

fprintf(stderr, "%d bytes written\n", bytes_written);

//check for key press event
if (GetPressedKey())
break;
}

// Set baudrate to send bytes at 10400 bps 8 N 1//

dcbSerialParams.BaudRate = 10400;
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 = 00;
timeouts.ReadTotalTimeoutConstant = 00;
timeouts.ReadTotalTimeoutMultiplier = 00;
timeouts.WriteTotalTimeoutConstant = 00;
timeouts.WriteTotalTimeoutMultiplier = 00;
if(SetCommTimeouts(hSerial, &timeouts) == 0)
{
fprintf(stderr, "Error setting timeouts\n");
CloseHandle(hSerial);
return 1;
}

while(1)
{


//Transmit fast initialization data
if (!WriteFile(hSerial, databytes_send, 5, &bytes_written, NULL))

// break;

fprintf(stderr, "%d bytes written\n", bytes_written);

break;
}

CloseHandle(hSerial);

if(!WriteFile(hSerial, bytes_written,6, &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
[no name] 24-Apr-15 6:31am    
Well you have reposted it once more.
vinod b v 24-Apr-15 6:36am    
yes i am having a problem with this code now. I need just a loop so that the data moves continously i tried while but with no success.i am totally confused with this now
[no name] 24-Apr-15 8:41am    
The whole thing looks whacky. There is no handshaking here so what is the other device up to? What is switching it? Just tell us what are you trying to achieve?
vinod b v 24-Apr-15 9:30am    
I want to send data to USB to serial converter and implement a wakeup pattern.send F0 at 200 bps and then send rest 5 bytes of data. i need to loop it continously.
[no name] 24-Apr-15 9:51am    
So how do you know the other device is receiving at 200. You aren't listening for a response. What will put it to 200? Why not a break?

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