Click here to Skip to main content
15,913,854 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralMemory leak Pin
lvantin5-Apr-05 18:22
lvantin5-Apr-05 18:22 
GeneralRe: Memory leak Pin
Christian Graus5-Apr-05 18:37
protectorChristian Graus5-Apr-05 18:37 
GeneralRe: Memory leak Pin
ThatsAlok5-Apr-05 19:00
ThatsAlok5-Apr-05 19:00 
GeneralRe: Memory leak Pin
S. Senthil Kumar5-Apr-05 22:40
S. Senthil Kumar5-Apr-05 22:40 
GeneralRe: Memory leak Pin
David Crow6-Apr-05 2:04
David Crow6-Apr-05 2:04 
GeneralRe: Memory leak Pin
lvantin7-Apr-05 15:14
lvantin7-Apr-05 15:14 
GeneralRe: Memory leak Pin
David Crow7-Apr-05 16:06
David Crow7-Apr-05 16:06 
GeneralPlease help with serial communication using Visual C++ Pin
Anonymous5-Apr-05 18:18
Anonymous5-Apr-05 18:18 
Hey all,

I'd like to start by saying that I am an electrical engineering student and have therefore had very little experience with Visual C++. However, a part of my senior design project requires a simple rs232 interface in visual c++. My design partner and I have designed the hardware and software needed to implement a traction control system for an R/C car. The traction system works fine, but the other part of our project is wireless transmission of the traction status and speed of the 4 wheels of the car to a laptop the user will have with them. The wireless hardware has been designed and built and is sending the traction status and wheel speed measurements formatted in bytes in the following manner:

0h10, traction status, 0h20, front left wheel speed, 0h30, front right wheel speed, 0h40, rear left wheel speed, 0h50, rear right wheel speed, and then back to 0h10, ...

(where the wheels speeds are also just byte values)

I pieced together what I could find online to write a simple windows program that would display this data using the 0h10, 0h20, ..., .. qualifiers to indicate what the next byte value received will represent (right front, left, etc).

In the program I assume that I read 1 byte at a time from the serial buffer and immediately process it before the next byte arrives which should be very possible since we are using a baud rate of only 2400 bps. I simply set a flag using the qualifiers to indicate what the next byte of data will represent. This method seems like it should work, but Im not sure if I am actually reading 1 byte from the serial buffer corrently or not.

The program compiles and initially displays the current vehicle wheel speeds and traction status but then fails to update these values at a usable rate. If after the program starts any wheel speed changes, the program doesn't register the change in incoming serial data for nearly a minute and a half. I tested the incoming serial data using a regular terminal program and the serial data changes instantly in response to vehicle wheel speed changes. Its as if the data on the screen is updated at a very slow rate and windows just stores all the incoming serial data until it finishes displaying what arrived earlier. I noticed using the windows task manager that the program continues to use more memory the entire time it is running which would reinforce my hypothesis that it is storing the incoming serial data, or there could be just a memory leak I am unaware of. I believe the cause of this to be related to the way I am reading the serial buffer using Cserial. Although like I said earlier, I dont have much experience with visual c++.

I have pasted below the code I am currently using. I know some of you are experts in visual c++ that can give me a simple explanation and/or fix to my problem. I am very close, as like I said, the serial data is read and displayed correctly when the program starts, it just fails to update using the incoming data at anything other than minute or two intervals which makes no sense to me.

Thanks for any and all suggestions.

Jordan

---------------------
Visual C++ Code
---------------------

#include <string>
#include <cstring>
#include <stdlib.h>
#include <afx.h>
#include "SerialWnd.h"
#include "Resource.h"

using namespace std;

HDC hdc ;
DCB dcb;
HANDLE hCom;
char buff[2];
char buffout[2];

HDC MemDCExercising;
PAINTSTRUCT Ps;
HBITMAP bmpExercising;
HINSTANCE hInst;

BOOL fSuccess;
unsigned long dwBytesRead = 0;
BYTE abBuffer[100];

char *pcCommPort = "COM1";
DWORD dwCommEvent;
DWORD dwMask;
DWORD inqueue;
DWORD outqueue;

int TractionData=0;
int WLF=0;
int WLR=0;
int WRF=0;
int WRR=0;

char fleft[26] = "Front Left Wheel Speed: ";
char fright[27] = "Front Right Wheel Speed: ";
char rleft[25] = "Rear Left Wheel Speed: ";
char rright[26] = "Rear Right Wheel Speed: ";

char tstatus[19] = "Traction Status: ";

CSerialWnd serial;

LRESULT CALLBACK Handler(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam){

if (iMsg == WM_PAINT)

{

hdc = BeginPaint(hWnd, &Ps);
// Load the bitmap from the resource
bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
// Create a memory device compatible with the above DC variable
MemDCExercising = CreateCompatibleDC(hdc);
// Select the new bitmap
SelectObject(MemDCExercising, bmpExercising);
// Copy the bits from the memory DC into the current dc
BitBlt(hdc, 0, 0, 782, 522, MemDCExercising, 0, 0, SRCCOPY);
// Restore the old bitmap
DeleteDC(MemDCExercising);
DeleteObject(bmpExercising);
EndPaint(hWnd, &Ps);

hdc = GetDC(hWnd);
SetTextColor(hdc, RGB(0,0,0));
//SetBkColor(hdc, RGB(0,0,0));

TextOut(hdc,385,435,fleft,26);
TextOut(hdc,5,175,fright,27);
TextOut(hdc,600,350,rleft,25);
TextOut(hdc,190,100,rright,26);

TextOut(hdc,330,30,tstatus,19);

}


if (iMsg == CSerialWnd::mg_nDefaultComMsg)

{
// A serial message occurred
const CSerialWnd::EEvent eEvent = CSerialWnd::EEvent(LOWORD(wParam));
const CSerialWnd::EError eError = CSerialWnd::EError(HIWORD(wParam));

switch (eEvent)
{
case CSerialWnd::EEventRecv:

hdc = GetDC(hWnd);
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(0,0,0));

serial.Read(abBuffer,1,&dwBytesRead,0,INFINITE);

sprintf(buffout,"%X",(LPCTSTR)abBuffer[0]);
SetTextColor(hdc, RGB(255,255,255));
//TextOut(hdc,0,80,buffout,2);

// Sets flags based on qualifiers

if ((buffout[0] == '1') && (buffout[1] == '0')) {

TractionData = 1;
break;

}

if ((buffout[0] == '2') && (buffout[1] == '0')) {
WLF = 1;
break;

}

if ((buffout[0] == '3') && (buffout[1] == '0')) {
WLR = 1;
break;

}

if ((buffout[0] == '4') && (buffout[1] == '0')) {
WRF = 1;
break;

}


if ((buffout[0] == '5') && (buffout[1] == '0')) {
WRR = 1;
break;

}

// Displays data based on flags

if (TractionData == 1) {

SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,434,30,buffout,1);
TractionData = 0;
break;

}

if (WLF == 1) {

SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,547,435,buffout,2);
WLF = 0;
break;

}

if (WLR == 1) {

SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,756,350,buffout,2);
WLR = 0;
break;

}

if (WRF == 1) {

SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,177,175,buffout,2);
WRF = 0;
break;

}

if (WRR == 1) {

SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,360,100,buffout,2);
WRR = 0;
break;

}

break;

}

return 0;
}

if (iMsg == WM_DESTROY) {

PostQuitMessage(0);
return 0;
}

return DefWindowProc(hWnd, iMsg, wParam, lParam);

}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow){

//Declare variables
const char* szAppName = "MyWin";
WNDCLASSEX wndclass;
HWND hWnd;
MSG msg;

hInst = hInstance;

wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = Handler;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hWnd = CreateWindow(szAppName, "Wireless Telemetry Monitor", WS_OVERLAPPEDWINDOW,0,0, 782, 522, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);

serial.Open(pcCommPort, hWnd, WM_NULL, 0, inqueue, outqueue);
serial.Setup(CSerial::EBaud2400,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
serial.SetupHandshaking(CSerial::EHandshakeOff);

serial.SetMask(CSerial::EEventRecv);
serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);


while (GetMessage(&msg, NULL,0,0)){

TranslateMessage(&msg);
DispatchMessage(&msg);


}
return msg.wParam;
}
GeneralListing down available domains programmatically Pin
alex12055-Apr-05 17:40
alex12055-Apr-05 17:40 
GeneralGraphic stuff Pin
LiYS5-Apr-05 15:22
LiYS5-Apr-05 15:22 
GeneralSetItemData, GetItemData Pin
elephantstar5-Apr-05 15:19
elephantstar5-Apr-05 15:19 
GeneralRe: SetItemData, GetItemData Pin
Christian Graus5-Apr-05 16:06
protectorChristian Graus5-Apr-05 16:06 
GeneralRe: SetItemData, GetItemData Pin
DasdaDAS5-Apr-05 21:01
DasdaDAS5-Apr-05 21:01 
GeneralRe: SetItemData, GetItemData Pin
elephantstar6-Apr-05 8:14
elephantstar6-Apr-05 8:14 
GeneralRe: SetItemData, GetItemData Pin
toxcct5-Apr-05 21:54
toxcct5-Apr-05 21:54 
GeneralRe: SetItemData, GetItemData Pin
Michael Dunn6-Apr-05 2:03
sitebuilderMichael Dunn6-Apr-05 2:03 
Generalneed help with a simple MFC question Pin
stuckupfool5-Apr-05 14:20
stuckupfool5-Apr-05 14:20 
GeneralRe: need help on the program data structure Pin
Christian Graus5-Apr-05 13:07
protectorChristian Graus5-Apr-05 13:07 
GeneralRe: need help on the program data structure Pin
Anonymous6-Apr-05 9:32
Anonymous6-Apr-05 9:32 
GeneralRe: need help on the program data structure Pin
Cedric Moonen5-Apr-05 20:13
Cedric Moonen5-Apr-05 20:13 
GeneralRe: need help on the program data structure Pin
toxcct5-Apr-05 20:56
toxcct5-Apr-05 20:56 
Generalemessaging client Pin
jessiesrage5-Apr-05 11:28
jessiesrage5-Apr-05 11:28 
GeneralRe: emessaging client Pin
David Crow5-Apr-05 11:39
David Crow5-Apr-05 11:39 
GeneralRe: emessaging client Pin
ThatsAlok5-Apr-05 18:16
ThatsAlok5-Apr-05 18:16 
GeneralChanging ComboBox selection by text editing Pin
Ultimate Newbie5-Apr-05 10:53
sussUltimate Newbie5-Apr-05 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.