Question : reading and saving from serial port

Hi there,

Im working on a program that reads data (a 1MB text file) from the serial port and saves it to disk. I have two functions - initComm to initialize the serial port and readComm to read data from the port.

I need help in writing the main function that reads the com port and saves the data to disk. I dont have much experience with c and im not too sure if this is right way to do it? maybe i can do the whole program within a main function and not bother with the initComm and readComm functions?

im using a pc running windows 7 and using microsoft vc++ to do the code. The serial connection between the computers is RS232.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
#include <stdio.h>
#include <windows.h>

BOOL fSuccess;
HANDLE hSerial;     //Handle for COM Port

void initComm()
{
COMMTIMEOUTS noblock;
DCB comSettings;	//Various Port Settings

// Initialize Serial Port
hSerial = CreateFile("COM1",        //Open COM1
		     GENERIC_READ,  //Read only
		     0,             //Exclusive Access
	             NULL,          //No Security Attributes
		     OPEN_EXISTING, //COM port already exists
		     0,
	             NULL);
if (hSerial==INVALID_HANDLE_VALUE)
{
	printf("Invalid Handle Value %d.\n", GetLastError());
}
//Set TimeOut in Milisecs
fSuccess = GetCommTimeouts(hSerial, &noblock);
	noblock.ReadTotalTimeoutConstant = 1;
	noblock.ReadTotalTimeoutMultiplier = MAXWORD;
	noblock.ReadIntervalTimeout = MAXWORD;
fSuccess = SetCommTimeouts(hSerial, &noblock);

//Set Port Parameters
fSuccess = GetCommState(hSerial, &comSettings);
if(!fSuccess)
{
	printf("\nGetCommState Error!");
}
	comSettings.BaudRate = 9600;
	comSettings.ByteSize = 8;
	comSettings.fParity = FALSE;
	comSettings.Parity = NOPARITY;
	comSettings.StopBits = ONESTOPBIT;
 fSuccess = SetCommState(hSerial, &comSettings);
 if(!fSuccess){
	 printf("\nSetCommState Error!");
 }
 printf("Comm port set\n");
}

//Read Data from Serial Port
char readComm()
{
	char inBuffer;
	int bytesRead;
	fSuccess = ReadFile(hSerial,    //Handle
			    &inBuffer,  //Incoming data
			    1000,       //No bytes to read
			    &bytesRead, //Bytes Read
		            0);
	if (bytesRead >0)
		return inBuffer;
	else return 0;
}

Answer : reading and saving from serial port

I was playing with it and finally I found the solution:

<%If rs("Red") =1 Then %>
<td align="center" bgcolor="red">&nbsp;</td>
<%Else%>
<td bgcolor="white">&nbsp;</td>
<%End If%>
Random Solutions  
 
programming4us programming4us