本文介绍了使用Fluke和PC进行RS 232串行编程.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.
我需要连接Fluke Meter Fluke 8845A/8846A 6.5位精密万用表
使用使用c语言的RS 232端口连接到Windows PC,后面的部分是我需要使用Visual Studio 2010在c中创建Windows服务类型程序.
请协助并向我建议c中的代码,以将电表的通信与pc Rs 232端口进行接口,该编程手册位于fluke的网站上(Fluke 8845A/8846A 6.5位精密万用表).
提前非常感谢

Hello.
I need to interface the Fluke meter Fluke 8845A/8846A 6.5 digit Precision Multimeters
to a windows PC using the RS 232 ports using the c language and the later part is i need to create a windows service type program in c using Visual Studio 2010.
Please assist and suggest me the code in c to interface the communication of the meter with the pc Rs 232 ports,the programming manual is in website of fluke (Fluke 8845A/8846A 6.5 digit Precision Multimeters).
Thanks a lot in advance

推荐答案

HANDLE hComm;
hComm = CreateFile( gszPort,
                    GENERIC_READ | GENERIC_WRITE,
                    0,
                    0,
                    OPEN_EXISTING,
                    FILE_FLAG_OVERLAPPED,
                    0);
if (hComm == INVALID_HANDLE_VALUE)
   // error opening port; abort




阅读:




Reading :

DWORD dwRead;
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osReader = {0};

// Create the overlapped event. Must be closed before exiting
// to avoid a handle leak.
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (osReader.hEvent == NULL)
   // Error creating overlapped event; abort.

if (!fWaitingOnRead) {
   // Issue read operation.
   if (!ReadFile(hComm, lpBuf, READ_BUF_SIZE, &dwRead, &osReader)) {
      if (GetLastError() != ERROR_IO_PENDING)     // read not delayed?
         // Error in communications; report it.
      else
         fWaitingOnRead = TRUE;
   }
   else {
      // read completed immediately
      HandleASuccessfulRead(lpBuf, dwRead);
    }
}



和写作:



And Writing :

BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite)
{
   OVERLAPPED osWrite = {0};
   DWORD dwWritten;
   DWORD dwRes;
   BOOL fRes;

   // Create this write operation''s OVERLAPPED structure''s hEvent.
   osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
   if (osWrite.hEvent == NULL)
      // error creating overlapped event handle
      return FALSE;

   // Issue write.
   if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
      if (GetLastError() != ERROR_IO_PENDING) {
         // WriteFile failed, but isn''t delayed. Report error and abort.
         fRes = FALSE;
      }
      else
         // Write is pending.
         dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
         switch(dwRes)
         {
            // OVERLAPPED structure''s event has been signaled.
            case WAIT_OBJECT_0:
                 if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, FALSE))
                       fRes = FALSE;
                 else
                  // Write operation completed successfully.
                  fRes = TRUE;
                 break;

            default:
                 // An error has occurred in WaitForSingleObject.
                 // This usually indicates a problem with the
                // OVERLAPPED structure''s event handle.
                 fRes = FALSE;
                 break;
         }
      }
   }
   else
      // WriteFile completed immediately.
      fRes = TRUE;

   CloseHandle(osWrite.hEvent);
   return fRes;
}



更多详细信息:

http://msdn.microsoft.com/en-us/library/ms810467.aspx [ ^ ]



More details :

http://msdn.microsoft.com/en-us/library/ms810467.aspx[^]



这篇关于使用Fluke和PC进行RS 232串行编程.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 09:38