I got my serial port working properly with a few unexpected values. As a plus, I now have it as a USB device too.

Just so you know. GENERIC_READ_WRITE (what you wrote as a correction) doesn't seem to workk. It gave me an error on runtime and I checked the microsoft msdn site and GENERIC_READ | GENERIC_WRITE does seem to be correct if you want to use both.

The FTDI serial-to-USB chip I have has some LEDs hooked up to tx and rx. Using these LEDs I can verify that the code works (or at least sends data to the right com port) I still have not hooked it up to a PIC with LCD to verify the right baud rate or anything, but it's a start.


Code:
// Simple COM1 test program
// my first try in LiteC
// I supose that some things should be done in a better way
// but after reading the LiteC WorkShop 2 it's enaugh for me!
//
// Autor: Juan Ignacio Odriozola
//
// sorry for my bad english!
// 
// Open (or try to) the Communication Port 1
// Sends some characters "Hello !!" twice
// and waits for characters to arrive
// Escape finish the program

#include <acknex.h>
#include <default.c>
#include <windows.h>

typedef struct {
  int DCBlength;
  int BaudRate;
  int Flags;
  short wReserved;
  short XonLim;
  short XoffLim;  
  char ByteSize; 
  char Parity; 
  char StopBits;
  char XonChar; 
  char XoffChar; 
  char ErrorChar;
  char EofChar; 
  char EvtChar; 
  short wReserved1; 
} DCB;

DCB LPDCB;

typedef struct { 
  int ReadIntervalTimeout; 
  int ReadTotalTimeoutMultiplier; 
  int ReadTotalTimeoutConstant; 
  int WriteTotalTimeoutMultiplier; 
  int WriteTotalTimeoutConstant; 
} COMMTIMEOUTS;

COMMTIMEOUTS LPCommTO;

char TxBuffer[8];
char RxChar;
char TxChar;
int TxCount;
int NumRx;
int NumTx;

#define  GENERIC_READ_WRITE 0xC0000000;

long hPort;

var a = 0;
var b = 0;
var c = 0;
var d = 7;

FONT* arial_font = "Arial#26b";

/////////////////////////////////////////////////////////////////////

PANEL* panDisplay =
{
	digits(35, 08, "Handle     = %0.f", arial_font, 1, a);
	digits(35, 48, "BaudRate   = %0.f", arial_font, 1, b);
	digits(35, 88, "LastRxChar = %0.f", arial_font, 1, c);
	digits(35, 128, "DCBlength  = %0.f", arial_font, 1, d);
	flags = VISIBLE;
}

/////////////////////////////////////////////////////////////////////

