ASCII support in libmodbus

Asked by Ajith James

Hiii
I am using the libmodbus, I have to communicate with a modbus client simulator which is in serial ascii. Is there any option in libmodbus for communicating with serial ascii devices ,Or could you please tell me some sample code for this communication.

Any responses are really appreciated

Thanks in Advance
Ajith James

Question information

Language:
English Edit question
Status:
Solved
For:
libmodbus Edit question
Assignee:
No assignee Edit question
Solved by:
Ajith James
Solved:
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :
#1

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Stéphane Raimbault (sra) said :
#2

No ASCII support, it's really odd to require that format.

Revision history for this message
Ajith James (ajithj) said :
#3

Thanks steffanie, Stéphane Raimbault
by writing a wrapper i made it supported to ascii also

Revision history for this message
Stéphane Raimbault (sra) said :
#4

> Thanks steffanie, Stéphane Raimbault
> by writing a wrapper i made it supported to ascii also
>

Could you provide a patch with that ASCII support, please?
Thank you.

Revision history for this message
Ajith James (ajithj) said :
#5

Sure,
I have modified your program bye adding a new type called ascii
then write a function to convert the libmodbus ascii to rtu form.
I am not sure this is a good way,I just do this trick :)

Thanks a lot,
Ajith

On Sun, Aug 2, 2009 at 4:36 AM, Stéphane Raimbault <
<email address hidden>> wrote:

> Your question #74711 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/74711
>
> Stéphane Raimbault posted a new comment:
> > Thanks steffanie, Stéphane Raimbault
> > by writing a wrapper i made it supported to ascii also
> >
>
> Could you provide a patch with that ASCII support, please?
> Thank you.
>
> --
> You received this question notification because you are a direct
> subscriber of the question.
>

Revision history for this message
Karol C. (viper-lasser) said :
#6

@Ajith James
Can you show your wrapper for modbus ascii ?
Thank in advance

Revision history for this message
Ajith James (ajithj) said :
#7

/* Builds a ASCII query from RTU */

static int build_query_basis_ascii(uint8_t * query_ascii, uint8_t * query,
int query_length)
{
    int i,j;
    for(i=((2*query_length)), j=query_length-1 ; i>0 ; )
    {

    switch(((query[j]%16)))
    {
    case 0:
            query_ascii[i] = 0x30;
        break;
    case 1:
            query_ascii[i] = 0x31;
        break;
    case 2:
            query_ascii[i] = 0x32;
        break;
    case 3:
            query_ascii[i] = 0x33;
        break;
    case 4:
            query_ascii[i] = 0x34;
        break;
    case 5:
            query_ascii[i] = 0x35;
        break;
    case 6:
            query_ascii[i] = 0x36;

        break;
    case 7:
            query_ascii[i] = 0x37;
        break;
    case 8:
            query_ascii[i] = 0x38;
        break;
    case 9:
            query_ascii[i] = 0x39;
        break;
    case 10:
            query_ascii[i] = 0x41;
        break;
    case 11:
            query_ascii[i] = 0x42;
        break;
    case 12:
            query_ascii[i] = 0x43;
        break;
    case 13:
            query_ascii[i] = 0x44;
        break;
    case 14:
            query_ascii[i] = 0x45;
        break;
    case 15:
            query_ascii[i] = 0x46;
        break;
    }

    i--;
    switch(((query[j]/16)))
    {
    case 0:
            query_ascii[i] = 0x30;
        break;
    case 1:
            query_ascii[i] = 0x31;
        break;
    case 2:
            query_ascii[i] = 0x32;
        break;
    case 3:
            query_ascii[i] = 0x33;
        break;
    case 4:
            query_ascii[i] = 0x34;
        break;
    case 5:
            query_ascii[i] = 0x35;
        break;
    case 6:
            query_ascii[i] = 0x36;

        break;
    case 7:
            query_ascii[i] = 0x37;
        break;
    case 8:
            query_ascii[i] = 0x38;
        break;
    case 9:
            query_ascii[i] = 0x39;
        break;
    case 10:
            query_ascii[i] = 0x41;
        break;
    case 11:
            query_ascii[i] = 0x42;
        break;
    case 12:
            query_ascii[i] = 0x43;
        break;
    case 13:
            query_ascii[i] = 0x44;
        break;
    case 14:
            query_ascii[i] = 0x45;
        break;
    case 15:
            query_ascii[i] = 0x46;
        break;
    }
    // query[i] = 30 +((query[j]%16));
    // query[i-1] = 30 +((query[j]/16));
        i--;
        j--;
    }

    query_ascii[0] = 0x3A;
    query_ascii[((2*query_length)+1)] = 0x0D;
    query_ascii[((2*query_length)+2)] = 0x0A;

    return ((2*query_length)+3);
}

/* extract RTU query from ASCII */

