I have written a code that sends the byte OF at 200 bps and then should send 5 bytes at 10400, i need to loop this data continuously, how can this be done? i use two while loop one for sending single byte at 200 and then again another for sending 5 bytes of data at 10400, when i start the program i can see the OF being transmitted and only when i press a key, i see the 5 bytes being transferred, i want all these in a single shot. How to do it. Here is the attached code.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <process.h>
#define true 1
int main()
{
unsigned char bytes_to_send[1];
bytes_to_send[0] = 0xF0;
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;
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
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");
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;
}
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;
}
while(true)
{
DWORD bytes_written = 0;
fprintf(stderr, "sending bytes...\n");
while(1)
{
if (!WriteFile(hSerial, bytes_to_send, 1,&bytes_written, NULL))
fprintf(stderr, "%d bytes written\n", bytes_written);
}
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error getting device state\n");
CloseHandle(hSerial);
return 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;
}
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;
}
while(1)
{
if (!WriteFile(hSerial, databytes_send, 5, &bytes_written, NULL))
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);
fprintf(stderr, "Closing serial port...");
if (CloseHandle(hSerial) == 0)
{
fprintf(stderr, "Error\n");
return 1;
}
fprintf(stderr, "OK\n");
return 0;
}
}