简体   繁体   English

c#可以通过串口发送数据但不能接收

[英]c# Can send data though serial port but not receive

so I have a PTZ camera connected to the PC though a serial port and I can send commands to it to control it.所以我有一个 PTZ 摄像机通过串行端口连接到 PC,我可以向它发送命令来控制它。 My problem is when I ask it which zoom it has, or anything, I will never get a response.我的问题是,当我问它有哪个变焦或其他任何东西时,我永远不会得到回应。

First, I open my Serial port:首先,我打开我的串口:

public static void StartCOM(string COMPort, ref SerialPort serialPort)
{
    serialPort = new SerialPort(COMPort, 9600, Parity.None, 8, StopBits.One);
    serialPort.Handshake = Handshake.None;
    serialPort.DtrEnable = true;
    serialPort.RtsEnable = true;
    serialPort.ReadTimeout = 500;
}

Then I add the event to trigger when I receive data:然后我添加事件以在接收数据时触发:

serialPort.DataReceived += new SerialDataReceivedEventHandler(OnDataReceived);
private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Debug.Log("Data recibida");
    Debug.Log(serialPort.ReadExisting());
}

And then I open it:然后我打开它:

public static void OpenCOM(ref SerialPort serialPort)
{
    //Comprobamos si el serial port existe
    if (serialPort != null)
    {
        //Si ya está abierto no hacemos nada
        if (serialPort.IsOpen)
        {
            Debug.Log("El puerto " + serialPort.PortName + " ya estaba abierto");
        }
        //Si está cerrado lo abrimos
        else
        {
            Debug.Log("Abrimos puerto: " + serialPort.PortName);
            serialPort.Open();
        }
    }
}

Finally, I write to it a command that is supossed to return me something, but it never does:最后,我给它写了一个命令,它应该返回给我一些东西,但它从来没有:

 byte[] byteToSend = { 0x81, 0x09, 0x04, 0x00, 0xFF };
 COMUtility.WriteToCOM(serialPort, byteToSend);

The page where I get the commands from its: https://www.epiphan.com/userguides/LUMiO12x/Content/UserGuides/PTZ/3-operation/VISCAcommands.htm#CamValues我从其获取命令的页面: https ://www.epiphan.com/userguides/LUMiO12x/Content/UserGuides/PTZ/3-operation/VISCAcommands.htm#CamValues

UPDATE:更新:

So, I started to read data.于是,我开始阅读资料。

i write to the COM port:我写到 COM 端口:

    if (Input.GetKeyDown(KeyCode.R))
    {
        //DebugAbsolutePosition();
        byte[] checkZoom = {0x81, 0x09, 0x04, 0x47, 0xFF };
        COMUtility.WriteToCOM(serialPort, checkZoom);
    }

    try
    {
        string data = serialPort.ReadExisting();
        Debug.Log("Data leida");
        Debug.Log(data);
    }
    catch { }

This checks the cameras zoom.这将检查相机变焦。 And in the external software I Receive在我收到的外部软件中
90 50 00 00 00 00 ff, which is correct. 90 50 00 00 00 00 ff,这是正确的。

My problem is that in my app if I debug im getting:我的问题是,在我的应用程序中,如果我调试我得到:

"?P\0\0". “?P\0\0”。

You are probably making a mistake setting the hardware flow control.您可能在设置硬件流控制时犯了错误。

According to the documentation of your camera you should only be using RTS/CTS but you are setting up your port for both RTS/CTS and DTR/DSR.根据您的相机文档,您应该只使用 RTS/CTS,但您正在为 RTS/CTS 和 DTR/DSR 设置端口。

It might be a good idea to drop flow control altogether, if only to start with.如果只是一开始,完全放弃流量控制可能是个好主意。 For such slow baud rates and short data exchanges you probably won't need any kind of flow control anyway.对于如此慢的波特率和短数据交换,您可能无论如何都不需要任何类型的流量控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM