"-1" needed after a int variable, or is it an int var?

I'm trying to understand the use of a int variable used for a serial port number. (from RE232.H 2016, Example code has No "-1" used here -> http://cc.bingj.com/cache.aspx?q=%22RS232_SendBuf%22&d=4562511026390469&mkt=en-US&setlang=en-US&w=vTNhPMMo3EwaynsXkoqfxcLypDxFVl16

My variable (port_nr=0) has values like: 0, 1, 2, etc.

Im my test code, I can open the com port using the below code, without the "-1" after the 'port_nr'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void SerialDemFrame::OnconBut2Click(wxCommandEvent& event)
{
   int baudrate=9600;
   port_nr=0; // 0 = Com1, 1 = Com2, etc.
   char mode[] = {'8','N','1',0};

 wxString comSelected = comSel->GetStringSelection();
   int port_aux = 0;
      if(comSelected.size() > 1)
      {
        port_aux = (int) comSelected[3] - 48;
        if(port_nr == 0)
        {
            //Syntax: RS232_OpenComport(com port number, baudrate, mode)
            if(!RS232_OpenComport(port_nr, baudrate, mode))
            {
                port_nr = port_aux;
                recBox->SetValue((char)(port_aux+48));
            }
        }
    }
}


But in Sending a string via serial port using the example syntax,
void RS232_cputs(int comport_number, const char *text)

my 'port_nr' variable Must be followed by a "-1".
I am trying to learn why the "-1" is necessary in this particular location and no where else.

When I remove the "-1" the serial message does not send.
When I add 'int' to my declaration like this: int port_nr=0, with and without "-1", the message does not send.

This is my full Sending code:
1
2
3
4
5
6
7
8
9
10
void SerialDemFrame::OnsendBut2Click(wxCommandEvent& event)
{
  wxString sendMesg = mesBox->GetValue();
    string sended;

    sended = sendMesg.ToStdString();
    if(port_nr > 0)
  
     RS232_cputs(port_nr-1, (char*)sended.c_str());
}


Why can't I just Type the 'port_nr' as int, and use it throughout, without the "-1"?
Last edited on
What does RS232_cputs() expect as its first parameter? Is 0 a valid input? I suspect you are reading in a port number starting at 1 but the actual function wants a port number starting at 0.
What does RS232_cputs() expect as its first parameter? Is 0 a valid input? I suspect you are reading in a port number starting at 1 but the actual function wants a port number starting at 0.


RS232_cput use: void RS232_cputs(int comport_number, const char *text)

It expects an Integer, I just placed an actual 0(zero) in place of the 'port_nr-1" and it worked!
So 0 is valid.

This demo takes a COM1 value from a combo box and converts it to 0.
Last edited on
I solved this by creating a variable (int port_nr=0;) in the Send procedure.
Now the 'port_nr' variable alone satisfies the 'RS232_cput' requirement.

I'm still searching for the '-1' answer, but I'll sleep tonight...
My guess is the same as it was. The function probably takes integers where 0 is port number 1, 1 is port number 2, and so on. If you read in the port number directly you have to subtract one to get the offset (port 1 is offset no spaces from the start, port 2 is offset one port from the start, and so on). Similar to arrays.
Thanks Zhuge.
Topic archived. No new replies allowed.