IOS

摄像机串口操作

版本/作者/日期修改内容
1.0/Sandy/2015-09-24初版

15.摄像机串口操作

条件:摄像机连接成功
语法:
1)函数
-(int)openSerial;  //打开串口
-(int)closeSerial; //关闭串口
-(int)writeSerial:(unsigned char*)data length:(int)len;  //写串口数据,串口必须打开了才能写数据
参数:(unsigned char*)data:数据内容
       (int)len:data的长度
 -(int)readSerial;//读串口数据,串口必须打开了才能读数据
2)消息通知:
 FOUNDATION_EXPORT NSString* const IPCamera_Open_Serial_Result; //打开串口成功
 FOUNDATION_EXPORT NSString* const IPCamera_Write_Serial_Result; //写数据成功
 FOUNDATION_EXPORT NSString* const IPCamera_Serial_Data; //读 有数据返回
3) 设置成功后的消息回调

-(void)IPCamera_response_function:(NSNotification*)note;

IPCamera.h宏定义了两种注册消息的方法
#define ADD_IPCAMERA_RESPONSE_WITHOUT_ARG(notice_name) \
    [[NSNotificationCenter defaultCenter] addObserver: self selector:               @selector(IPCamera_response_function:) name: notice_name object: nil]

#define ADD_IPCAMERA_RESPONSE_WITH_ARG(notice_name,camera_object) \
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(IPCamera_response_function:) name: notice_name object: camera_object]
示列:打开串口写数据
1)注册消息+打开串口
ADD_IPCAMERA_RESPONSE_WITH_ARG( IPCamera_Open_Serial_Result, cam_) 
ADD_IPCAMERA_RESPONSE_WITH_ARG( IPCamera_Write_Serial_Result, cam_)
ADD_IPCAMERA_RESPONSE_WITH_ARG( IPCamera_Serial_Data, cam_)
  [cam_ openSerial ];
2)串口打开成功后开始读写数据
-(void)IPCamera_response_function:(NSNotification*)note
{    

    NSDictionary* dic = note.userInfo;

        //串口打开成功后开始读写数据
    if( [note.name isEqualToString: IPCamera_Open_Serial_Result ])
    {
        NSLog(@"串口已打开!");       
        unsigned char m_data = 0x01;
        [cam_ writeSerial:m_data length: 1]; //写数据
        [cam_ readSerial]; //读数据
    }

    //后开始写数据
    if( [note.name isEqualToString: IPCamera_Write_Serial_Result ])
    {
        NSLog(@"写数据成功!");       
    }

    if( [note.name isEqualToString: IPCamera_Serial_Data ])
    {
        NSLog(@"读数据成功!");       
    }

}
3)关闭串口

[cam_ closeSerial ];

版权所有:深圳市瑞彩电子科技有限公司

信产部备案号:粤ICP备09050685号 技术支持:800069965