我正在尝试创建一个应用程序,它将调用远程调制解调器并进行一些数据传输(字节数组形式的自定义数据)。
我正在使用 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 已锁定它。)
我愿意接受所有建议。