index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { NativeModules } from 'react-native';
  2. const { RNSerialport } = NativeModules;
  3. const definitions = {
  4. DATA_BITS :{
  5. DATA_BITS_5: 5,
  6. DATA_BITS_6: 6,
  7. DATA_BITS_7: 7,
  8. DATA_BITS_8: 8
  9. },
  10. STOP_BITS: {
  11. STOP_BITS_1 : 1,
  12. STOP_BITS_15: 3,
  13. STOP_BITS_2 : 2
  14. },
  15. PARITIES: {
  16. PARITY_NONE : 0,
  17. PARITY_ODD : 1,
  18. PARITY_EVEN : 2,
  19. PARITY_MARK : 3,
  20. PARITY_SPACE: 4
  21. },
  22. FLOW_CONTROLS: {
  23. FLOW_CONTROL_OFF : 0,
  24. FLOW_CONTROL_RTS_CTS : 1,
  25. FLOW_CONTROL_DSR_DTR : 2,
  26. FLOW_CONTROL_XON_XOFF: 3
  27. },
  28. RETURNED_DATA_TYPES: {
  29. INTARRAY : 1,
  30. HEXSTRING: 2
  31. },
  32. DRIVER_TYPES: {
  33. AUTO : "AUTO",
  34. CDC : "cdc",
  35. CH34x : "ch34x",
  36. CP210x : "cp210x",
  37. FTDI : "ftdi",
  38. PL2303 : "pl2303"
  39. }
  40. };
  41. const actions = {
  42. ON_SERVICE_STARTED : 'onServiceStarted',
  43. ON_SERVICE_STOPPED : 'onServiceStopped',
  44. ON_DEVICE_ATTACHED : 'onDeviceAttached',
  45. ON_DEVICE_DETACHED : 'onDeviceDetached',
  46. ON_ERROR : 'onError',
  47. ON_CONNECTED : 'onConnected',
  48. ON_DISCONNECTED : 'onDisconnected',
  49. ON_READ_DATA : 'onReadDataFromPort'
  50. };
  51. RNSerialport.intArrayToUtf16 = (intArray) => {
  52. var str = "";
  53. for (var i = 0; i < intArray.length; i++) {
  54. str += String.fromCharCode(intArray[i]);
  55. }
  56. return str;
  57. }
  58. RNSerialport.hexToUtf16 = (hex) => {
  59. var str = "";
  60. var radix = 16;
  61. for (var i = 0; i < hex.length && hex.substr(i, 2) !== "00"; i += 2) {
  62. str += String.fromCharCode(parseInt(hex.substr(i, 2), radix));
  63. }
  64. return str;
  65. }
  66. module.exports = { RNSerialport, definitions, actions };