gpt4 book ai didi

javascript - 在 React Native 中通过 IP 发送 TCP 数据包不起作用

转载 作者:行者123 更新时间:2023-12-05 05:30:05 25 4
gpt4 key购买 nike

我一直在尝试使用 react-native-tcp 库通过 IP 发送 TCP 十六进制数据包。我编写了一个非常非常简单的应用程序来尝试执行此操作。但是,代码中的函数sendPackets 似乎从来没有运行过。我认为这可能是我在按钮中实现 onPress 方法的问题,但是我不能排除我的 sendPackets 函数编码不正确的可能性。

这是应用程序的代码

import React, { useState } from "react";
import {
View,
Text,
TextInput,
Button,
SafeAreaView,
StyleSheet,
TouchableOpacity,
} from "react-native";
import TcpSocket from "react-native-tcp";
import { Buffer } from "buffer";

const sendPacket = () => {
console.log("sendPacket called");
try {
const packet = Buffer.from(
"0x038, 0x031, 0x020, 0x030, 0x031, 0x020, 0x030, 0x036, 0x020, 0x030, 0x031, 0x020, 0x030, 0x032, 0x020, 0x030, 0x032, 0x020, 0x030, 0x031, 0x020, 0x030, 0x033, 0x020, 0x066, 0x066",
"hex"
);
const client = TcpSocket.createConnection({
port: 5678,
host: "Ipaddr",
});

client.write(packet);
} catch (error) {
console.log(error);
}
};

const App = () => {
console.log("app called");

return (
<SafeAreaView>
<View>
{console.log("button Rendered")}
<TouchableOpacity onPress={sendPacket}>
<Text>Send Packet</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
};

export default App;

如果它有用:此代码用于使用索尼 VISCA 协议(protocol)通过 IP 控制 PTZ 摄像机。它发送命令 81 01 06 01 VV WW 01 03 FF,其中 V 在 0x01 和 0x18 之间变化(控制相机摇摄的速度)。 W 在 0x01 和 0x14 之间变化(控制相机的倾斜速度)

最佳答案

我注意到的第一件事是您没有向 TcpSocket.createConnection() 添加第二个参数,它应该是这样的:

const options = { port: 5678, host: "Ipaddr" };
const client = TcpSocket.createConnection(options, () => {
client.write(packet);
});

希望这对您有所帮助!

关于javascript - 在 React Native 中通过 IP 发送 TCP 数据包不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74793236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com