|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <windows.h>
|
|
|
|
int main()
|
|
{
|
|
// Declare variables and structures
|
|
const char scilabCSV[] = "csvData.csv";// Scilab csv file name
|
|
const char *delim = ";";
|
|
char buff1[1024]; //buff2[1024];
|
|
char *dataSend;// *dataReceive;
|
|
//static unsigned int write_counter=0, read_counter=0;
|
|
|
|
HANDLE hSerial;
|
|
DCB dcbSerialParams = {0};
|
|
COMMTIMEOUTS timeouts = {0};
|
|
|
|
/* opening file for reading */
|
|
FILE *fp = fopen(scilabCSV, "r");
|
|
|
|
if(fp == NULL)
|
|
{
|
|
perror("Error opening file");
|
|
return 1;
|
|
}
|
|
rewind(fp);
|
|
|
|
// Open the highest available serial port number
|
|
fprintf(stderr, "Opening serial port...");
|
|
hSerial = CreateFile(
|
|
"\\\\.\\COM3", //port name
|
|
GENERIC_READ|GENERIC_WRITE, //Read/Write
|
|
0, // No Sharing
|
|
NULL, // No Security
|
|
OPEN_EXISTING, // Open existing port only
|
|
FILE_ATTRIBUTE_NORMAL, // Non Overlapped I/O
|
|
NULL ); // Null for comm Devices
|
|
|
|
if (hSerial == INVALID_HANDLE_VALUE)
|
|
{
|
|
fprintf(stderr, "Error\n");
|
|
return 2;
|
|
}
|
|
else fprintf(stderr, "OK\n");
|
|
|
|
// Set device parameters (115200 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); //Closing the Serial Port
|
|
return 3;
|
|
}
|
|
|
|
dcbSerialParams.BaudRate = CBR_115200;
|
|
dcbSerialParams.ByteSize = 8;
|
|
dcbSerialParams.StopBits = ONESTOPBIT;
|
|
dcbSerialParams.Parity = NOPARITY;
|
|
if(SetCommState(hSerial, &dcbSerialParams) == 0)
|
|
{
|
|
fprintf(stderr, "Error setting device parameters\n");
|
|
CloseHandle(hSerial);
|
|
return 4;
|
|
}
|
|
|
|
// 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 5;
|
|
}
|
|
|
|
// Send specified text (remaining command line arguments)
|
|
DWORD bytes_written=0; //total_bytes_written = 0;
|
|
fprintf(stderr, "Sending bytes...");
|
|
|
|
while(fgets(buff1, sizeof(buff1), fp))
|
|
{
|
|
if(feof(fp))
|
|
break;
|
|
|
|
dataSend = strtok(buff1, delim);
|
|
while(dataSend != NULL)
|
|
{
|
|
WriteFile(hSerial, // Handle to the Serial port
|
|
dataSend, // Data to be written to the port
|
|
sizeof(dataSend), //No of bytes to write /*write_counter++*/
|
|
&bytes_written, //Bytes written
|
|
NULL);
|
|
|
|
//ReadFile(hSerial, // Handle to the Serial port
|
|
//dataReceive, // Data to be read to the port
|
|
//read_counter++, //No of bytes to read
|
|
//&bytes_written, //Bytes written
|
|
//NULL);
|
|
|
|
if(!WriteFile(hSerial,dataSend, sizeof(dataSend+1024),&bytes_written, NULL))
|
|
{
|
|
fprintf(stderr, "Error\n");
|
|
CloseHandle(hSerial);
|
|
return 6;
|
|
}
|
|
dataSend = strtok(NULL, delim);
|
|
}
|
|
}
|
|
|
|
fprintf(stderr, "%ld bytes written\n", bytes_written);
|
|
|
|
// Close serial port
|
|
fprintf(stderr, "Closing serial port...");
|
|
|
|
if (CloseHandle(hSerial) == 0)
|
|
{
|
|
fprintf(stderr, "Error\n");
|
|
return 7;
|
|
}
|
|
|
|
fprintf(stderr, "OK\n");
|
|
fclose(fp);
|
|
|
|
// exit normally
|
|
return 0;
|
|
}
|