I am confused, too many of you thats not a surprising statement... :-)
I've been trying to learn serial port programing and threading, and so far its been ok till now, but recently I just got baffled, I have never had this happen....
I got help coding a Win32 console app that reads my serial port and I programed a device, basic stamp, where I could get the exact output I want to repeat the message "Hello" . I refined the code below until I got exactly the message reads I wanted from the serial port and then output that to a file. I won't include the function definitions unless necessary to conserve time, but certainly don't mind if it helps, I am just including the execution of the functions and output: Win32 Console App
OUTPUT From Console app:Code:int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; //open port and configure it hPort = ConfigureSerialPort("COM1"); //declare data variables for read string function char message[4]; int sizeBuffer =4; int count = 0; //check to see if configure serial port failed if(hPort==NULL) { // cout<<"COM port config failed"<<endl; return -1; } else { ofstream fout; fout.open("C:\\rs232test.txt", ios::app); do { ofstream fout; fout.open("C:\\rs232test.txt"); //call readfile function ReadString(message, &sizeBuffer); cout<<message<<endl; fout<<message<<endl; count++; Sleep(46); fout.close(); }while (count <=100); } //call the close port function ClosePort(); return nRetCode; }
Same Code in a MFC DLG application, upon clicking the OK button, runs for a few seconds, then app crashes and closes automatically, output file very differentCode:ΜΜΜΜ HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO
OUTPUT from DLG app:Code:void CSerialTestDlg::OnOK() { // TODO: Add extra validation here hPort = ConfigureSerialPort("COM1"); Sleep(1000); //declare data variables for read string function char message[4]; int sizeBuffer =4; int count = 0; //check to see if configure serial port failed if(hPort==NULL) { // cout<<"COM port config failed"<<endl; return; } else { ofstream fout; fout.open("C:\\ThreadTest.txt", ios::app); do { //call readfile function ReadString(message, &sizeBuffer); fout<<message<<endl; Sleep(46); count++; Sleep(46); if (count == 100) { AfxMessageBox("Loop Finished"); fout.close(); break; } }while (count <=100); } AfxMessageBox("Calling ClosePort"); //call the close port function ClosePort(); // CDialog::OnOK(); }
Now, I know there are some very different message handling concepts between a MFC app and basic Win32 Console app, but can someone tell me why I would be getting such dramatic results?Code:HELLOHELLO



LinkBack URL
About LinkBacks


