简体   繁体   中英

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. 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

UPDATE:

So, I started to read data.

i write to the COM port:

    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.

My problem is that in my app if I debug im getting:

"?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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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