Reading USB problems

So I have been looking all over and this is what I have found I am trying to take the measurement from a scale and output what is given and yes sadly the never ending loop has to be there right now the program is showing that there is information but it is not outputting anything.(this WILL not be the full program)

Current output:
//this is where the weight from the scale should be
In delay
//this is where the weight from the scale should be
In delay
//this is where the weight from the scale should be
In delay
//this is where the weight from the scale should be
In delay
//this is where the weight from the scale should be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <windows.h>
#include<conio.h>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>

HANDLE openUSB(char *);
void wait();
int main(void)
{

do
{
 HANDLE scale;
 char scalearray[24];
 DWORD byteswritten = 0, bytesread = 0;
 char usbplace[] = {"COM5"};
 scale = openUSB(usbplace);
 ReadFile(scale, scalearray, 10, &bytesread, NULL);
if(bytesread)
{
   printf("%s\n", scalearray);
}
 else
 {
        printf("Please Make Sure That The Scale is Connected");
 }
 CloseHandle(scale);
 wait();
}while(1);

}

HANDLE openUSB(char *p)
{
 HANDLE hSerial;
 hSerial = CreateFile(p,
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL,
                      0);

 DCB dcbSerialParams = {0};
 dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
 dcbSerialParams.BaudRate=CBR_19200;
 dcbSerialParams.ByteSize=8;
 dcbSerialParams.StopBits=ONESTOPBIT;
 dcbSerialParams.Parity=NOPARITY;
 SetCommState(hSerial, &dcbSerialParams);
 return hSerial;
}

void wait ()
{
int i = 10000;
printf("In delay\n");
while(i>0)
{
   i--;
}

}
Last edited on
a simple test to see if your code is working.
initialize your handle
remove the do while loop.
just do a single write and read after initialization.
then close the port.

i would normally put a delay after write just before read.
Last edited on
Ok so it dose the same thing when I take the do while loop away it see's that there is something there but it doesn't show anything
Ok I found My problem I had the wrong Baud Rate thanks any way :)
Topic archived. No new replies allowed.