Serial Port Communiocation

In the code below I am effectively only reading from a serial port(although a writing handle is also implemented). I got the main part of this code from some template someone posted. For one, the part below "This is very slow" is how I "debug", in the sense that I'm writing whatever comes on the serial port and makes sense to a file. Now my BaudRate is set to 57600 bps and the message length is set to 10 Bytes, which should render approx 57600/10/8=720 messages per second. I'm nowhere near that. Is that because of my debugging technique. how can I make it faster? Secondly I attached a copy of the output. Notice the discrepancies, some garbage at the beginning, all values listed twice, empty lines except the begining or the end. Any explanations? I'm kinda new at this, is there a better way to go about it?

Thanks,
Remus

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>
using namespace std;
#include <stdio.h>
#include <Windows.h>
#include <conio.h>
#include <fstream>

int main(int argc, char* argv[])
{
    char INBUFFER[500];
    char OUTBUFFER[200];
	char testBuffer[]="ABC";
    DWORD        bytes_read    = 0;    // Number of bytes read from port
    DWORD        bytes_written = 0;    // Number of bytes written to the port
    HANDLE       comport      = NULL;  // Handle COM port
    int   bStatus;
    DCB          comSettings;          // Contains various port settings
    COMMTIMEOUTS CommTimeouts;
	ofstream WriteToFile;
    strcpy(&OUTBUFFER[0], "The quick brown fox jumped over the lazy dog. \n\r\0");
	
	char* msg_rec,msg_good;
	char header[1000],buf[500];
	int i=1;
    static char* msg_prev="\0";

	// Open COM port
    if ((comport = 
         CreateFile("\\\\.\\COM7",                // open com7:
                    GENERIC_READ | GENERIC_WRITE, // for reading and writing
                    0,                            // exclusive access
                    NULL,                         // no security attributes
                    OPEN_EXISTING,              
                    FILE_ATTRIBUTE_NORMAL,
                    NULL)) == INVALID_HANDLE_VALUE)
    {
        // error processing code goes here
    }
    // Set timeouts in milliseconds
    CommTimeouts.ReadIntervalTimeout         = 0; 
    CommTimeouts.ReadTotalTimeoutMultiplier  = 0; 
    CommTimeouts.ReadTotalTimeoutConstant    = 100;
    CommTimeouts.WriteTotalTimeoutMultiplier = 0;
    CommTimeouts.WriteTotalTimeoutConstant   = 100;
    bStatus = SetCommTimeouts(comport,&CommTimeouts);
    if (bStatus != 0)
    {
        // error processing code goes here
    }

    // Set Port parameters.
    // Make a call to GetCommState() first in order to fill
    // the comSettings structure with all the necessary values.
    // Then change the ones you want and call SetCommState().
    GetCommState(comport, &comSettings);
    comSettings.BaudRate = 57600;
    comSettings.StopBits = ONESTOPBIT;
    comSettings.ByteSize = 8;
    comSettings.Parity   = NOPARITY;
    comSettings.fParity  = FALSE;
    bStatus = SetCommState(comport, &comSettings);
    if (bStatus == 0)
    {
        // error processing code goes here
    }


    while(!kbhit())
    {

      bStatus = WriteFile(comport,         // Handle
				          &OUTBUFFER,      // Outgoing data
                          15,              // Number of bytes to write
                          &bytes_written,  // Number of bytes written
                          NULL);
        if (bStatus != 0)
        {
            // error processing code here
        }
        bStatus = ReadFile(comport,     // Handle
                           &INBUFFER,   // Incoming data
                           10,         // Number of bytes to read
                           &bytes_read, // Number of bytes read
                           NULL);
        if (bStatus != 0)
        {
            // error processing code goes here
        }

        // code to do something with the data goes here

		msg_rec=&INBUFFER[0];
                // THIS IS VERY SLOW
		{
		WriteToFile.open("Sine.txt",std::ofstream::app);
		unsigned int inMessageLength=10;
		strcat(msg_rec," ");
		WriteToFile.write(msg_rec,inMessageLength);
		WriteToFile.close();
		//Find valid chars only, cut out unusefull data;
		for (j=0;j<inMessageLength;j++)
			if ((int)msg_rec[j]<0)
				break;
		//here I break the message
		msg_rec[j]='\0';
		
    }
    CloseHandle(comport);
	system("PAUSE");
 return 0;
}


Program Output:

ÌÌÌÌÌÌÌÌÌÌ0.02
ÌÌÌÌ0.02
ÌÌ-0.18
Ì-0.18
-0.37
-0.37
-0.55
-0.55
-0.71
-0.71
-0.83
-0.83
-0.93
-0.93
-0.98
-0.98
-1.00
-1.00
-0.98
-0.98
-0.92
-0.92
-0.82
-0.82
-0.69
-0.53
-0.53
-0.35
-0.35
-0.16
-0.16
0.04

0.04

0.24

0.24

0.43

0.43

0.60

0.60

0.75

0.75

0.86

0.86

0.95

0.95

0.99

0.99

1.00

1.00

0.96

0.96

0.89

0.89

0.78

0.78

0.64

0.64

0.48

0.30

0.30

0.10

0.10

-0.10
-0.10
-0.29
-0.29
-0.48
-0.48
-0.64
-0.64
-0.78
-0.78
-0.89
-0.89
-0.96
-0.96
-1.00
-1.00
-0.99
-0.99
-0.95


Last edited on
Topic archived. No new replies allowed.