Serial port help

I am having a problem reading from a serial port every-time that I read from the port I get a different value and even (most of the time) more than one output, Thus I am stuck these are snip its from a much larger code that runs prefect except for this portion.

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
  HANDLE hSerial=0;
  hSerial = CreateFile("COM1",
                    GENERIC_READ | GENERIC_WRITE,
                    0,
                    0,
                    OPEN_EXISTING,
                    0,
                    0);//creating the comunication with the scale
char scaleInput1[20];//used for reading the scale


COMMTIMEOUTS CommTimeouts= {0};
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
dcbSerialParams.BaudRate=CBR_4800;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=TWOSTOPBITS;
dcbSerialParams.Parity=NOPARITY;

dcbSerialParams.fBinary = TRUE;
dcbSerialParams.fDtrControl = DTR_CONTROL_DISABLE;
dcbSerialParams.fRtsControl = RTS_CONTROL_DISABLE;
dcbSerialParams.fOutxCtsFlow = TRUE;
dcbSerialParams.fOutxDsrFlow = TRUE;
dcbSerialParams.fDsrSensitivity= FALSE;
dcbSerialParams.fAbortOnError = TRUE;


CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
CommTimeouts.ReadTotalTimeoutConstant = 500;
SetCommTimeouts (hSerial, &CommTimeouts);

SetCommState(hSerial, &dcbSerialParams);//parameters of the scale



then later in the code where it is read
1
2
3
4
5
6
7
8
9
while(!TestDestroy())      {//this loops to make sure the scale is always reading except if the button is pressed
                BytesRead=0;
                flag6="6";
                ReadFile(hSerial, scaleInput1, 20, &bytesread,NULL );
                wxString mystring3(scaleInput1, wxConvUTF8);
                MyStringQueue4.Post(mystring3);
                if(strlen(scaleInput1) != 0 && strlen(scaleInput1) != 1) {
                    partTwo.InputFromScale=mystring3;
                    break;      }




p.s. this is also in a thread and the first part is outside of the loop
Last edited on
Well, what would you do if ReadFile() failed? And, how would you even know?
how do you check if readfile() failed?
Is this a function you wrote? Or is this a function you are using? Anyway, a lot of functions return an error code.
I'd suggest you read the documentation for that function. MSDN is a good place to start.
wait ReadFile() I am using that, I did not write it
and I did I thought I had everything set right (well now I know that I have to make sure it did not fail but) other then that everything is set right and it was in a thread before and when I added the timeouts things started to get weird
Last edited on
Here is a link to the ReadLine function:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx

I hope this helps.
ok i got the code done for checking to see if it failed or not but it is still having the same problem as before

p.s. it is not failing
oh and the output goes like this
1st output
3
second output
.098 oz

and the other one is
1st output
3.098
2nd output
oz
another one that I just saw was
1st output

2nd output
3.098 oz
and another
1st output
3.0
2nd output
98 oz
Last edited on
what is your expected output anyway?
how many bytes of data are you expecting?
i would recommend to read the exact amount of bytes.

if possible try to add a checksum to the packet and a start of transmission and end of transmission character. so you know exactly what to expect. it will be easier to parse the data also.
The expected output from the scale I am working with should be 3.098 oz (I could do without the oz I just technically need the number) and I have tried to read the exact number and it gives me the same thing I am just having the problem of figuring out why it is random everytime
do you know the exact number of bytes expected replace 20 with that number.
Topic archived. No new replies allowed.