Here is my Serial code which runs on Windows, written in C.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
HANDLE SerialHandle;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
char serialBuffer = 123;
DWORD dwBytesRead = 0;
//int n = 0;
SerialHandle = CreateFile("COM3",GENERIC_WRITE,0,0,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,0);
if (SerialHandle == INVALID_HANDLE_VALUE)
{
if(GetLastError() == ERROR_FILE_NOT_FOUND)
{
printf("Serial port doesnt exist\n");
}
printf("Some other error");
}
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if(!GetCommState(SerialHandle, &dcbSerialParams))
{
printf("Error getting state\n");
}
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(SerialHandle,&dcbSerialParams))
{
printf("Error getting serial port state\n");
}
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(SerialHandle, &timeouts))
{
printf("Error Occured setting timeouts\n");
}
while(1)
{
if(!WriteFile(SerialHandle,serialBuffer,1,&dwBytesRead,NULL))
{
printf("Write error occured\n");
}
}
return 0;
}
When this code is compiled, I get a few warnings. They are shown below.
E:\Programming\serialcom\main.c||In function 'main'

E:\Programming\serialcom\main.c|62|warning: passing argument 2 of 'WriteFile' makes pointer from integer without a cast|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\winbase.h|2033|note: expected 'PCVOID' but argument is of type 'char'|
||=== Build finished: 0 errors, 1 warnings ===|
And this is my Arduino Code.
char serialData;
void setup()
{
Serial.begin(9600);
Serial.println("Serial test starting");
}
void loop()
{
if (Serial.available() > 0)
{
serialData = Serial.read();
Serial.print("This is what you entered: ");
Serial.println(serialData);
}
}
When I run both applications, these are my observations.
1. Regardless of whether the Arduino Serial Monitor is open or not, I always get a "Write Error" i.e. the windows app couldnt write to the serial port.
2. Regardless of which COM port I enter, (Arduino was on COM3) I entered COM 6, 8 etc. It still gave the "Write Error" which means the windows app didnt check to see if it was an existing port or not.
3. As I run my windows serial app, the "L" (activity?) LED and "TX" LED on the Arduino blink for a few miliseconds and then dont blink after that. (this happens regardless of whether the serial monitor is open or not.