A simple understanding of visa protocol and its implementation

Reprinted from: https://latelee.blog.csdn.net/article/details/35811777

A simple understanding of visa protocol and its implementation

Li Chi 2014-06-30 14:09:01 7064

Category column: Programming Code life Knowledge base of migrant workers

copyright

I've been working on the visa agreement recently. It's a record to write here.

Visa is a protocol developed by Sony to control the camera. It usually communicates through rs232 (rs485 is also useful after reading some materials).

1, Command format

The basic unit of command communication is called packet. The length of a packet is 3 to 16 bytes, which is composed of header, message body and terminator. The first byte of the command packet is called the command header. The high half byte consists of 1 (the highest bit, fixed as 1) and the address of the sender (controller) (the address is generally 0), and the low half byte consists of 0 and the address (or "number") of the equipment (camera). From the composition format, there are up to 7 cameras that can be connected externally. If you send a command to camera 1, the command header is 0x81. The last byte of the command package is the terminal symbol, fixed as 0xff. The middle part of the byte is called the message body. In the protocol description document, the command header is written as "8x", where X represents the camera address.

There are two types of commands: General Command and inquiry. The former sends commands directly to the camera, while the latter obtains data from the camera.
The specific command package format is as follows:
8X QQ RR ... FF
Among them, QQ is the command classification, 01 represents the general command and 09 represents the query command. RR is the category code. X indicates the camera address. Range 1 ~ 7.

2, Respond

Each command has a response package in the following format:
X0 ... ... FF
The X range is 9~F, and the value is camera number + 8. End with FF. When sending a normal command, the camera will return an ACK response, but the query command will not return an ACK.

ACK response package format: X0 41 FF
Format of ordinary command response package: X0 51 FF
Query command response package format: x0 51 FF
Where, the X range is 9~F, which is the camera address value + 8. The response package of the query command contains data. Each kind of data is different. You can query the protocol document.

The format of the error message is as follows:
Syntax error: X0 61 02 FF
Command cancel: X0 61 04 FF
No socket: X0 61 05 FF
Command not executed: X0 61 41 FF
Where the value of X is the same as that above. The scope of "socket" is not well understood yet. These values are the basis for the code to make judgments.

3, Agreement document comments
The description of qprs in the protocol document can be directly put into the items of 16 bit hexadecimal data (the hexadecimal format is 0xAAAA). For example, if the response package format of a command is "y0 # 50 # 0p 0q # 0r # 0s # FF", the actual data is 0xpqrs. For example, "01 02 03 04", the corresponding data is 0x1234. vice versa. It can be realized by shift in the code. Here are some classic command formats.

1. Command without parameters
Camera power on CAM_Power command format: 8x # 01 # 04 # 00 # 02 # FF
"X" in "8x" indicates the camera number. Such commands can be assembled directly according to the command field.

2. Commands with parameters
Zoom cam_ The format of zoom command is 8x # 01 # 04 # 47 # 0p 0q # 0r # 0s # FF.
pqrs in "0p 0q 0r 0s" constitute the focus position parameter. When assembling the command, shift this parameter to the corresponding field in turn. Assuming the parameter value is 0x1234, the corresponding field is "01 02 03 04".
CAM_AFMode command can set two values of Active/Interval Time in the format of 8x # 01 # 04 # 27 # 0p 0q # 0r # 0s # FF
"0p 0q" corresponds to movement time and "0r 0s" corresponds to Interval. When assembling commands, assemble them separately. See above for the method.

3. Query command without parameters
Like CAM_PowerInq query command, send 8x # 09 # 04 # 00 # FF, and directly return y0 # 50 # 02 # FF or y0 # 50 # 03 # FF
Where "y0 # 50 # 02 # FF" is the returned data, and the y value is the camera number + 8. For such commands, the corresponding status can be obtained by directly reading the third byte.

4. Query command with parameters
Like CAM_ZoomPosInq command, send 8x # 09 # 04 # 47 # FF, return y0 # 50 # 0p 0q # 0r # 0s # FF
In the query commands, most commands have variable data. The "0p 0q 0r 0s" in "y0 # 50 # 0p 0q 0r 0s # FF" needs to be shifted to know the exact value, and the corresponding value is 0xpqrs.

