// 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;
/////////////////////////////////////////////////////////////////////
PANEL* panDisplay =
{
digits(35, 10, "Handle = %0.f", *, 1, a);
digits(35, 19, "BaudRate = %0.f", *, 1, b);
digits(35, 28, "LastRxChar = %0.f", *, 1, c);
flags = VISIBLE;
}
/////////////////////////////////////////////////////////////////////
function main()
{
video_mode = 7;
screen_color.blue = 150;
LPDCB.DCBlength = sizeof(LPDCB);
// COM1, COM2, as you need
hPort = CreateFile ("COM1", // 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 = 9600; // 300, 600, 1200 and so on
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
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)
{
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;
wait (1);
}
if (hPort != 0)
CloseHandle(hPort); // don't forget to close the port!
}