FaxMan Jr
AutoReceiving of Faxes
Implementing Automatic Receiving of Faxes in Your Application

The Main .NET samples implement an Auto Receive function, how can I implement this in my application?

The FaxCompletionStatus is the key to this functionality. When this event is fired the fax modem is now ready to either listen for receiving another fax or send a fax that may be queued up.

If all this modem does in your application is to receive faxes then this event is very simple to implement.  Just tell it to prepare to receive another fax.

C# Receive Sample
Copy Code
// Form Level Variables 
DataTech.FaxManJr.Faxing _faxing; 

// This is the event when the device is no longer busy. 
private void _faxing_FaxCompletionStatus(object sender, FaxEventArgs args) 
{ 
   // Did my last received fax come in successfully? 
   if (DataTech.FaxManJr.FaxError.Ok == args.Fax.FaxError) 
       // Do something with the fax 
       System.Diagnostics.Debug.WriteLine( 
           String.Format("Succesfully received fax file: {0}", 
           args.Fax.FaxFiles[0].ToString())); 

   // Ready to Receive another fax 
   _faxing.Receive(1); 
} 

// And make sure you start the receiving process somewhere else in your application: 
private void Form1_Load(object sender, System.EventArgs e) 
{ 
   // Set the modem up for use 
   _faxing.Modem = new Modem(3, FaxClass.Class21); 

   // The single parameter tells FaxMan Jr to 
   // let the phone ring once before picking up. 
   _faxing.Receive(1); 
} 
VB.Net Receive Sample
Copy Code
' Form Level Variables
Private _faxing As DataTech.FaxManJr.Faxing
' This is the event when the device is no longer busy.
Private Sub _faxing_FaxCompletionStatusEvent(ByVal sender As Object, ByVal args As FaxEventArgs)
   ' Did my last received fax come in successfully?
   If DataTech.FaxManJr.FaxError.Ok = args.Fax.FaxError Then
    ' Do something with the fax
    System.Diagnostics.Debug.WriteLine(String.Format("Succesfully received fax file: {0}", args.Fax.FaxFiles(0).ToString()))
   End If
   ' Ready to Receive another fax
   _faxing.Receive(1)
End Sub
' And make sure you start the receiving process somewhere else in your application:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
   ' Set the modem up for use
   _faxing.Modem = New Modem(3, True, False, False)
   ' The single parameter tells FaxMan Jr to
   ' let the phone ring once before picking up.
   _faxing.Receive(1)
End Sub

The samples do more than this. They use the line for both sending and receiving. When the modem is not busy sending it will revert to receive mode. When a user wants to send a fax and if the modem is not currently receiving a fax it will stop listening for an incoming fax and send out the fax.

So how does this look in my application?

C# Sample
Copy Code
// Form Level Variables 
DataTech.FaxManJr.Faxing _faxing; 
DataTech.FaxManJr.Fax _fax; 
bool _waitingToSend; 

// We will have the modem ready to receive 
// for the entire time the application is running 
private void Form1_Load(object sender, System.EventArgs e) 
{ 
   // Modem on port 2 is a USR modem so I am passing "&F1 instead 
   // of the default "&F" into the init string so that flow control is enabled. 
   // I am also passing "M0" to turn of the modems speaker. 
   _faxing.Modem = new Modem(2, FaxClass.Class20, "AT&F1E0V1&C1&D2S0=0M0", "AT"); 
   _faxing.Receive(1); 
} 

private void mnuSend_Click(object sender, System.EventArgs e) 
{ 
   // Fill in _fax object with sending information 
   // The comma in the phone number tells the modem 
   // to pause to get an outside line on a pbx 
   _fax.FaxNumber = @"9,1-123-456-7890"; 
   _fax.FaxFiles.Add(@"c:\myfaxfile.fmf"); 
   _fax.UserData = "sending fax"; 

   if (_faxing.DeviceStatus == DeviceStatuses.DeviceReceiveMode) 
   { 
       _faxing.CancelFax(); 
       _waitingToSend = true; 
   } 
   else if (_faxing.DeviceStatus == DeviceStatuses.DeviceCurrentlyReceiving) 
   { 
       // Don't cancel fax in the middle of reception, simply wait. 
       _waitingToSend = true; 
   } 
   // In this sample we are not going to let someone schedule 
   // while they are currently sending. 
   // See Batch Sending example for queueing multiple faxes 
   else 
   { 
       System.Windows.Forms.MessageBox.Show("You are currently sending a fax, please wait.", 
           "My Sample App"); 
   } 
} 

// This is the critical event where we decide what to do next 
private void _faxing_FaxCompletionStatus(object sender, FaxEventArgs args) 
{ 
   // First what happened with the last fax 
   if (args.Fax.UserData == "sending fax") 
   { 
       if (DataTech.FaxManJr.FaxError.Ok == args.Fax.FaxError) 
           System.Diagnostics.Debug.WriteLine("Successfully sent your last fax!"); 
       else 
           System.Diagnostics.Debug.WriteLine("There were problems sending your last fax!"); 
   } 
   else if (DataTech.FaxManJr.FaxError.Ok == args.Fax.FaxError) 
       System.Diagnostics.Debug.WriteLine( 
           String.Format("Successfully received fax file: {0}", 
           args.Fax.FaxFiles[0].ToString())); 

   if (_waitingToSend) 
   { 
       // Let's send this fax that is ready to go out 
       _waitingToSend = false; 
       _faxing.Send(_fax); 
   } 
   else 
   { 
       // Ready to Receive another fax 
       _faxing.Receive(1); 
   } 
} 

An important addition to the FaxCompletionStatusEvent would be a handler in case there is something wrong with the port so that the application does not endlessly loop trying to receive a fax when the port is not working.  Below is one possible implementation of this, refer to the main sample for another.

 

C# Sample
Copy Code
// Form Level Variable 
int    _receiveErrors; 

private void _faxing_FaxCompletionStatus(object sender, FaxEventArgs args) 
{ 

   // Other Completion Status Event Code 
   ... 

   // Check for bad device information to prevent endless cycling 
   _receiveErrors = (DataTech.FaxManJr.FaxError.Error == args.Fax.FaxError) ? _receiveErrors + 1 : 0; 

   if (_waitingToSend) 
   { 
       // Let's send this fax that is ready to go out 
       _waitingToSend = false; 
       _faxing.Send(_fax); 
   } 
   else if (_receiveErrors > 10) 
   { 
       // device failed 10 times in a row 
       System.Diagnostics.Debug.WriteLine("There are problems with your modem, Please check your modem and restart the application!"); 
   } 
   else 
   { 
       // Ready to Receive another fax 
       _faxing.Receive(1); 
   } 
} 


In conclusion, when designing a desktop application users expect their application to be able to receive faxes as well as send them out.  It is easy to add this functionality. The full sample automatically views the received faxes on successful reception as well.

See Also

 

 


© 2014 Data Techniques, Inc. All Rights Reserved.

Send comments on this topic.