4, Realize

Fortunately, there are libvisca open source projects about visca (see resources). The following refers to this project and makes some minor modifications to write the key implementation code.

I won't say more about the opening, reading, writing and closing of serial port here. The following describes the encapsulation of commands:

void _visca_append_byte(VISCAPacket_t *packet, unsigned char byte)
{
    packet->bytes[packet->length]=byte;
    (packet->length)++;
}

void _visca_init_packet(VISCAPacket_t *packet)
{
    // set it to null
    memset(packet->bytes, '\0', sizeof(packet->bytes));
    // we start writing at byte 1, the first byte will be filled by the
    // packet sending function(_visca_send_packet). This function will also append a terminator.
    packet->length=1;
}

These two functions are the filling of commands, and each call_ visca_append_byte is filled with a character. Before it is filled, call_ visca_init_packet to initialize the length of the packet. Of course, the implementation can also directly use the array form to send each command together_ visca_send_packet is used to fill the header and tail data, which does not need to be considered by the caller. The caller only needs to pay attention to the actual command data. This also uses the viscainterface_ The benefits of T (you will see the pelco implementation later).

int32_t _visca_send_packet(VISCAInterface_t *iface, VISCACamera_t *camera, VISCAPacket_t *packet)
{
    // check data:
    if ((iface->address>7)||(camera->address>7)||(iface->broadcast>1))
    {
        com_print("(%s): Invalid header parameters\n",__FILE__);
        com_print("addr: %d %d broadcast: %d(0x%x)\n",iface->address,camera->address,
                    iface->broadcast,iface->broadcast);
        return VISCA_FAILURE;
    }

    // build header:
    packet->bytes[0]=0x80;
    packet->bytes[0]|=(iface->address << 4);
    if (iface->broadcast>0)
    {
        packet->bytes[0]|=(iface->broadcast << 3);
        packet->bytes[0]&=0xF8;
    }
    else
    {
        packet->bytes[0]|=camera->address;
    }

    // append footer(0xff)
    _visca_append_byte(packet,VISCA_TERMINATOR);

    return _visca_write_packet_data(iface,packet);
}

The command response package functions are as follows:

int32_t _visca_get_reply(VISCAInterface_t *iface, VISCACamera_t *camera)
{
    // first message: -------------------
    if (_visca_get_packet(iface)!= VISCA_SUCCESS)
        return VISCA_FAILURE;

    iface->type=iface->ibuf[1]&0xF0;

    // skip ack messages
    while (iface->type==VISCA_RESPONSE_ACK)
    {
        if (_visca_get_packet(iface)!=VISCA_SUCCESS)
            return VISCA_FAILURE;
        iface->type=iface->ibuf[1]&0xF0;
    }

    switch (iface->type)
    {
    case VISCA_RESPONSE_CLEAR:
        return VISCA_SUCCESS;
        break;
    case VISCA_RESPONSE_ADDRESS:
        return VISCA_SUCCESS;
        break;
    case VISCA_RESPONSE_COMPLETED:
        return VISCA_SUCCESS;
        break;
    case VISCA_RESPONSE_ERROR:
        return VISCA_CMDERROR;
        break;
    }

    return VISCA_FAILURE;
}

Some macros are defined as follows:

/* response types */
#define VISCA_RESPONSE_CLEAR             0x40
#define VISCA_RESPONSE_ADDRESS           0x30
#define VISCA_RESPONSE_ACK               0x40
#define VISCA_RESPONSE_COMPLETED         0x50
#define VISCA_RESPONSE_ERROR             0x60

In fact, the judgment is also very simple, that is, judge one by one according to the error code given in the agreement.
For other codes, you can directly refer to libvisca, which is not listed here.

 

Reference resources:

1. Open source visca protocol library: libvisca: http://damien.douxchamps.net/libvisca/
2. wiki introduction of visa protocol: http://en.wikipedia.org/wiki/VISCA_Protocol

 

Postscript: as of the time of writing this article, hardware resources have not been available. The article is written according to the description in libvisca and visca protocol documents, which should not have practical value.

Li Chi Ji on June 30, 2014

Added by Patrick on Tue, 01 Feb 2022 11:49:33 +0200