现在有两个程序在同一台电脑上运行,一个程序复杂从PLC里取出数据,写到某个文件中,另一个实时从这个文件把数据读取出来。我现在想问一下,我现在最好是把数据往哪里写,另外一个程序才能有效的读取出来,数据库行不???或者文本文件,或者其他,那种方式最好,起码不能冲动或者丢数据。

解决方案 »

  1.   

    写数据库和写文件都可以,不过文件在打开的时候需要设置共享,不然另外一个程序打开文件就会失败。
    写数据库也可以,但是数据的同步就比较麻烦,可以设置一个公共标记,但是这样是明显是自找麻烦。
    所以,建议用文件,不要使用缓冲,只需要顺序读写就可以了。
      如果你要求是从plc中连续读取数据,不需要保留历史数据的话,还是建立socket 或 pipe通信吧。
      

  2.   

    如果数据量不大,不用写文件,用WM_COPYDATA消息,使两个进程间传递数据,如果稍微大点还可以使用内存映射文件的方法,管道等。
      

  3.   

    WM_COPYDATA
    The WM_COPYDATA message is sent when an application passes data to another application. WM_COPYDATA 
    wParam = (WPARAM) (HWND) hwnd;            // handle of sending window 
    lParam = (LPARAM) (PCOPYDATASTRUCT) pcds; // pointer to structure with data 
     
    Parameters
    hwnd 
    Handle to the window passing the data. 
    pcds 
    Pointer to a COPYDATASTRUCT structure that contains the data to be passed. 
    Return Values
    If the receiving application processes this message, it should return TRUE; otherwise, it should return FALSE. Res
    An application must use theSendMessage function to send this message, not thePostMessage function. The data being passed must not contain pointers or other references to objects not accessible to the application receiving the data. While this message is being sent, the referenced data must not be changed by another thread of the sending process. The receiving application should consider the data read-only. The pcds parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by pcds. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer.