ITAPI3 发送和接收数据

发布于 2024-11-15 12:31:04 字数 2350 浏览 4 评论 0原文

我正在尝试创建一个应用程序,它将调用远程调制解调器并进行一些数据传输(字节数组形式的自定义数据)。

我正在使用 JulMar 的 ITapi3 包装器和在 Windows 7 64 位操作系统上运行的 c# 4.0(编译为 x86)。

我有应用程序按照我的预期拨打电话并断开连接,但我在实际通过线路发送数据时遇到了麻烦。目前,当呼叫状态已连接时,我在 CallStateChanged 事件中有以下代码

  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;

这似乎并未实际发送数据或从远程站点接收任何有效数据。我有什么遗漏的吗?有没有办法检查正在使用的 SafeFileHandle 是否实际上是对调制解调器端口的引用?

连接后,我尝试使用 .NET 的内置 SerialPort 类,但收到错误消息,指出相关端口正在被另一个进程使用(我假设 TAPI 已锁定它。)

我愿意接受所有建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2024-11-22 12:31:04

好吧,我知道问题是什么了。显然,我错误地格式化了发送到远程调制解调器的数据,而不是远程站点告诉我它完全忽略了我的消息。

因此,上面的代码适用于在 C# 中通过调制解调器线路异步发送和接收数据。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文