|
Nooo.
I have a text file that I created manually,and I'd like to read the text from it.
I made a mistake when I write ||, but it is not the error
modified 11-Oct-11 20:20pm.
|
|
|
|
|
`So, why are you asking for the the file to be Created?
if ( myFile.Open(path, CFile::modeCreate | CFile::modeRead ) )
| is a bitwise OR, || is a logical OR
|
|
|
|
|
Yes you are right, I didnt know what mean this argument, if I dont put this argument, everything run rightly
|
|
|
|
|
I continue to feel very fortunate that the vast majority of programming (that I see) is done in english.
I understand that with function/variable names in my own language I am at an advantage to those for whom they are in a different language.
|
|
|
|
|
Is there any API which gives me list of Seial Ports available on system. I am trying to use QueryDosDevice () but its failed to provide any result .
Thanks
|
|
|
|
|
|
pandit84 wrote: Is there any API which gives me list of Seial Ports available on system.
SerialPort.GetPortNames Method[^] is available as a.Net library method.
You could also try Another serial port enumerator[^] which uses a different approach to find serial ports by enumerating registry keys.
-OR-
Enumerating serial ports - W2K style [^]- Enumerating the serial ports using the SetupDi* API provided with Win2K and later
pandit84 wrote: QueryDosDevice () but its failed to provide any result .
I have also found this support page [^] on why QueryDosDevice fails.
modified 11-Oct-11 7:31am.
|
|
|
|
|
Good answer overall, but considering this is the "C/C++/MFC" forum, don't think pointing out things in the .Net library are applicable. +5 for the other info though...
|
|
|
|
|
|
Thank you for contributing...
|
|
|
|
|
|
The CreateFile function creates or opens a file or I/O device.
The following code lists COM Ports in Win32..
#include <iostream>
#include <windows.h>
void main(){
char port_name[16];
for (int port_number = 1; port_number < 256; ++port_number)
{
port_number < 10 ?
sprintf(port_name, "COM%d", port_number) :
sprintf(port_name, "\\\\.\\COM%d", port_number);
HANDLE hFile = ::CreateFile(port_name,
GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
std::cout << port_name << std::endl;
else
{
DWORD err = GetLastError();
if (err == ERROR_ACCESS_DENIED ||
err == ERROR_SHARING_VIOLATION)
std::cout << port_name << std::endl;
}
CloseHandle(hFile);
}
}
|
|
|
|
|
What if a COM port is in use at the time this code is run? It won't get listed.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Also, under Vista (and probably Weven), you need 'run as admin' to use the "\\\\.\\COM11" form. Stoopid bug I ran into when Vista was first released.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Good info... I hate having to deal with all these "run as admin" issues...
|
|
|
|
|
This may not be so bad though... in most programs, if a COM port is already in use, then you can't use it for something else... so it shouldn't be listed anyway. Depends on what the OP needs to list the COM ports for.
|
|
|
|
|
umm... yea
|
|
|
|
|
Hi pandit84,
I highly recommend that you have a look at EnumSerialPorts[^] written by MVP PJ Naughter[^]. Its been in development since around 1998 and supports every enumeration type... WMI, registry, setupapi and QueryDosDevice.
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks a lot to All,
I have successfully enumerated registry and got list of COM ports available . If we have any
virtual port software which can create multiple ports, then also we can enumerate registry and read those port number.
|
|
|
|
|
Hello! I have a function that saves a jpeg file from an IP camera. It works sometimes, but sometimes, it doesnt. When I investigated, i found out that in the instances that it didnt work, the characters of the file when i convert it to a text file looks like this:
ෝ㚆��鍫᱉㓉��⛷통⌫㘿答窂脚麜鋴ᬓ犚厜褝沣놊��ꋂ䴍㢛諱᷐锫잛䕡逺Řޥ舔룣⣅‵쭫帏갪ಱ彗䩂䖒깔숢⢞ᴑ鎵졋쮧呤ᑵ蛝㙕횇涚噈혘㙌සᲷ貚臛诞
Proper jpeg files have characters that look like this:
*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³
I don't know what's wrong. Please help me!!! Here is the function I'm using:
int SendReceiveTest1( Socket^ server )
{
FileStream^ ambot = File::Create("pastilan.jpg");
array<Byte>^ msg = Encoding::UTF8->GetBytes("GET /snapshot.cgi HTTP/1.1\r\n");
array<Byte>^ msg1 = Encoding::UTF8->GetBytes("Authorization: Basic YWRtaW46\r\n\r\n");
array<Byte>^ header = gcnew array<Byte>(224);
array<Byte>^ bytes = gcnew array<Byte>(30000);
try
{
// Blocks until send returns.
Int32 byteCount = server->Send( msg );
Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
Int32 byteCount1 = server->Send( msg1 );
Console::WriteLine( "Sent {0} bytes.", byteCount1.ToString() );
byteCount = server->Receive( header, header->Length, SocketFlags::None );
if ( byteCount > 0 )
{
Console::WriteLine( Encoding::UTF8->GetString( header ) );
}
while(byteCount > 0){
byteCount = server->Receive( bytes, server->Available, SocketFlags::None);
}
ambot->Write( bytes, 0, bytes->Length );
if( ambot )
delete (IDisposable^)ambot;
}
catch ( SocketException^ e )
{
Console::WriteLine( "{0} Error code: {1}.", e->Message, e->ErrorCode.ToString() );
return ( e->ErrorCode );
}
return 0;
}
Usually I dont bother asking if it NEVER works. But Im worried about this particular code because most of the time it DOES work. Thanks!!!
|
|
|
|
|
JPEG files contain encoded image data and will make no sense whatsoever if displayed as text. You should investigate what is happening to your data in between receiving it from the camera and passing it to whatever location you do. I am not sure that you should be trying to encode the data as indicated in the code fragment above.
|
|
|
|
|
Im sorry, something went wrong, here's the whole function:
int SendReceiveTest1( Socket^ server )
{
FileStream^ ambot = File::Create("pastilan.jpg");
array<Byte>^ msg = Encoding::UTF8->GetBytes("GET /snapshot.cgi HTTP/1.1\r\n");
array<Byte>^ msg1 = Encoding::UTF8->GetBytes("Authorization: Basic YWRtaW46\r\n\r\n");
array<Byte>^ header = gcnew array<Byte>(224);
array<Byte>^ bytes = gcnew array<Byte>(30000);
try
{
// Blocks until send returns.
Int32 byteCount = server->Send( msg );
Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
Int32 byteCount1 = server->Send( msg1 );
Console::WriteLine( "Sent {0} bytes.", byteCount1.ToString() );
byteCount = server->Receive( header, header->Length, SocketFlags::None );
if ( byteCount > 0 )
{
Console::WriteLine( Encoding::UTF8->GetString( header ) );
}
while(byteCount > 0){
byteCount = server->Receive( bytes, bytes->Length, SocketFlags::None);
}
ambot->Write( bytes, 0, bytes->Length );
if( ambot )
delete (IDisposable^)ambot;
}
catch ( SocketException^ e )
{
Console::WriteLine( "{0} Error code: {1}.", e->Message, e->ErrorCode.ToString() );
return ( e->ErrorCode );
}
return 0;
}
The pastilan.jpg file that results is sometimes a proper jpeg file, meaning it's really a snapshot of the ip camera, and sometimes, it's not. It's totally RANDOM. I don't know what's happening...
|
|
|
|
|
while(byteCount > 0){
byteCount = server->Receive( bytes, bytes->Length, SocketFlags::None);
}
ambot->Write( bytes, 0, bytes->Length );
In the above lines you may be overwriting part of your image in the while loop, which means that you only write the last part of the file. You are also using the buffer length rather than the actual bytecount to write to the destination file.
|
|
|
|
|
There are two major flaws AFAICT:
1. you have a receiving loop that overwrites the receiving buffer, whereas your file writing is outside the loop; the correct way would have both receive and write in one loop, there is no need to first hold the entire image in memory.
2. you write a fixed length, independent of what you received. That is so wrong; your write statement needs to be based on the byte count you obtain from the receive statement.
And there is another weakness, if not a bug: you assume the header will hold 224 bytes. What if it doesn't? It is not up to the client to guess how big a header the server will use. Check the standards, or use an official implementation (if this is managed C++, why not use HttpWebRequest/HttpWebResponse classes?).
PS: please learn how to apply proper formatting, using PRE tags[^].
modified 11-Oct-11 12:27pm.
|
|
|
|
|