static int ext_query_basis_ascii(uint8_t * query)
{
    int i=0;
    int j= 1;
    while(j<MAX_MESSAGE_LENGTH && query[j] != 0x0D )
    {
        switch(query[j])
        {
        case 0x30:
            query[i] = 0x00;
        break;
        case 0x31:
                query[i] = 0x01;
            break;
        case 0x32:
                query[i] = 0x02;
            break;
        case 0x33:
                query[i] = 0x03;
            break;
        case 0x34:
                query[i] = 0x04;
            break;
        case 0x35:
                query[i] = 0x05;
            break;
        case 0x36:
                query[i] = 0x06;

            break;
        case 0x37:
                query[i] = 0x07;
            break;
        case 0x38:
                query[i] = 0x08;
            break;
        case 0x39:
                query[i] = 0x09;
            break;
        case 0x41:
                query[i] = 0x0A;
            break;
        case 0x42:
                query[i] = 0x0B;
            break;
        case 0x43:
                query[i] = 0x0C;
            break;
        case 0x44:
                query[i] = 0x0D;
            break;
        case 0x45:
                query[i] = 0x0E;
            break;
        case 0x46:
                query[i] = 0x0F;
            break;
        case 0x0D:
                return i;
        }
        switch(query[j+1])
        {
        case 0x30:
            query[i] =( (query[i]<<4) | 0x00 ) ;
            break;
        case 0x31:
                query[i] =( (query[i]<<4) | 0x01) ;
            break;
        case 0x32:
                query[i] =( (query[i]<<4) | 0x02) ;
            break;
        case 0x33:
                query[i] =( (query[i]<<4) | 0x03) ;
            break;
        case 0x34:
                query[i] =( (query[i]<<4) | 0x04) ;
            break;
        case 0x35:
                query[i] =( (query[i]<<4) | 0x05) ;
            break;
        case 0x36:
                query[i] =( (query[i]<<4) | 0x06) ;

            break;
        case 0x37:
                query[i] =( (query[i]<<4) | 0x07) ;
            break;
        case 0x38:
                query[i] =( (query[i]<<4) | 0x08) ;
            break;
        case 0x39:
                query[i] =( (query[i]<<4) | 0x09) ;
            break;
        case 0x41:
                query[i] =( (query[i]<<4) | 0x0A) ;
            break;
        case 0x42:
                query[i] =( (query[i]<<4) | 0x0B) ;
            break;
        case 0x43:
                query[i] =( (query[i]<<4) | 0x0C) ;
            break;
        case 0x44:
                query[i] =( (query[i]<<4) | 0x0D) ;
            break;
        case 0x45:
                query[i] =( (query[i]<<4) | 0x0E) ;
            break;
        case 0x46:
                query[i] =( (query[i]<<4) | 0x0F) ;
            break;
        case 0x0D:
                return i;
        }

        j+= 2;
        i++;
    }
    return i;

}

void modbus_init_ascii(modbus_param_t *mb_param, const char *device,
                     int baud, const char *parity, int data_bit,
                     int stop_bit)
{
        memset(mb_param, 0, sizeof(modbus_param_t));
        strcpy(mb_param->device, device);
        mb_param->baud = baud;
        strcpy(mb_param->parity, parity);
        mb_param->debug = FALSE;
        mb_param->data_bit = data_bit;
        mb_param->stop_bit = stop_bit;
        mb_param->type_com = ASCII;
        mb_param->header_length = HEADER_LENGTH_RTU;
        mb_param->checksum_length = CHECKSUM_LENGTH_RTU;
}

    else if ( mb_param->type_com == ASCII )
        {
            for (i = 0; i < query_length; i++)
                printf("[%.2X]", query[i]);
            printf("\n");

            s_lrc = lrc(query, query_length);

            query[query_length++] = s_lrc;

            query_length = build_query_basis_ascii(query_ascii,
query,query_length);

            printf("lrc: [%.2X] query_length :%d\n", s_lrc,query_length);

        }

On Tue, Aug 4, 2009 at 12:20 AM, Karol C. <
<email address hidden>> wrote:

> Your question #74711 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/74711
>
> Karol C. posted a new comment:
> @Ajith James
> Can you show your wrapper for modbus ascii ?
> Thank in advance
>
> --
> You received this question notification because you are a direct
> subscriber of the question.
>

Revision history for this message
Karol C. (viper-lasser) said :
#8

Thanks for help :)

Revision history for this message
Anibal Limón (limon-anibal) said :
#11

Hi ignatius,

The opportunity sounds interesting to me, i want to review more in detail
the job description.

Could you share with me the link?, i can't get it into the answers page.

Cheers,
 alimon

On Mon, Oct 3, 2016 at 11:08 AM, ignatius <
<email address hidden>> wrote:

> Question #74711 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/74711
>
> ignatius posted a new comment:
> Hi!
>
> We are looking for employees working remotely.
>
> My name is Forrest, I am the personnel manager of a large International
> company.
> Most of the work you can do from home, that is, at a distance.
>
> Salary is $2800-$5500.
>
> If you are interested in this offer, please visit
> Our Site
>
> Best regards!
>
> --
> You received this question notification because your team Modbus Team is
> an answer contact for libmodbus.
>