473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

using timer in serial communication causes unhandled exception

I am coding in c++ mfc where i try to do serial communication.
I have successfully done all the connection to the camera and even receive data from the camera.

However, ran into some problems.

When i was running the program however, i got a "Unhandled exception at 0x7833dcfa (mfc80ud.dll) in SerialCommunication.exe: 0xC0000005: Access violation reading location 0x00000020"

It points to this particular line.
SetTimer(1, 100,NULL);

the timer is being used in a way such that it will be set when it is trying to receive/send data from the camera through a serial communication and if data is not received within tat timer period, the program will abort the operation.
May 25 '10 #1
5 3511
Banfa
9,065 Expert Mod 8TB
Post the actual code, are you sure the window object you are calling it through is valid? An invalid this pointer could cause such an error but it is not likely the SetTimer call itself is causing it.
May 25 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
According to MSDN, the first argument for SetTimer is an hWnd which is either NULL or is a valid handle to a window owned by the current thread.

Why is your first argument a 1?
May 25 '10 #3
Banfa
9,065 Expert Mod 8TB
I think because MFC is being used, that SetTimer call has exactly 1 parameter less than required which corresponds to the SetTimer method of the CWnd class. And is the reason I requested the actual code.
May 25 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
It may be some obj->SetTimer where obj is a CWnd*, but I just took the post at face value. I have to watch out doing that.
May 25 '10 #5
sorry for the late reply people.

This is the code for connecting to the port
Expand|Select|Wrap|Line Numbers
  1. bool CSerialControl::Connect(CString portName, CString btnName)
  2. {
  3.  
  4.    // fill timeout structure
  5.     GetCommTimeouts(hComm, &timeout);
  6.     timeout.ReadIntervalTimeout = 100;          
  7.     timeout.ReadTotalTimeoutConstant = 100;
  8.     timeout.ReadTotalTimeoutMultiplier = 0;
  9.     timeout.WriteTotalTimeoutConstant = 100;
  10.     timeout.WriteTotalTimeoutMultiplier = 100;    
  11.  
  12.     SetCommTimeouts(hComm, &timeout);
  13.  
  14.     portName = _T("\\\\.\\") + portName;
  15.     hComm = CreateFile( portName, GENERIC_READ |  GENERIC_WRITE, 0, NULL, OPEN_EXISTING,  0, NULL ) ;
  16.     if( hComm == INVALID_HANDLE_VALUE )
  17.     {
  18.         AfxMessageBox( _T( "Error opening Port" ) ) ;
  19.         return false;
  20.     }
  21.     else
  22.         return true;
  23.  
  24. }
This is the method which calls the settimer
Note 1 : uResult is UINT declared globally.
Note 2: Global variable const UINT ID_TIMER = 0x1001;
Expand|Select|Wrap|Line Numbers
  1. /*method to read camera replies */
  2. bool CSerialControl::ReadCameraParameter()
  3. {
  4.     char m_ReceiveBuffer[ 5000 ] ;
  5.     DWORD m_CharRead ;
  6.  
  7.     m_CharRead = NULL ;
  8.  
  9.  
  10.  
  11.     uResult = SetTimer( ID_TIMER ,100,NULL);
  12.     ReadFile( hComm, m_ReceiveBuffer, 5000, &m_CharRead, NULL ) ;
  13.  
  14.  
  15.     if ( uResult == 0 )
  16.     {
  17.         AfxMessageBox(_T("Error: Could not connect the RS232 port!"), MB_OK|MB_ICONEXCLAMATION);
  18.         // Stop the timer. Indirectly it will stop the reading
  19.         BOOL blStatus = KillTimer(uResult) ;
  20.  
  21.         // Check Whether the timer is killed properly or not.
  22.         if ( blStatus == 0 )
  23.             AfxMessageBox(_T("Error: Could not stop the watching of RS232 port!"),  MB_OK|MB_ICONEXCLAMATION);
  24.  
  25.         // Reset the handle variable
  26.         CloseHandle( hComm) ;
  27.         return false;
  28.     }     
  29.     if( m_CharRead == NULL ) 
  30.         return false ;
  31.  
  32.     ci.camName = GetParam(m_ReceiveBuffer, "Camera Model No.:");
  33.     ci.serial = GetParam(m_ReceiveBuffer, "Camera Serial No.:" );
  34.     ci.firmware = GetParam(m_ReceiveBuffer, "Firmware Design Rev.:" );
  35.     ci.cci = GetParam(m_ReceiveBuffer, "CCI Version:");
  36.     ci.fgpa = GetParam(m_ReceiveBuffer, "FPGA Version:");
  37.     ci.exposure = GetParam(m_ReceiveBuffer, "Exposure Mode:");
  38.     ci.baudrate = GetParam(m_ReceiveBuffer, "UART Baud Rate:");
  39.     ci.tdi = GetParam(m_ReceiveBuffer, "TDI Mode:");
  40.     ci.mirror = GetParam(m_ReceiveBuffer, "Mirroring Mode:");
  41.  
  42.  
  43.  
  44.  
  45.     return true;
  46. }
  47.  
and finally, the ontimer handler method

Expand|Select|Wrap|Line Numbers
  1. void CSerialControl::OnTimer(UINT_PTR nIDEvent)
  2. {
  3.  
  4.                 if( nIDEvent == ID_TIMER )
  5.                       uResult --;
  6.     CDialog::OnTimer(nIDEvent);
  7. }
  8.  
I was thinking it was the handler to the settimer too!but i cant seem to find whats wrong with it :(
May 26 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Haluk Gokmen | last post by:
I had an inquiry about the use of MSComm OCX in a C# application awhile ago and I really appreciated Nicholas Paldino's help, which is listed below... My code worked well in VS2003. I am now...
5
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by...
5
by: Simon Tamman {Uchiha Jax} | last post by:
Now this is bugging me. I just released software for a client and they have reported an unhandled stack overflow exception. My first concern is that the entirity of the UI and any threaded...
4
by: Lonifasiko | last post by:
Hi, I've been able to communicate using HyperTerminal with my device via serial port COM1. I just send a command and device switches on. I just need that to start playing with it. This way, I...
3
by: NickP | last post by:
Hi there, Im experiencing an unhandled exception that seems to be impossible for me to catch. I am launching an assembly of mine from within another assembly using the Process object. Once the...
5
by: InNeedOfHelp | last post by:
I am using VC++ 6, I am trying to add a CBitmapButton to a dialog, I followed the steps from the web page below. I got the bitmap button working but when I exit the dialog box a message appears...
0
by: Mike S | last post by:
I'm still looking into this problem, but to summarize: I wrote a very simple Windows Service in VB.NET, as part of a larger application. I created a deployment project for the application the...
0
by: nmsreddi | last post by:
Hi friends I am working on c#.net . i am developing a windows application using c# 2003 aim of my application is sending and receiving sms using GSM modem. for this i am using AxMscomm...
5
by: =?Utf-8?B?c3VydHVyeg==?= | last post by:
Hi, I feel like a noob for asking this. When I publish a VB windows application, I want to disable the ability of the the user to continue when there is an unhandled exception. For example,...
1
by: InduGokul | last post by:
i want to perform serial communication on selecting .Net Framework2.0--->Visual Basic--->Device application presently i am using this code Imports System.IO.Ports Imports...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.