erjiang / usbscale

Reads weight data from USB scales.
GNU General Public License v3.0
91 stars 22 forks source link

usbscale.c type mismatch gives corrupted weight on ubuntu 11.10 #2

Closed rben closed 12 years ago

rben commented 12 years ago

Using usbscale on ubuntu 11.10 was problematic and after analyzing debug data I discovered a type mismatch in usbscale.c

I would suggest you change the debug printf to reduce lines. Perhaps the following:

FROM:

ifdef DEBUG

        int i;
        for(i = 0; i < WEIGH_REPORT_SIZE; i++) {
            printf("%x\n", data[i]);
        }

endif

TO:

ifdef DEBUG

        int i;
        for(i = 0; i < WEIGH_REPORT_SIZE; i++) {
    printf("%x, ", data[i]);
        }
    printf("\n");

endif

ALSO: fprintf(stderr, "It has descriptors:\n\tmanufc: %d\n\tprodct: %d\n\tserial: %d\n\tclass: %d\n\tsubclass: %d\n", TO: fprintf(stderr, "It has descriptors: manufc: %d, prodct: %d, serial: %d, class: %d, subclass: %d; ",

Finally - In my debugging to track down the type mismatch I added the following to the print_scale_data call after the following statement: double weight = (double)(dat[4] + (dat[5] << 8)) / 10;

ifdef DEBUG

        int i;
        for(i = 0; i < WEIGH_REPORT_SIZE; i++) {
        printf("%x, ", dat[i]);
        }
    printf("\n");

endif

With the suggested debug messages my output looked as follows:

Found scale 1446:6a73 (bus 3, device 9):It has descriptors: manufc: 1, prodct: 2, serial: 3, class: 0, subclass: 0; Manufacturer: ELANE 3, 4, b, ff, 8d, 0, 3, 4, b, ffffffff, ffffff8d, 0, -11.5 oz

The negative output was due to the mismatch when applying the follwing in the code: double weight = (double)(dat[4] + (dat[5] << 8)) / 10;

Solution: Suggest you change print_scale_data in usbscale.c:

FROM: static int print_scaledata(char); and static int print_scaledata(char dat) {

TO: static int print_scaledata(unsigned char); and static int print_scaledata(unsigned char dat) { respectively.

This should unify the type between the function and the "data" type which it is called with.

It would be really great if debug could just be switched on the command line.

erjiang commented 12 years ago

Thanks for your contribution. I will work on getting them into the source code by the end of next week. In the future, a diff or better yet, a pull request, would streamline things. You can find documentation about pull requests on Github at Help.Github.

erjiang commented 12 years ago

https://github.com/erjiang/usbscale/commit/0d4982a1d68b18398dd25db1ac4105fe2add7f16