Search code examples
c#.netmodemtapi

ITAPI3 Sending and Receiving Data


I am attempting to create an application that will call a remote modem and do some data transfer (custom data in the form of byte arrays).

I am using JulMar's ITapi3 wrapper and c# 4.0 running on Windows 7 64Bit OS (Compiling as x86).

I have the application making the phone call and disconnecting as I expect but I am having trouble actually sending data across the line. Currently I have the following code in the CallStateChanged event when the call state is connected

  var handleArray = callForData.GetID("comm/datamodem");

        var byteContents = BitConverter.ToInt64(handleArray, 0);
        ////temporary Handle array
        IntPtr myPointer =new IntPtr(byteContents);


        ////var pinnedArray = GCHandle.Alloc(handleArray, GCHandleType.Pinned);
        ////var pointer = pinnedArray.AddrOfPinnedObject();
        var commHandle = new SafeFileHandle(myPointer, true);
        try
        {
           //now init filestream
            _dataTransferOutFileStream = new FileStream(commHandle, FileAccess.ReadWrite, 2048, true);

            //start by writing the login message to the modem
            var buffer = CreatePasswordMessage();

       IAsyncResult result= _dataTransferOutFileStream.BeginWrite(buffer, 0, buffer.Length,null,null);

        while (!result.IsCompleted)
            {
            //wait for completion of sending login message.
            Thread.Sleep(10);
            }

            //now we are done with sending login message 
       _dataTransferOutFileStream.EndWrite(result);

            //wait 5 seconds 
            Thread.Sleep(5000);
            //do the same type of thing for the read or whether it was sucessful.
        var readBuffer = new byte[2048];
        IAsyncResult readResult = _dataTransferOutFileStream.BeginRead(readBuffer, 0, 2048,null,null);
        while (!readResult.IsCompleted)
            {
            Thread.Sleep(10);
            }

            //read is complete.

       int readCount = _dataTransferOutFileStream.EndRead(readResult);

            Debug.WriteLine("Read Complete Count of Bytes Read: {0} Content of First Byte: {1} ",new object[]{readCount,readBuffer[0]});
        }

        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
            return false;
        }
        finally
        {

            commHandle.Close();

        }




        return true;

This doesn't seem to be actually sending the data or recieving any valid data from the remote site. Is there something I am missing? Is there a way to check whether the SafeFileHandle being used is actually a reference to the modem's port?

I tried using the builtin SerialPort class for .NET after I am connected but I get an error that the port in question is in use by another process (I am assuming TAPI has a lock on it.)

I am open to all suggestions.


Solution

  • Ok I figured out what the issue was. Apparently I was formatting the data I was sending to the remote modem incorrectly and rather than the remote site telling me that it was simply ignoring my message altogether.

    So the code above will work for sending and receiving data asynchronously over a modem line in C#.