Hi,
I have some problems connecting to my SSC32 using the Win32 API. Whenever I open my RS-232 port and send “ver\r” to verify the connection the program is unable to read from the port (the LED is flashing, so it seems to receive the data). I always have to start LynxTerm, send something, close LynxTerm again, from this time on it works. If I reboot Windows or unplug/plug my Wiretek USB-to-RS232-cable I have to repeat this (sometimes also in random situations as it seems). Here’s the code:
[code] char buffer[1024];
HANDLE hf = CreateFile( "\\\\.\\COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
if( hf == INVALID_HANDLE_VALUE )
throw std::runtime_error( "opening handle failed" );
if( !SetupComm( hf, sizeof( buffer ), sizeof( buffer ) ) )
throw std::runtime_error( "setting up comm failed" );
DWORD bytesWritten = 0;
if( !WriteFile( hf, "ver\r", sizeof( "ver\r" ), &( bytesWritten ), NULL ) )
throw std::runtime_error( "writing comm port failed" );
std::cout << bytesWritten << " bytes written" << std::endl;
DWORD bytesRead = 0;
if( !ReadFile( hf, buffer, sizeof( buffer ), &( bytesRead ), NULL ) )
throw std::runtime_error( "reading comm port failed" );
std::cout << bytesRead << " bytes read" << std::endl;
buffer[bytesRead] = 0;
std::cout << buffer << std::endl;[/code]
Simple enough, I don’t realize what’s the problem here. I found some C# code which does basicly the same – but works:
[code] TempController.ServoController sc = new TempController.ServoController( 3 );
SerialPort port = new SerialPort( "COM3", 115200 );
port.Open();
port.NewLine = "\r";
port.WriteLine( "ver\r" );
System.Diagnostics.Debug.WriteLine( "received: \"" + port.ReadLine() + "\"" );[/code]
Seems like the only difference is the assignment of the NewLine character – if i remove this line the C# code acts the same as the C++ code, it hangs when it comes to reading from the port. Given this behaviour I am pretty sure that I am missing something in the initialization, but I couldn’t find a NewLine or some similar property in Win32.
Unfortunately I couldn’t find C++ sample code. How do you set up your COM handle in LynxTerm or in RIOS?
Thanks!