1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Chris
  • 21
  • 7
  • Still no answers? I am now looking at somehow getting a SerialPort object created from the established call. I can get the Comm Port name and create the SerialPort object but it is of course closed when I try to open it I get an error that it is in use by another process TAPI of course. – Chris Jun 17 '11 at 15:34

1 Answers1

0

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#.

Chris
  • 21
  • 7
  • I read your post , you said "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." Can you please explain more because i am filling in the same trouble. – Nemer Apr 06 '13 at 06:02
  • The issue I encountered was that the device I was attempting to communicate with was expecting a specific message format. I was sending the message in an incorrect format, and rather than communicate that back to me the device was simple ignoring the message. When I reformatted the message to comply correctly with the custom message format the device was expecting everything worked as expected. – Chris Apr 08 '13 at 14:04