1.0.0 • Published 7 months ago
react-native-serialport-windows v1.0.0
react-native-serialport-windows
Serial Communication for React Native Windows apps
Installation
npm install react-native-serialport-windows
import {
listPorts,
openPort,
closePort,
write,
eventEmitter,
} from 'react-native-serialport-windows';
Usage
1. List available ports
Retrieve a list of all available serial ports:
const availablePorts = await listPorts();
1. Open a port
Open a port with the desired settings:
await openPort('COM1', 9600, 8, 1, 0, 0);
// Parameters: portName, baudRate, dataBits, stopBits, parity, flowControl
2. Write data
Send data to the serial port. Data should be an array of byte values:
const data = [0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello"
await write(data);
3. Receive data
Listen for incoming data using the eventEmitter:
useEffect(() => {
const subscription = eventEmitter.addListener(
'SerialPortDataReceived',
({ data }) => {
const text = String.fromCharCode(...data);
console.log('Received:', text);
}
);
return () => subscription.remove();
}, []);
4. Close the port
When finished, ensure the port is closed:
await closePort();
Example
git clone https://github.com/MihirGrand/react-native-serialport-windows.git
cd react-native-serialport-windows
npm install
cd example
npm run windows
Contributing
git checkout -b my-feature-branch
git add .
git commit -m "feat: New feature description"
git push origin my-feature-branch
and create a PR!
License
MIT
1.0.0
7 months ago