diff --git a/bindings/c/CMakeLists.txt b/bindings/c/CMakeLists.txt index a11a6b0..753f963 100644 --- a/bindings/c/CMakeLists.txt +++ b/bindings/c/CMakeLists.txt @@ -15,6 +15,8 @@ cmake_minimum_required(VERSION 3.8.2) project(cserialport) add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode +# add_definitions(-DCSERIALPORT_USE_UTF8) # CSerialPort UTF8 Character Encoding +add_definitions(-DCSERIALPORT_BINDING_LANGUAGE=C) # CSerialPort Binding Language # set output directory set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/bindings/csharp/CMakeLists.txt b/bindings/csharp/CMakeLists.txt index 69b345f..4ea646c 100644 --- a/bindings/csharp/CMakeLists.txt +++ b/bindings/csharp/CMakeLists.txt @@ -14,7 +14,9 @@ cmake_minimum_required(VERSION 3.8.2) project(libcserialport) -# add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode +add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode +# add_definitions(-DCSERIALPORT_USE_UTF8) # CSerialPort UTF8 Character Encoding +add_definitions(-DCSERIALPORT_BINDING_LANGUAGE=CSharp) # CSerialPort Binding Language # set output directory set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/bindings/java/CMakeLists.txt b/bindings/java/CMakeLists.txt index 2ff7af5..ac2e560 100644 --- a/bindings/java/CMakeLists.txt +++ b/bindings/java/CMakeLists.txt @@ -16,6 +16,7 @@ project(cserialport) add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode add_definitions(-DCSERIALPORT_USE_UTF8) # CSerialPort UTF8 Character Encoding +add_definitions(-DCSERIALPORT_BINDING_LANGUAGE=Java) # CSerialPort Binding Language # set output directory set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/bindings/javascript/CMakeLists.txt b/bindings/javascript/CMakeLists.txt index 441101f..00df2df 100644 --- a/bindings/javascript/CMakeLists.txt +++ b/bindings/javascript/CMakeLists.txt @@ -14,8 +14,9 @@ cmake_minimum_required(VERSION 3.8.2) project(cserialport) -# add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode +add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode add_definitions(-DCSERIALPORT_USE_UTF8) # CSerialPort UTF8 Character Encoding +add_definitions(-DCSERIALPORT_BINDING_LANGUAGE=JavaScript) # CSerialPort Binding Language # set output directory # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/bindings/javascript/binding.gyp b/bindings/javascript/binding.gyp index b017650..2b1d11f 100644 --- a/bindings/javascript/binding.gyp +++ b/bindings/javascript/binding.gyp @@ -2,7 +2,7 @@ 'targets': [ { 'target_name': 'cserialport', - 'defines': ['CSERIALPORT_USE_UTF8'], + 'defines': ['CSERIALPORT_DEBUG','CSERIALPORT_USE_UTF8','CSERIALPORT_BINDING_LANGUAGE=JavaScript'], 'include_dirs': ['../../include'], 'sources': [ '../../src/SerialPort.cpp', diff --git a/bindings/nodejs/CMakeLists.txt b/bindings/nodejs/CMakeLists.txt index 6e9cb11..444c094 100644 --- a/bindings/nodejs/CMakeLists.txt +++ b/bindings/nodejs/CMakeLists.txt @@ -14,8 +14,9 @@ cmake_minimum_required(VERSION 2.8.12) project(cserialport) -# add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode +add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode add_definitions(-DCSERIALPORT_USE_UTF8) # CSerialPort UTF8 Character Encoding +add_definitions(-DCSERIALPORT_BINDING_LANGUAGE=Node.js) # CSerialPort Binding Language set(CMAKE_CXX_STANDARD 11) #add_compile_options(-g) diff --git a/bindings/nodejs/binding.gyp.bak b/bindings/nodejs/binding.gyp.bak index ff1041d..6c46624 100644 --- a/bindings/nodejs/binding.gyp.bak +++ b/bindings/nodejs/binding.gyp.bak @@ -2,7 +2,7 @@ 'targets': [ { 'target_name': 'cserialport', - 'defines': ['CSERIALPORT_USE_UTF8'], + 'defines': ['CSERIALPORT_DEBUG','CSERIALPORT_USE_UTF8','CSERIALPORT_BINDING_LANGUAGE=Node.js'], 'cflags!': [ '-fno-exceptions' ], 'cflags_cc!': [ '-fno-exceptions' ], 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index 25e46a2..c1f2524 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -14,8 +14,9 @@ cmake_minimum_required(VERSION 3.8.2) project(cserialport) -# add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode +add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode add_definitions(-DCSERIALPORT_USE_UTF8) # CSerialPort UTF8 Character Encoding +add_definitions(-DCSERIALPORT_BINDING_LANGUAGE=Python) # CSerialPort Binding Language # set output directory set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/bindings/rust/CMakeLists.txt b/bindings/rust/CMakeLists.txt index f433bf9..9c74f6d 100644 --- a/bindings/rust/CMakeLists.txt +++ b/bindings/rust/CMakeLists.txt @@ -16,6 +16,7 @@ project(cserialport) add_definitions(-DCSERIALPORT_DEBUG) # CSerialPort Debug Mode add_definitions(-DCSERIALPORT_USE_UTF8) # CSerialPort UTF8 Character Encoding +add_definitions(-DCSERIALPORT_BINDING_LANGUAGE=Rust) # CSerialPort Binding Language # set output directory set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/bindings/rust/README.md b/bindings/rust/README.md index 0be3d2c..c35f294 100644 --- a/bindings/rust/README.md +++ b/bindings/rust/README.md @@ -4,6 +4,7 @@ rust: 1.83.0 (90b35a623 2024-11-26) bindgen: 0.71.1 cmake: 3.26.4 +llvm(libclang): 18.1.8 ``` ## Install cmake diff --git a/bindings/rust/bindings.rs b/bindings/rust/bindings.rs index c7fff85..86ce14c 100644 --- a/bindings/rust/bindings.rs +++ b/bindings/rust/bindings.rs @@ -1,5 +1,6 @@ /* automatically generated by rust-bindgen 0.71.1 */ +pub type i_handle_t = ::std::os::raw::c_ulonglong; #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct SerialPortInfo { @@ -36,21 +37,6 @@ const _: () = { pub const OperateMode_AsynchronousOperate: OperateMode = 0; pub const OperateMode_SynchronousOperate: OperateMode = 1; pub type OperateMode = ::std::os::raw::c_int; -pub const BaudRate_BaudRate110: BaudRate = 110; -pub const BaudRate_BaudRate300: BaudRate = 300; -pub const BaudRate_BaudRate600: BaudRate = 600; -pub const BaudRate_BaudRate1200: BaudRate = 1200; -pub const BaudRate_BaudRate2400: BaudRate = 2400; -pub const BaudRate_BaudRate4800: BaudRate = 4800; -pub const BaudRate_BaudRate9600: BaudRate = 9600; -pub const BaudRate_BaudRate14400: BaudRate = 14400; -pub const BaudRate_BaudRate19200: BaudRate = 19200; -pub const BaudRate_BaudRate38400: BaudRate = 38400; -pub const BaudRate_BaudRate56000: BaudRate = 56000; -pub const BaudRate_BaudRate57600: BaudRate = 57600; -pub const BaudRate_BaudRate115200: BaudRate = 115200; -pub const BaudRate_BaudRate921600: BaudRate = 921600; -pub type BaudRate = ::std::os::raw::c_int; pub const DataBits_DataBits5: DataBits = 5; pub const DataBits_DataBits6: DataBits = 6; pub const DataBits_DataBits7: DataBits = 7; @@ -70,37 +56,16 @@ pub const FlowControl_FlowNone: FlowControl = 0; pub const FlowControl_FlowHardware: FlowControl = 1; pub const FlowControl_FlowSoftware: FlowControl = 2; pub type FlowControl = ::std::os::raw::c_int; -pub const SerialPortError_ErrorUnknown: SerialPortError = -1; -pub const SerialPortError_ErrorOK: SerialPortError = 0; -pub const SerialPortError_ErrorFail: SerialPortError = 1; -pub const SerialPortError_ErrorNotImplemented: SerialPortError = 2; -pub const SerialPortError_ErrorInner: SerialPortError = 3; -pub const SerialPortError_ErrorNullPointer: SerialPortError = 4; -pub const SerialPortError_ErrorInvalidParam: SerialPortError = 5; -pub const SerialPortError_ErrorAccessDenied: SerialPortError = 6; -pub const SerialPortError_ErrorOutOfMemory: SerialPortError = 7; -pub const SerialPortError_ErrorTimeout: SerialPortError = 8; -pub const SerialPortError_ErrorNotInit: SerialPortError = 9; -pub const SerialPortError_ErrorInitFailed: SerialPortError = 10; -pub const SerialPortError_ErrorAlreadyExist: SerialPortError = 11; -pub const SerialPortError_ErrorNotExist: SerialPortError = 12; -pub const SerialPortError_ErrorAlreadyOpen: SerialPortError = 13; -pub const SerialPortError_ErrorNotOpen: SerialPortError = 14; -pub const SerialPortError_ErrorOpenFailed: SerialPortError = 15; -pub const SerialPortError_ErrorCloseFailed: SerialPortError = 16; -pub const SerialPortError_ErrorWriteFailed: SerialPortError = 17; -pub const SerialPortError_ErrorReadFailed: SerialPortError = 18; -pub type SerialPortError = ::std::os::raw::c_int; pub type pFunReadEvent = ::std::option::Option< unsafe extern "C" fn( - arg1: *mut ::std::os::raw::c_void, + arg1: i_handle_t, arg2: *const ::std::os::raw::c_char, arg3: ::std::os::raw::c_uint, ), >; pub type pFunHotPlugEvent = ::std::option::Option< unsafe extern "C" fn( - arg1: *mut ::std::os::raw::c_void, + arg1: i_handle_t, arg2: *const ::std::os::raw::c_char, arg3: ::std::os::raw::c_int, ), @@ -112,14 +77,14 @@ unsafe extern "C" { pub fn CSerialPortAvailablePortInfosFree(portInfoArray: *mut SerialPortInfoArray); } unsafe extern "C" { - pub fn CSerialPortMalloc() -> *mut ::std::os::raw::c_void; + pub fn CSerialPortMalloc() -> i_handle_t; } unsafe extern "C" { - pub fn CSerialPortFree(pSerialPort: *mut ::std::os::raw::c_void); + pub fn CSerialPortFree(handle: i_handle_t); } unsafe extern "C" { pub fn CSerialPortInit( - pSerialPort: *mut ::std::os::raw::c_void, + handle: i_handle_t, portName: *const ::std::os::raw::c_char, baudRate: ::std::os::raw::c_int, parity: Parity, @@ -130,193 +95,146 @@ unsafe extern "C" { ); } unsafe extern "C" { - pub fn CSerialPortSetOperateMode( - pSerialPort: *mut ::std::os::raw::c_void, - operateMode: OperateMode, - ); + pub fn CSerialPortSetOperateMode(handle: i_handle_t, operateMode: OperateMode); } unsafe extern "C" { - pub fn CSerialPortOpen(pSerialPort: *mut ::std::os::raw::c_void) -> ::std::os::raw::c_int; + pub fn CSerialPortOpen(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortClose(pSerialPort: *mut ::std::os::raw::c_void); + pub fn CSerialPortClose(handle: i_handle_t); } unsafe extern "C" { - pub fn CSerialPortIsOpen(pSerialPort: *mut ::std::os::raw::c_void) -> ::std::os::raw::c_int; + pub fn CSerialPortIsOpen(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { pub fn CSerialPortConnectReadEvent( - pSerialPort: *mut ::std::os::raw::c_void, + handle: i_handle_t, pFun: pFunReadEvent, ) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortDisconnectReadEvent( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_int; + pub fn CSerialPortDisconnectReadEvent(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { pub fn CSerialPortConnectHotPlugEvent( - pSerialPort: *mut ::std::os::raw::c_void, + handle: i_handle_t, pFun: pFunHotPlugEvent, ) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortDisconnectHotPlugEvent( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_int; + pub fn CSerialPortDisconnectHotPlugEvent(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortGetReadBufferUsedLen( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_uint; + pub fn CSerialPortGetReadBufferUsedLen(handle: i_handle_t) -> ::std::os::raw::c_uint; } unsafe extern "C" { pub fn CSerialPortReadData( - pSerialPort: *mut ::std::os::raw::c_void, + handle: i_handle_t, data: *mut ::std::os::raw::c_void, size: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int; } unsafe extern "C" { pub fn CSerialPortReadAllData( - pSerialPort: *mut ::std::os::raw::c_void, + handle: i_handle_t, data: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int; } unsafe extern "C" { pub fn CSerialPortReadLineData( - pSerialPort: *mut ::std::os::raw::c_void, + handle: i_handle_t, data: *mut ::std::os::raw::c_void, size: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int; } unsafe extern "C" { pub fn CSerialPortWriteData( - pSerialPort: *mut ::std::os::raw::c_void, + handle: i_handle_t, data: *const ::std::os::raw::c_void, size: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortSetDebugModel( - pSerialPort: *mut ::std::os::raw::c_void, - isDebug: ::std::os::raw::c_int, - ); + pub fn CSerialPortSetDebugModel(handle: i_handle_t, isDebug: ::std::os::raw::c_int); } unsafe extern "C" { - pub fn CSerialPortSetReadIntervalTimeout( - pSerialPort: *mut ::std::os::raw::c_void, - msecs: ::std::os::raw::c_uint, - ); + pub fn CSerialPortSetReadIntervalTimeout(handle: i_handle_t, msecs: ::std::os::raw::c_uint); } unsafe extern "C" { - pub fn CSerialPortGetReadIntervalTimeout( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_uint; + pub fn CSerialPortGetReadIntervalTimeout(handle: i_handle_t) -> ::std::os::raw::c_uint; } unsafe extern "C" { pub fn CSerialPortSetMinByteReadNotify( - pSerialPort: *mut ::std::os::raw::c_void, + handle: i_handle_t, minByteReadNotify: ::std::os::raw::c_uint, ); } unsafe extern "C" { - pub fn CSerialPortFlushBuffers( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_int; + pub fn CSerialPortFlushBuffers(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortFlushReadBuffers( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_int; + pub fn CSerialPortFlushReadBuffers(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortFlushWriteBuffers( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_int; + pub fn CSerialPortFlushWriteBuffers(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortGetLastError( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_int; + pub fn CSerialPortGetLastError(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortGetLastErrorMsg( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> *const ::std::os::raw::c_char; + pub fn CSerialPortGetLastErrorMsg(handle: i_handle_t) -> *const ::std::os::raw::c_char; } unsafe extern "C" { - pub fn CSerialPortClearError(pSerialPort: *mut ::std::os::raw::c_void); + pub fn CSerialPortClearError(handle: i_handle_t); } unsafe extern "C" { - pub fn CSerialPortSetPortName( - pSerialPort: *mut ::std::os::raw::c_void, - portName: *const ::std::os::raw::c_char, - ); + pub fn CSerialPortSetPortName(handle: i_handle_t, portName: *const ::std::os::raw::c_char); } unsafe extern "C" { - pub fn CSerialPortGetPortName( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> *const ::std::os::raw::c_char; + pub fn CSerialPortGetPortName(handle: i_handle_t) -> *const ::std::os::raw::c_char; } unsafe extern "C" { - pub fn CSerialPortSetBaudRate( - pSerialPort: *mut ::std::os::raw::c_void, - baudRate: ::std::os::raw::c_int, - ); + pub fn CSerialPortSetBaudRate(handle: i_handle_t, baudRate: ::std::os::raw::c_int); } unsafe extern "C" { - pub fn CSerialPortGetBaudRate( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_int; + pub fn CSerialPortGetBaudRate(handle: i_handle_t) -> ::std::os::raw::c_int; } unsafe extern "C" { - pub fn CSerialPortSetParity(pSerialPort: *mut ::std::os::raw::c_void, parity: Parity); + pub fn CSerialPortSetParity(handle: i_handle_t, parity: Parity); } unsafe extern "C" { - pub fn CSerialPortGetParity(pSerialPort: *mut ::std::os::raw::c_void) -> Parity; + pub fn CSerialPortGetParity(handle: i_handle_t) -> Parity; } unsafe extern "C" { - pub fn CSerialPortSetDataBits(pSerialPort: *mut ::std::os::raw::c_void, dataBits: DataBits); + pub fn CSerialPortSetDataBits(handle: i_handle_t, dataBits: DataBits); } unsafe extern "C" { - pub fn CSerialPortGetDataBits(pSerialPort: *mut ::std::os::raw::c_void) -> DataBits; + pub fn CSerialPortGetDataBits(handle: i_handle_t) -> DataBits; } unsafe extern "C" { - pub fn CSerialPortSetStopBits(pSerialPort: *mut ::std::os::raw::c_void, stopbits: StopBits); + pub fn CSerialPortSetStopBits(handle: i_handle_t, stopbits: StopBits); } unsafe extern "C" { - pub fn CSerialPortGetStopBits(pSerialPort: *mut ::std::os::raw::c_void) -> StopBits; + pub fn CSerialPortGetStopBits(handle: i_handle_t) -> StopBits; } unsafe extern "C" { - pub fn CSerialPortSetFlowControl( - pSerialPort: *mut ::std::os::raw::c_void, - flowControl: FlowControl, - ); + pub fn CSerialPortSetFlowControl(handle: i_handle_t, flowControl: FlowControl); } unsafe extern "C" { - pub fn CSerialPortGetFlowControl(pSerialPort: *mut ::std::os::raw::c_void) -> FlowControl; + pub fn CSerialPortGetFlowControl(handle: i_handle_t) -> FlowControl; } unsafe extern "C" { - pub fn CSerialPortSetReadBufferSize( - pSerialPort: *mut ::std::os::raw::c_void, - size: ::std::os::raw::c_uint, - ); + pub fn CSerialPortSetReadBufferSize(handle: i_handle_t, size: ::std::os::raw::c_uint); } unsafe extern "C" { - pub fn CSerialPortGetReadBufferSize( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_uint; + pub fn CSerialPortGetReadBufferSize(handle: i_handle_t) -> ::std::os::raw::c_uint; } unsafe extern "C" { - pub fn CSerialPortSetDtr(pSerialPort: *mut ::std::os::raw::c_void, set: ::std::os::raw::c_int); + pub fn CSerialPortSetDtr(handle: i_handle_t, set: ::std::os::raw::c_int); } unsafe extern "C" { - pub fn CSerialPortSetRts(pSerialPort: *mut ::std::os::raw::c_void, set: ::std::os::raw::c_int); + pub fn CSerialPortSetRts(handle: i_handle_t, set: ::std::os::raw::c_int); } unsafe extern "C" { - pub fn CSerialPortGetVersion( - pSerialPort: *mut ::std::os::raw::c_void, - ) -> *const ::std::os::raw::c_char; + pub fn CSerialPortGetVersion(handle: i_handle_t) -> *const ::std::os::raw::c_char; } diff --git a/bindings/rust/example/src/main.rs b/bindings/rust/example/src/main.rs index 622869f..c76b8f3 100644 --- a/bindings/rust/example/src/main.rs +++ b/bindings/rust/example/src/main.rs @@ -14,7 +14,7 @@ extern "C" {} static mut COUNT_READ: u32 = 0; unsafe extern "C" fn on_read_event( - serial_port: *mut ::std::os::raw::c_void, + handle: i_handle_t, port_name: *const ::std::os::raw::c_char, read_buffer_len: ::std::os::raw::c_uint, ) { @@ -23,7 +23,7 @@ unsafe extern "C" fn on_read_event( let data_ptr = data.as_ptr() as *mut c_void; // read - let rec_len = CSerialPortReadData(serial_port, data_ptr, read_buffer_len as c_int); + let rec_len = CSerialPortReadData(handle, data_ptr, read_buffer_len as c_int); if rec_len > 0 { let data_str = String::from_utf8_lossy(&data[..rec_len as usize]); @@ -42,17 +42,13 @@ unsafe extern "C" fn on_read_event( ); // return receive data - CSerialPortWriteData( - serial_port, - data.as_ptr() as *const c_void, - rec_len as c_int, - ); + CSerialPortWriteData(handle, data.as_ptr() as *const c_void, rec_len as c_int); } } } unsafe extern "C" fn hot_plug_event( - serial_port: *mut ::std::os::raw::c_void, + handle: i_handle_t, port_name: *const ::std::os::raw::c_char, is_add: ::std::os::raw::c_int, ) { @@ -63,9 +59,9 @@ unsafe extern "C" fn hot_plug_event( fn main() { unsafe { - let serial_port = CSerialPortMalloc(); + let handle = CSerialPortMalloc(); - let version = CStr::from_ptr(CSerialPortGetVersion(serial_port)); + let version = CStr::from_ptr(CSerialPortGetVersion(handle)); println!("Version: {}", version.to_str().unwrap()); let mut port_info_array = SerialPortInfoArray { @@ -75,9 +71,9 @@ fn main() { CSerialPortAvailablePortInfosMalloc(&mut port_info_array); // connect for read - CSerialPortConnectReadEvent(serial_port, Some(on_read_event)); + CSerialPortConnectReadEvent(handle, Some(on_read_event)); // connect for hot plug - CSerialPortConnectHotPlugEvent(serial_port, Some(hot_plug_event)); + CSerialPortConnectHotPlugEvent(handle, Some(hot_plug_event)); if 0 == port_info_array.size { println!("No Valid Port"); @@ -117,7 +113,7 @@ fn main() { println!("Port Name: {}", port_name_str); CSerialPortInit( - serial_port, + handle, port_name.as_ptr(), // windows:COM1 Linux:/dev/ttyS0 9600, // baudrate Parity_ParityNone, // parity @@ -126,32 +122,32 @@ fn main() { FlowControl_FlowNone, // flow 4096, // read buffer size ); - CSerialPortSetReadIntervalTimeout(serial_port, 0); // read interval timeout + CSerialPortSetReadIntervalTimeout(handle, 0); // read interval timeout - CSerialPortOpen(serial_port); + CSerialPortOpen(handle); - if 1 == CSerialPortIsOpen(serial_port) { + if 1 == CSerialPortIsOpen(handle) { println!("Open {} Success", port_name_str); } else { println!("Open {} Failed", port_name_str); } - let code = CSerialPortGetLastError(serial_port); - let message = CStr::from_ptr(CSerialPortGetLastErrorMsg(serial_port)); + let code = CSerialPortGetLastError(handle); + let message = CStr::from_ptr(CSerialPortGetLastErrorMsg(handle)); let message_str = message.to_str().unwrap(); println!("Code: {}, Message: {}", code, message_str); // write hex data let hex: [u8; 5] = [0x31, 0x32, 0x33, 0x34, 0x35]; - CSerialPortWriteData(serial_port, hex.as_ptr() as *const c_void, 5); + CSerialPortWriteData(handle, hex.as_ptr() as *const c_void, 5); // write str data let data = CString::new("itas109").unwrap(); - CSerialPortWriteData(serial_port, data.as_ptr() as *const c_void, 7); + CSerialPortWriteData(handle, data.as_ptr() as *const c_void, 7); } loop {} // CSerialPortAvailablePortInfosFree(&mut port_info_array); - // CSerialPortFree(serial_port); + // CSerialPortFree(handle); } }