酷德网

HSC6150发布~
查看: 150|回复: 1

Codesys 使用共享内存 打通通讯

[复制链接]
  • TA的每日心情
    擦汗
    2019-8-13 14:13
  • 签到天数: 32 天

    [LV.5]常住居民I

    发表于 2020-8-7 16:30:36 | 显示全部楼层 |阅读模式
    Codesys V3.5 平台   提供了库SysShm,其中包含了共享内存操作的接口函数:
    SysSharedMemoryClose;
    SysSharedMemoryCreate;
    SysSharedMemoryDelete;
    SysSharedMemoryGetPointer;
    SysSharedMemoryOpen2;
    SysSharedMemoryRead;
    SysSharedMemoryReadByte;
    SysSharedMemoryWrite;
    SysSharedMemoryWriteByte;

    与其进行共享内存通讯的平台Linux,window;下的编程Demo:
    Linux:
    [C] 纯文本查看 复制代码
    #include <stdio.h>[/font][/color][/align]
    #include <stdlib.h>
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
     
    
    struct DataExchange {
        int i1;
        int i2;
    };
     
    
    int main(int argc, char **argv)
    
     
    
    {
    
        struct DataExchange *pRead, *pWrite;
    
        char sSharedMemReadName[100] = "_CODESYS_SharedMemoryTest_Write";
    
        char sSharedMemWriteName[100] = "_CODESYS_SharedMemoryTest_Read";
    
        int fdRead = shm_open(sSharedMemReadName, O_CREAT | O_RDWR, S_IRWXU | S_IRWXG);
    
        printf("Shared Memory Read: %s %d\n\n",sSharedMemReadName, fdRead);
    
        ftruncate(fdRead, sizeof(*pRead));
    
        pRead = mmap(0, sizeof(*pRead), PROT_READ | PROT_WRITE, MAP_SHARED, fdRead, 0);
    
        close(fdRead);
    
        int fdWrite = shm_open(sSharedMemWriteName, O_CREAT | O_RDWR, S_IRWXU | S_IRWXG);
    
        printf("Shared Memory Write: %s %d\n\n",sSharedMemWriteName, fdWrite);
    
        ftruncate(fdWrite, sizeof(*pWrite));
    
        pWrite = mmap(0, sizeof(*pWrite), PROT_READ | PROT_WRITE, MAP_SHARED, fdWrite, 0);
    
        close(fdWrite);
    
        do
    
            sleep(1);
    
            printf("pRead->i1: %d pRead->i2: %d pWrite->i1: %d pWrite->i2: %d\n",
    
             pRead->i1, pRead->i2, pWrite->i1, pWrite->i2);
    
            printf("Press 'Enter' to increment values or 'q' and then 'Enter' to quit\n");
    
            pWrite->i1++;
    
            pWrite->i2--;
    
        } while (getchar() != 'q');
    
        munmap(pRead, sizeof(*pRead));
    
        shm_unlink(sSharedMemReadName);
    
        munmap(pWrite, sizeof(*pWrite));
    
        shm_unlink(sSharedMemWriteName);
    
        exit(0);
    
    }


    在Linux 下的编程注意 shm_open();若使用使有问题,可以用open()函数替代。
    Linux下共享内存的实质:是在/dev/shm/   文件夹下创建相应文件,路径为:  /dev/shm/文件名;
    Codesys若运行于Linux下则是在该路径创建共享文件。


    Windows平台,C++/ C # 编程:
    C++:
    OpenFileMapping();
    CreateFileMapping();
    MapViewOfFile();
    GetProcessWorkingSetSize(); //查询进程最大,最小工作集内存大小
    SetProcessWorkingSetSize(); //设定进程最大,最小工作集内存大小,在进程挂起,或最小化时,将部分内存交换到虚拟内存。
    VirtualLock();   //锁定进程的虚拟地址空间的指定块到物理内存。


    C#:
    [C++] 纯文本查看 复制代码
    using System;
    using System.IO.MemoryMappedFiles;
    using System.Runtime.InteropServices;
    using System.Threading;
    
    namespace SharedMemoryWin
    {
    
        // Example structure for data exchange
        internal struct DataExchange
        {
            internal Int32 i1;
            internal Int32 i2;
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                DataExchange dataExchangeRead, dataExchangeWrite;           
                dataExchangeWrite.i1 = 0;
                dataExchangeWrite.i2 = 0;
               
                Console.Out.WriteLine("Press 'q' to quit");
    
                int dataSIze = Marshal.SizeOf(typeof(DataExchange));
               
                // Open a mapped file with read access and one with write access.
                using (var mmfRead = MemoryMappedFile.CreateOrOpen("_CODESYS_SharedMemoryTest_Write", dataSIze))
                using (var mmfWrite = MemoryMappedFile.CreateOrOpen("_CODESYS_SharedMemoryTest_Read", dataSIze))
                {
                    bool quit = false;
                    while (!quit)
                    {
                        using (var accessorRead = mmfRead.CreateViewAccessor(0, dataSIze, MemoryMappedFileAccess.Read))
                        using (var accessorWrite = mmfWrite.CreateViewAccessor(0, dataSIze, MemoryMappedFileAccess.Write))
                        {
                            // Read the structure
                            accessorRead.Read(0, out dataExchangeRead);
                            // Write the structure
                            accessorWrite.Write(0, ref dataExchangeWrite);
    
                            // Display the values
                            Console.Out.Write("Read i1: {0} i2: {1}       Write i1: {2} i2: {3}\r",
                                                dataExchangeRead.i1,
                                                dataExchangeRead.i2,
                                                dataExchangeWrite.i1,
                                                dataExchangeWrite.i2);
    
                            // Wait a second
                            Thread.Sleep(1000);
    
                            // Increment sample values
                            dataExchangeWrite.i1++;
                            dataExchangeWrite.i2--;
    
                            // Check quit condition
                            if(Console.KeyAvailable)
                                if (Console.ReadKey().KeyChar == 'q')        
                                    quit = true;
                        }
                    }
                }
            }
        }
    }

    打赏鼓励一下!
    +10
    回复

    使用道具 举报

  • TA的每日心情
    难过
    4 天前
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2020-9-3 16:44:40 | 显示全部楼层
    这种是不是cosesys和C#只能在同一台电脑上
    打赏鼓励一下!
    +10
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

    本版积分规则

    
    顶部qrcode底部
    关注酷德网订阅号,获取更多资讯!

    QQ|小黑屋|手机版|Archiver|酷德论坛 ( 苏ICP备16065247号 )|网站地图

    GMT+8, 2020-10-23 07:01 , Processed in 0.163506 second(s), 62 queries .

    酷德网

    © 2001-2013 Hicodesys. 技术支持 by 酷德网

    快速回复 返回顶部 返回列表