|
I tried different timeouts and the following work for the first read:
CTimeouts.ReadIntervalTimeout = 20; CTimeouts.ReadTotalTimeoutMultiplier = 10; CTimeouts.ReadTotalTimeoutConstant = 100; CTimeouts.WriteTotalTimeoutMultiplier = 0; CTimeouts.WriteTotalTimeoutConstant = 0;
I also added a loop using ClearCommError to verify that cbInQue has returned before performing ReadFile so was able to do that as soon as data arrived. Everything in that first request-receipt cycle works fine.
I still have the problem that the next WriteFile is essentially ignored, although it is sent without errors. The device appears to be waiting for something after the read was completed before it can respond to whatever is sent on the transmit line.
|
|
|
|
|
Does WriteFile say bytes sent > 0? Do you see data on the wire (a breakout box might be handy)?
Can you connect a PC with a terminal program in place of the device (with a null modem cable) so you can see (and verify) what, if anything is being sent by your program and manually send the data you expect to get. A scenario where you could set break points and see exactly what code is being executed and the value of the variables.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
It ended up that I had to force-transmit a carriage return after the ReadFile to "kick" the device using TransmitCommChar . After the "kick" it responds to the next WriteFile with the appropriate response. Using a second PC was going to be my next course of action.
|
|
|
|
|
First housekeeping when you open a comport you really need to put the colon in ... its "COM1:".
Yes your string works on some versions of Windows but not all, the documentation spells it out
To open a serial port
- Insert a colon after the communication port pointed to with the first parameter, lpzPortName.
For example, specify COM1: as the communication port.
In the ReadFile documentation it spells out the blocking function.
ReadFile function (Windows)[^]
You will have an error which will be a Readtimeout. Why because you set the read timeout to
1 second and execute a read and there won't be any data because you haven't yet sent another
command.
From that moment on you are toast as your code does not ever clear the error condition and
ReadFile will always return with reading zero bytes because you haven't cleared the error.
The big obvious is there isn't even a single GetLastError in your code much less dealing
with the error. The reason the terminal program fixes the problem is at some point they go
and clear the error.
You could try brutal and SetLastError to zero just before the ReadFile .. I don't like it
but it will probably work
While the thread error variable has an error value I am pretty sure that none of the IO
functions will work so I am pretty sure WriteFile will also bail always writing zero
bytes while it's set. It should also return success but the actual bytes written will
always be zero because the fail out is for pre-existing error that hasn't been cleared.
I don't particually like the way you are using the com port but probably the easiest way
to fix you problem is make the ReadFile timeout bail immediately .. so set no timeout on
the read. How to do that is given in the documentation
COMMTIMEOUTS structure (Windows)[^]
Quote => "A value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant
and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return
immediately with the bytes that have already been received, even if no bytes have been
received."
That should stop you getting a read timeout which should fix your problem with your current code
and it getting into a block condition. It just means it will spin around your do loop like a crazy
person rather than slow 1 second pace it currently is.
On the plus side it will be more responsive
However you really need to actually put in code to deal with errors, you set the com timeouts
but do nothing if the timeout or any other error actually occurs.
In vino veritas
modified 28-Sep-16 0:32am.
|
|
|
|
|
Excellent points and suggestions, and I've implemented much of what you suggested. In the end it turned out the device was waiting for a response from the computer to indicate the read was completed, which I accomplished with a TransmitCommChar . Clunky but it works.
|
|
|
|
|
Much thanks to all who made suggestions. I implemented many of them.
In the end, the problem was solved by "kicking" the device with TransmitCommChar(hComm, 13); after the read request.
|
|
|
|
|
If anyone has a minute can they confirm a weird behaviour of _tcscpy_s which caught me totally off guard that it trashes the unused part of the return buffer.
The code I would like you to test follows, you will need include TCHAR.H for the standard _tcscpy_s function
#include <TCHAR.H>
TCHAR szFilter[512];
memset(szFilter, 0, sizeof(szFilter));
_tcscpy_s(szFilter, _countof(szFilter), _T("All files"));
int i = _tcslen(szFilter);
TCHAR Ch1 = szFilter[i + 2];
memset(szFilter, 0, sizeof(szFilter));
_tcscpy_s(szFilter, _tcslen(_T("All files"))+1, _T("All files"));
TCHAR Ch2 = szFilter[i + 2];
What I am interested in is the part of the buffer the result is not written in. What stunned me was it was all trashed.
Okay the function does not define what it does with the unused parts of the buffer but it seems a waste of time to play with it.
The stranger part is if you lie to the function and make it have just the right size buffer it doesn't trash the buffer.
It's so quirky I am trying to work out if it is a machine/processor dependent implementation of the function.
In vino veritas
|
|
|
|
|
I have seen this before and I think it is to do with the fact that the _s variants are "safe" mode functions. The first thing they do is to overwrite the buffer with some known pattern (hex FEFE in my Unicode test), before doing the copy - at least in Debug mode. You could try a Release mode version and add some code to examine the buffer at each point. A 16 character buffer will be big enough.
|
|
|
|
|
Yes it's hex FEFE pattern for me as well. I tried release and it's not there .. must be something to do with debug mode.
That is what was doing my head in a code that would run in release mode but not in debug mode ... now that is a turn up.
Update: David Crowe got the weirdness there is a FILL_STRING call put on the balance of the buffer. I can't for the life of me think why they do it so have referred it to Microsoft as to why debug mode does it, and will work around the behaviour.
In vino veritas
|
|
|
|
|
Just confirmed this using ordinary strcpy_s , so nothing to do with TCHAR s. Also when I run it in Release mode the buffer is not filled with the xFE characters.
|
|
|
|
|
|
A good catch that does the job. Setting it to zero and then putting the value back after the code solves my problem.
Well you learn something everyday, I was completely unaware they had added that functionality into the safe routines but at least they do realize the problem and allow control of it.
In vino veritas
|
|
|
|
|
I reckon it has something to do with _tcscpy_s() internally calling _FILL_STRING() for the balance of the destination string. If you can live without the safe version, _tcscpy() will work as expected.
It looks like you may need to use memcpy() instead.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 29-Sep-16 0:11am.
|
|
|
|
|
You are correct the FILL_STRING is the culprit and no I need the function so will just do the buffer fill long handed. As Richard worked out it isn't there in release mode so it's something to do with debug mode display functions. My guess is it's a pattern for there watch for change implementation.
It did however totally catch me out
Update: Chris correctly identified why it's doing it and how you control it.
In vino veritas
modified 28-Sep-16 2:28am.
|
|
|
|
|
leon de boer wrote: ...so will just do the buffer fill long handed. Just curious but what does it matter what's after the \0 value in the buffer?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I was try to make a filter for OPENFILENAME
OPENFILENAME structure (Windows)[^]
It's not a great structure from MS and it look something like this "DXF Files\0*.dxf\0PLT Files\0*.plt\0\0 "
You can't string copy that in because of the terminating 0 characters in it.
For me it's worse "DXF files", "PLT files" is the English version I have German, French etc strings
to replace it so the string changes.
So I have to build that structure and the old code basically relied on the buffer being zeroed
and it just wrote the string bits in without having to track all the string lengths. It was assumed
strcpy doesn't play with parts of the buffer it isn't writing the result into.
So the correct way to write it would be strcpy + strcat + strcat + strcat + strcat(\0).... which I did.
The safe functions mean you have to track the amount of buffer left to pass in and so it becomes
a lot of code for a very trivial thing ... BUT IT'S GUARANTEED TO WORK
So basically while I was trying to debug the program the code was failing because the code relied
on something not guaranteed. For such a simple string structure its a pain to have to make code
track multiple string pointers just to create it but I should have done it .. lesson 1 never assume
anything not actually specified!!!!
The bigger issue for me and why I went after it was performance. If they are playing around with
stuff they don't need to they are wasting time. String functions are used often and everywhere
and on very large memory blocks and I was concerned about the impact.
In vino veritas
modified 29-Sep-16 0:12am.
|
|
|
|
|
leon de boer wrote:
You can't string copy that in because of the terminating 0 characters in it. Why would you want to? The lpstrFilter member is a pointer not an "array". You should have something like:
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = "DXF Files\0*.dxf\0PLT Files\0*.plt\0";
... Now if you are wanting a dynamic filter (one that changes at runtime), that's a horse of a different color, although still do-able.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Edit: Correction yes that works as a static.
But yes that is a static create but my strings are coming from memory the user can switch language on the fly and the number of types of file that can be loaded depends on version purchased. Bascially I can't possible do it with static strings I have to build the string into an array and pass the pointer to the array. The array is created and disposed of by the dialog and all very normal when doing multilingual dialog functions.
Many of the string bits are coming from windows via the locale API and heaven only knows how long they are and where they are coming from in memory.
I need the array because I have to assemble all the string somewhere.
I would like to see a dynamic form of your code without allocating an array on the heap or local stack
Anyhow it's all working now, the performance was more the issue to me and that is resolved.
In vino veritas
modified 29-Sep-16 1:14am.
|
|
|
|
|
leon de boer wrote:
I need the array because I have to assemble all the string somewhere. Like was previously mentioned, you may need to use memcpy() .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
_tcscpy_s and it's variants only guarantee that the string will be copied IF it fits. I suspect that the compiler "figured out" that it could safely optimize the copy using 32 or 64 bit chunks. This would result in satisfying the condition that the string be null terminated, but would also end up copying random data stored after the original string.
|
|
|
|
|
Hi,
I am new in MFC vc++ Please help me to come out from an major issue.
I want to move an object(Image,Rectangle) ..etc in Canvas buy using Arrow Key(left,right,up,down) in MFC VC++ application.only i get OnKeyDown()function I am not able to use this OnKeyDown()function to get keyboard event.
please provide a proper code
|
|
|
|
|
|
Hi Chris Losinger
Yes this is dialog-based application but arrow key functionality i need to apply in canvas/editor not in dialog.
Thanks
|
|
|
|
|
I am learning how to use ellipsis in function.
Started with putting "int" as parameters, with first entry being count of parameters passed to he function.
Now I want to pass multiple types , not just int , to the function.
The following snippet will do, but...
How do you get "type" from passing a char * - in this case "format" variable?
What did I missed ?
Thanks for your help.
Vaclav
I am not sure if I tagged the code properly.
<pre lang="c++">
void my_printf( char *format, ... ) {
va_list argptr;
va_start( argptr, format );
while( *format != '\0' ) {
// string
if( *format == 's' ) {
char* s = va_arg( argptr, char * );
printf( "Printing a string: %s\n", s );
}
// character
else if( *format == 'c' ) {
char c = (char) va_arg( argptr, int );
printf( "Printing a character: %c\n", c );
break;
}
// integer
else if( *format == 'd' ) {
int d = va_arg( argptr, int );
printf( "Printing an integer: %d\n", d );
}
*format++;
}
va_end( argptr );
} </pre>
|
|
|
|
|
That code looks like it should broadly work, but you are assuming any 's', 'c', or 'd' in the string is a format identifier. Your main loop should iterate the characters of the format string, copying them to the output until you see a format specifier. When you see a '%' character then you know that is the start of a formatting sequence so you need to get the width, precision and type details. Once you have extracted all those values, then you can process the next argument according to the type field character.
|
|
|
|