Im using VS2008 for compiling and developing my application. Im required to take Adc input and serial print it through RS232 in a WinCE6 OS over some termianl like putty or hyperterminal. The problem is when I use the WriteFile function it gives me the following error.
Error 3 error C2440: '=' : cannot convert from 'double' to 'char [32]' c:\Users\Sohan\Downloads\uartdemo\uartdemo\src\main.c 137 Sohan_1
I want to take the input from ADC channel and then after converting it to voltage i have to transmitt. I have tried using a constant char string and it works but when i take the input from the channel and then try it doesnt work.
HANDLE portHandle;
DWORD noOfBytesRead = 0;
DWORD bytesTransmitted = 0;
DWORD firstChoice = 0;
BOOL retVal = FALSE;
char c="hello";
char transmit2Buffer[BUFFER_SIZE] = "7.8888v";
char volt[BUFFER_SIZE];
WriteFile(portHandle, transmit2Buffer, strlen(transmit2Buffer), &bytesTransmitted, NULL);
WriteFile(portHandle, volt, strlen(volt), &bytesTransmitted, NULL);
the first write function works but the second doesnt. variable volt will keep on changing so how should i write it.please help..
After searching i have got an answer for my question.
this worked for me..
You can use sprintf() as you have done to convert a double to a string, but I would actually recommend using _snprintf() instead simply because sprintf() has no regard for the fact that strings are fixed length devices in memory and will overflow if you don't watch it. _snprintf() allows you to specify the length of the out string, just be sure to specify the size as one less than the actual allocated memory block, because _snprintf() does not store the terminating null character if it has to cut the output short.
An example us using _snprintf() is:
void ToString(char * outStr, int length, double val)
{
_snprintf(outStr,length,"%f",val);
}
Got this answer on some website!!!
Related
In visual studio 2015, When I am trying to write less than 4 characters in Rich Text Box, it gives exception (below is the attachment)
After doing Debugging, we came to know that Ensure() is causing the Exception:
int CRichEditCtrl::GetLine(_In_ int nIndex, _Out_writes_to_(nMaxLength, return) LPTSTR lpszBuffer, _In_ int nMaxLength) const
{
ASSERT(::IsWindow(m_hWnd));
ENSURE(sizeof(nMaxLength)<=nMaxLength*sizeof(TCHAR)&&nMaxLength>0);
*(LPINT)lpszBuffer = nMaxLength;
return (int)::SendMessage(m_hWnd, EM_GETLINE, nIndex, (LPARAM)lpszBuffer);
}
When we are giving less that 4 characters in Rich Text Box,
sizeof(nMaxLength)<=nMaxLength*sizeof(TCHAR)
in this case sizeof(nMaxLength) = 4 and nMaxLength*sizeof(TCHAR) = 3
So, 3<4 is causing the Exception.
Now, I need help in which way Shall I give less than 4 charcters in a Rich Text Box, so that this function works and doesnt gives an Exception.
Sure it works.
This function GETs a line. You have to offer a buffer, large enough to fir the contents. The smallest buffer you are allowed to pass to the message is the size of an integer (4 bytes).
The size of the that you receive is returned by the function.
This code always work for any length
CString strTemp;
nMinLength = min(nLineLength,sizeof(int));
int iLen = m_ItemTextCtrl.GetLine(k, strtemp.GetBuffer(nMinLength), nLineLength);
strTetmp.ReleaseBuffer(iLen);
Passing data to the RTF control is done by streaming in data or using WM_SETTEXT or SetWindowText
I will briefly explain what I want to do and help appreciated.
I have a hex number which is formatted as 16 byte number like this:
1: std::string myhex = "00000000000000000000000000000FFD";
Then I want to convert it to int. Which I think I successfully do using this:
// convert hex to int
unsigned int x = strtoul(myhex.c_str(), NULL, 16);
printf("x = %d\n", x); // prints 4093 as needed
Now, I want to convert this integer back to hex. Which I think I also successfully do using this:
// Convert int back to hex
char buff[50];
string hexval;
sprintf(buff,"%x",x);
hexval = buff;
cout << hexval.c_str(); // prints "ffd".
But my problem is that now, I want to convert the "ffd" string as above back to the format it was before, e.g., 16 byte number padded with zeros like this:
00000000000000000000000000000FFD
I want to convert the string not only print it.
Any help how to do this?
Also any corrections if anything I was achieving above is wrong or not OK are welcome.
Preferably I would like this to compile on Linux also.
Use the 0 flag (prefix) for zero-padding and field width specification in a printf:
printf("%032X", x);
Use snprintf to store it in your string:
snprintf(buff, sizeof(buff), "%032X", x);
Or use asprintf to store it in a newly-allocated string, to be certain that the memory available for the string is sufficient (since it's allocated by asprintf):
char *as_string = NULL;
asprintf(&as_string, "%032X", x);
I am helping a friend get a graphics LCD working on his AVR, a few months ago all was working without issue, it has been untouched since then. The chip has now been swapped out from an ATMega32 to an ATMega164P. Essentially the same chip with more flash, since this change a lot of the code has stopped working.
We have narrowed where the error is occuring, but are unable to rectify it. It is where we pass in a pointer to a const char string, and attempt to print that string, however for some reason the stack(heap, something else?) gets corrupted and the pointer contains all zeros. Does anyone have any ideas how this could occur? We have -O1 level optimisations enabled which are required for correct timing, we have switched to winAVR compiler as well with no changes. We also do not have access to a debugger, and only have limited 'print' style debugging.
Here is the section of code that is causing issues:
//in the header file that is included
int SGCTEXTStringF(int column, int row, int font, int colour, const char* text);
//Call the function
SGCTEXTStringF(0, 0, 0x10, SGCColour(255,255,255), "text");
//Function
int SGCTEXTStringF(int column, int row, int font, int colour, const char* text){
unsigned char bytes[23]={0};
//...Code to communicate with LCD and set up a 'print string'
if(text[0] == 0) {
bytes[6] = 'a';
bytes[7] = 't';
bytes[8] = 'e';
bytes[9] = 's';
bytes[10] = 't';
}
//..more code to finish sending the array
}
Now the display prints 'atest' when the code is run, this is showing that the const char array is being some how zero'd? I have also tried the following lines which also all print 'atest'
if(text[1] == 0) //prints 'atest'
if(*text== 0) //prints 'atest'
if(text != 0) //prints 'atest'
This shows that it gets a valid pointer, but it appears to point to all zeros.
We also tried changing the call to the method to:
const char * string = "test";
SGCTEXTStringF(0, 0, 0x10, SGCColour(255,255,255), string);
This code was known to be working a few months ago, we even have a video of it running, now every single function in the program exhibits the same issue, char arrays being passed (on the stack?) don't seem to work and get cleared to 0s.
I can arrange to host a copy of the complete source code if anyone is interested. Any help or pointers at all are appreciated!
The ATMega164P is not the replacement for the ATMega32 but for the ATMega16. For the ATMega32 you would need the ATMega324P.
If you are using the memory map for the ATMega32 (which has double EEPROM, SRAM and flash), your string may have landed in memory which simply does not exist.
For porting issues you may look at the porting guide from Atmel.
Im using the win32 function ReadFile:
CHAR lpBuffer[256];
DWORD nBytesRead;
DWORD nCharsWritten;
ReadFile(hPipeRead,lpBuffer,sizeof(lpBuffer),
&nBytesRead,NULL) || !nBytesRead)
now im catcing the response from stdout in to lpBuffer with this i like to convert it to std string , the problem is when i do simple :
std::string szReturnlpBuffer(lpBuffer);
the value of the szReturnlpBuffer contains alot of garbege carecthers after the real string:
its looks like this the szReturnlpBuffer value :
"Im stdout from the Qt
applicationÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ"
what im doing wrong here ?
You need to specify the size of the string:
std::string szReturnlpBuffer(lpBuffer, nBytesRead);
because otherwise it reads till it finds a null character, which causes undefined behavior when it gets outside lpBuffer's memory.
You need to terminate the string with a null character:
lpBuffer[nBytesread] = '\0';
The following piece of code seems to unreliably execute and after and undeterministic time it will fail with error code 234 at the RegEnumValue function.
I have not written this code, I am merely trying to debug it. I know there is an issue with doing RegEnumValue and then deleting keys in the while loop.
I am trying to figure out first, why it is throwing this 234 error at seemingly random points, as in, it is never after a consistent number of loop iterations or anything like that.
From what I have seen it fails to fill its name buffer, but this buffer is by no means too small for its purpose, so I don't understand how it could fail??
Could someone please advice on getting rid of this 234 error thrown by the RegEnumValue funciton?
HKEY key;
DWORD dw;
int idx;
char name[8192];
DWORD namesize=4096;
std::string m_path = "SOFTWARE\\Company\\Server 4.0";
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,m_path.c_str(),0,KEY_ALL_ACCESS,&key) == ERROR_SUCCESS)
{
bool error=false;
idx=0;
long result;
long delresult;
while (true)
{
result = RegEnumValue(key,idx,(char*)name,&namesize,NULL,NULL,NULL,NULL);
if (result == ERROR_SUCCESS && !error){
delresult = RegDeleteValue(key,name);
if (delresult != ERROR_SUCCESS)
error = true;
idx++;
}
else
{
break;
}
}
RegCloseKey(key);
}
There are some errors in your code:
The 4-th parameter of RegEnumValue (the namesize) is in-out parameter. So you have to reset namesize to sizeof(name)/sizeof(name[0]) (in case of the usage char type it is just sizeof(name)) inside the while loop before every call of RegEnumValue. It's the main error in your program.
If you don't want to have ERROR_MORE_DATA error any time you have the buffer having 32,767 characters. It is the maximum size of name the the regitry value (see documentation of RegEnumValue).
It is not good to use KEY_ALL_ACCESS in the RegOpenKeyEx. I'll recomend you to change it to KEY_QUERY_VALUE | KEY_SET_VALUE. It is not a real error, but depends on your environment it could be.
It if better to use UNICODE version of all this functions to speed-up a little the code.
UPDATED: Only small comment about the usage of the UNICODE version. Intern Windows work with UNICODE characters. So usage of non-Unicode version of RegEnumValue si more slow because at the evry call a new UICODE memeory block will be allocated and converted to ANSI/Multi-byte. Moreover if you will has a value name written in a language which can't be converted in you Windows ANSI code page (Chinese, Japanese and so on) and some characters will be replaced to '?' (see WC_DEFAULTCHAR flag of WideCharToMultiByte), then it can be that the function RegDeleteValue will fail with the error code like "the value with the name is not exist".
just change the value of your fourth parameter i.e namesize from 4096 to 8192 .Always MakeSure that it should be always equal to buffer size.
The answer is at the bottom of that page:
http://msdn.microsoft.com/en-us/library/ms724865(VS.85).aspx
Please read the answer of "ERROR_MORE_DATA: lpData too small, or lpValueName too small?" question.