function main()
{
	video_mode = 7;
	screen_color.blue = 100;
	
	
	LPDCB.DCBlength = sizeof(LPDCB);
	d = sizeof(LPDCB);
	// COM1, COM2, as you need
   hPort =  CreateFile ("COM3", 			// Pointer to the name of the port
                      GENERIC_READ | GENERIC_WRITE,
                                    	// Access (read-write) mode
                      0,            	// Share mode
                      NULL,         	// Pointer to the security attribute
                      OPEN_EXISTING,	// How to open the serial port
                      0,            	// Port attributes
                      NULL);        	// Handle to port with attribute to copy
   a = hPort;
   
   if (hPort != 0)
   {
		SetupComm(hPort,1024,1024);	// sets de buffers for Tx and Rx
		
		GetCommState(hPort,&LPDCB);	// get the state of the device
										// modify some parameters
		LPDCB.BaudRate = 1200;	// 300, 600, 1200 and so on
		//b = LPDCB.BaudRate;
		LPDCB.Flags = 1;			// no hardware, no software handshake
		LPDCB.ByteSize = 8;		// 8 bits for each char
		LPDCB.Parity = 0;			// 0=none, should be odd, even, mark, space
		LPDCB.StopBits = 1;		// depending on ByteSize should be 1, 1.5 or 2
		
		SetCommState(hPort,&LPDCB);
		
		GetCommState(hPort,&LPDCB);	// just for verify that the device acepted de BaudRate
		b=LPDCB.BaudRate;
		
		GetCommTimeouts(hPort, &LPCommTO);		// this function fills LPCommTO structure
	   LPCommTO.ReadTotalTimeoutConstant = 5;	// just modify one of the time out's
	   SetCommTimeouts(hPort, &LPCommTO);		// set the read time out, wait no more than 5 ms for
	   													// a char to arrive
	   
	   
	   
	   //Commented because I am testing the transmit in a loop below.
	   TxBuffer[0]=str_to_asc("H");
//		TxBuffer[1]=str_to_asc("e");
//	   TxBuffer[2]=str_to_asc("l");
//	   TxBuffer[3]=str_to_asc("l");
//	   TxBuffer[4]=str_to_asc("o");
//	   TxBuffer[5]=str_to_asc(" ");
//	   TxBuffer[6]=str_to_asc("!");
//	   TxBuffer[7]=str_to_asc("!");
//	   
//	   TxCount = 1;
//	   
//	   // some examples about how to write
//	   // one by one
//	   var i;
//	   for (i=0; i<8; i++)
//		   {
//	   	TxChar = TxBuffer[i];
//		   WriteFile (hPort,	// Port handle
//   			&TxChar,			// Pointer to the data to write 
//      		1,					// Number of bytes to write
//        		&NumTx,			// Pointer to the number of bytes written
//        		NULL				// Must be NULL for Windows CE
//				);
//			}
//			
//		//four in one call from offset 0
//		WriteFile( hPort, &TxBuffer[0], 4, &NumTx, NULL);
//		//four in one call from offset 4
//		WriteFile( hPort, &TxBuffer[4], 4, &NumTx, NULL);
		
	}
	
	while (1)
	{
		
		// send this data over and over to make sure there is a connection and a stable one
		
	   	TxChar = TxBuffer[0];
		   WriteFile (hPort,	// Port handle
   			&TxChar,			// Pointer to the data to write 
      		1,					// Number of bytes to write
        		&NumTx,			// Pointer to the number of bytes written
        		NULL				// Must be NULL for Windows CE
				);
			
		//four in one call from offset 0
		WriteFile( hPort, 77, 4, &NumTx, NULL);		//  <-- not sure if 77 shows up, but it does sent some data
		
		
		
		
		
		///   where is the bracket set for the following if? or doesn't it need it in lite C code
		
		if (hPort != 0)
		// waits no more than 5 ms for a character arrival
		ReadFile (hPort,          // Port handle
          &RxChar,		        // Pointer to data to read
          1,                    // Number of bytes to read
          &NumRx,					  // Pointer to number of bytes read
          NULL                  // Must be NULL for Windows CE
					);
		
		if (NumRx != 0)
			// if one was received then show it
			c=RxChar;
		
		
		//WriteFile( hPort, &TxBuffer[0], 8, &NumTx, NULL);
		
		if(key_esc == 1)				//   <--- this probably also needs to recognize any termination of the program
		{
			if (hPort != 0)
			CloseHandle(hPort);	// don't forget to close the port!
		}
		wait (100);			//   <-- long enough to see the LED blink
		
	}
}




I had to change mine to com3 for my device. This could probably have a string dependant on what com port the user chooses, same with the baud rate.




I noticed a few things though.
1. under createfile you have 0 in the port attributes, I don't see that as an option on the msdn website, but I would assume it's because it's connecting to hardware and not a file itself

2. every time I connect I get a different number under "a" the Handle why is this

3. It has 9600 listed as the baud in the code, but 1200 is what shows up in the text panel.

4. what is the DBClength and why is it 28, does it need to be that number?


I just thought you'd want to know the progress, especially if you haven't done much with it yet.

Also, to anyone wanting to get into serial communication with your own device, I really would suggest looking into those FTDI chips. The sparkfun breakout board for it is so freaking easy, you don't even have to solder on that tiny little chip.

This is the one I used it seems to be out of stock.
this one should work too. and they're cheap too.


Black holes are where God divided by zero.