// 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
}
}