Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. string

string

Scheduled Pinned Locked Moved C / C++ / MFC
question
22 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    macmac38
    wrote on last edited by
    #1

    Hello, This arrives at my serial port:
    924.09,-4.06,676.26,0.156488,-0.653856,-0.714853,-0.192273
    CString nx; while(port.Read(cReceivedChar, 1 , overlapped, &dwBytesRead)) { nx += cReceivedChar[0]; SetDlgItemText(IDC_SHIFT_X,("%c", nx)); } but my dialog only shows 06,676 ?? Why? Thanks, Mark

    J D 2 Replies Last reply
    0
    • M macmac38

      Hello, This arrives at my serial port:
      924.09,-4.06,676.26,0.156488,-0.653856,-0.714853,-0.192273
      CString nx; while(port.Read(cReceivedChar, 1 , overlapped, &dwBytesRead)) { nx += cReceivedChar[0]; SetDlgItemText(IDC_SHIFT_X,("%c", nx)); } but my dialog only shows 06,676 ?? Why? Thanks, Mark

      J Offline
      J Offline
      jmkhael
      wrote on last edited by
      #2

      What is in nx? Try a MessageBox call and why not a SetDlgItemText(IDC_SHIFT_X,(LPCSTR)nx); Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      M 1 Reply Last reply
      0
      • J jmkhael

        What is in nx? Try a MessageBox call and why not a SetDlgItemText(IDC_SHIFT_X,(LPCSTR)nx); Papa while (TRUE) Papa.WillLove ( Bebe ) ;

        M Offline
        M Offline
        macmac38
        wrote on last edited by
        #3

        Hello, nx contains nothing when i trace it. cReceivedChar[0] contains confused values from the string.. I think the whole procedure must be rewied Thanks, Mark

        1 Reply Last reply
        0
        • M macmac38

          Hello, This arrives at my serial port:
          924.09,-4.06,676.26,0.156488,-0.653856,-0.714853,-0.192273
          CString nx; while(port.Read(cReceivedChar, 1 , overlapped, &dwBytesRead)) { nx += cReceivedChar[0]; SetDlgItemText(IDC_SHIFT_X,("%c", nx)); } but my dialog only shows 06,676 ?? Why? Thanks, Mark

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          CString nx;
          char cReceivedChar;

          while(port.Read(&cReceivedChar, 1, overlapped, &dwBytesRead))
          nx += cReceivedChar;

          SetDlgItemText(IDC_SHIFT_X, nx);


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          M 1 Reply Last reply
          0
          • D David Crow

            CString nx;
            char cReceivedChar;

            while(port.Read(&cReceivedChar, 1, overlapped, &dwBytesRead))
            nx += cReceivedChar;

            SetDlgItemText(IDC_SHIFT_X, nx);


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            M Offline
            M Offline
            macmac38
            wrote on last edited by
            #5

            char cReceivedChar; is not working (other type expected) BOOL CSerialPort::Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesRead) Thanks, Mark

            D 1 Reply Last reply
            0
            • M macmac38

              char cReceivedChar; is not working (other type expected) BOOL CSerialPort::Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesRead) Thanks, Mark

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              How are you calling the Read() method?


              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

              M 1 Reply Last reply
              0
              • D David Crow

                How are you calling the Read() method?


                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                M Offline
                M Offline
                macmac38
                wrote on last edited by
                #7

                while(port.Read(cReceivedChar, 1 , overlapped, &dwBytesRead)) Thanks, Mark

                D 1 Reply Last reply
                0
                • M macmac38

                  while(port.Read(cReceivedChar, 1 , overlapped, &dwBytesRead)) Thanks, Mark

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  The first parameter is supposed to be a pointer.


                  "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                  M 1 Reply Last reply
                  0
                  • D David Crow

                    The first parameter is supposed to be a pointer.


                    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                    M Offline
                    M Offline
                    macmac38
                    wrote on last edited by
                    #9

                    char *cReceivedChar; while(port.Read(cReceivedChar, 1, overlapped, &dwBytesRead)) Thanks, Mark

                    D 1 Reply Last reply
                    0
                    • M macmac38

                      char *cReceivedChar; while(port.Read(cReceivedChar, 1, overlapped, &dwBytesRead)) Thanks, Mark

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      But then you'd have to allocate one byte of memory from the heap (i.e., cReceivedChar = new char). Why not just use this instead:

                      char cReceivedChar;
                      while(port.Read(&cReceivedChar, 1, overlapped, &dwBytesRead))
                      ...


                      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                      M 1 Reply Last reply
                      0
                      • D David Crow

                        But then you'd have to allocate one byte of memory from the heap (i.e., cReceivedChar = new char). Why not just use this instead:

                        char cReceivedChar;
                        while(port.Read(&cReceivedChar, 1, overlapped, &dwBytesRead))
                        ...


                        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                        M Offline
                        M Offline
                        macmac38
                        wrote on last edited by
                        #11

                        yep, it seems to be better to use a reference. with this procedure my string output is only "0.1922" remember incoming: 924.09,-4.06,676.26,0.156488,-0.653856,-0.714853,-0.192273 It looks like that the first values running through the digits in the dialog..mmh?? Is this so complicate to give out the full string? Thanks, Mark

                        D 1 Reply Last reply
                        0
                        • M macmac38

                          yep, it seems to be better to use a reference. with this procedure my string output is only "0.1922" remember incoming: 924.09,-4.06,676.26,0.156488,-0.653856,-0.714853,-0.192273 It looks like that the first values running through the digits in the dialog..mmh?? Is this so complicate to give out the full string? Thanks, Mark

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          macmac38 wrote: yep, it seems to be better to use a reference. My example was not a reference. It was a pointer, exactly what the Read() method is expecting. Had it wanted a reference, the signature would look like:

                          BOOL Read(void& lpBuf, ...);

                          You mentioned that 924.09,-4.06,676.26,0.156488,-0.653856,-0.714853,-0.192273 is arriving at the serial port. How are you verifying this?


                          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                          M 1 Reply Last reply
                          0
                          • D David Crow

                            macmac38 wrote: yep, it seems to be better to use a reference. My example was not a reference. It was a pointer, exactly what the Read() method is expecting. Had it wanted a reference, the signature would look like:

                            BOOL Read(void& lpBuf, ...);

                            You mentioned that 924.09,-4.06,676.26,0.156488,-0.653856,-0.714853,-0.192273 is arriving at the serial port. How are you verifying this?


                            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                            M Offline
                            M Offline
                            macmac38
                            wrote on last edited by
                            #13

                            This is the position string from a robot arm and every time the same format. x,y,z shift and quaternion 1-4 I have to extract them later from the string in float variables Thanks, Mark

                            D 1 Reply Last reply
                            0
                            • M macmac38

                              This is the position string from a robot arm and every time the same format. x,y,z shift and quaternion 1-4 I have to extract them later from the string in float variables Thanks, Mark

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #14

                              That's all well and good, but in your original post, you indicated that "This arrives at my serial port," implying that the data is indeed there. That's why I asked how you were verifying it. In the while loop that you are using to read the incoming data, how many times does the loop execute? After each call to Read(), what is the value of dwBytesRead? If the incoming data is always in the same format and is always less than X bytes, you might could use this instead:

                              char received[X];
                              port.Read(received, sizeof(received), overlapped, &dwBytesRead);
                              received[dwBytesRead] = '\0';


                              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                              M 1 Reply Last reply
                              0
                              • D David Crow

                                That's all well and good, but in your original post, you indicated that "This arrives at my serial port," implying that the data is indeed there. That's why I asked how you were verifying it. In the while loop that you are using to read the incoming data, how many times does the loop execute? After each call to Read(), what is the value of dwBytesRead? If the incoming data is always in the same format and is always less than X bytes, you might could use this instead:

                                char received[X];
                                port.Read(received, sizeof(received), overlapped, &dwBytesRead);
                                received[dwBytesRead] = '\0';


                                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                M Offline
                                M Offline
                                macmac38
                                wrote on last edited by
                                #15

                                received[dwBytesRead] = '\0'; what does this line mean? I have "access violation" with this line. Thanks, Mark

                                D 1 Reply Last reply
                                0
                                • M macmac38

                                  received[dwBytesRead] = '\0'; what does this line mean? I have "access violation" with this line. Thanks, Mark

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #16

                                  macmac38 wrote: what does this line mean? It appends a \0 character to the end of the string. macmac38 wrote: I have "access violation" with this line. How big is the received array, and what is the value of dwBytesRead?


                                  "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                  M 1 Reply Last reply
                                  0
                                  • D David Crow

                                    macmac38 wrote: what does this line mean? It appends a \0 character to the end of the string. macmac38 wrote: I have "access violation" with this line. How big is the received array, and what is the value of dwBytesRead?


                                    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                    M Offline
                                    M Offline
                                    macmac38
                                    wrote on last edited by
                                    #17

                                    char received[40]; port.Read(received, sizeof(received), overlapped, &dwBytesRead); TRACE( "Bytes read: %d",dwBytesRead); received[dwBytesRead] = '\0'; SetDlgItemText(IDC_SHIFT_X, received); Bytes read: 1Bytes read: -858993460Bytes read: -858993460Bytes read: -858993460 Thanks, Mark

                                    D 1 Reply Last reply
                                    0
                                    • M macmac38

                                      char received[40]; port.Read(received, sizeof(received), overlapped, &dwBytesRead); TRACE( "Bytes read: %d",dwBytesRead); received[dwBytesRead] = '\0'; SetDlgItemText(IDC_SHIFT_X, received); Bytes read: 1Bytes read: -858993460Bytes read: -858993460Bytes read: -858993460 Thanks, Mark

                                      D Offline
                                      D Offline
                                      David Crow
                                      wrote on last edited by
                                      #18

                                      macmac38 wrote: TRACE( "Bytes read: %d",dwBytesRead); Since dwBytesRead is a DWORD, you'll need to use the %lu format. Nonetheless, if received[] is only 40 bytes, trying to access the -858993460th position, or any value outside of 0-39, is obviously wrong. Check the return value of Read() beforehand. In your original post, you indicated that 58 characters were being sent to the serial port. You'll need room for that plus one more so received[] should be 59 not 40. This is not the root of the problem, however, but will eventually need to be addressed.


                                      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                      M 1 Reply Last reply
                                      0
                                      • D David Crow

                                        macmac38 wrote: TRACE( "Bytes read: %d",dwBytesRead); Since dwBytesRead is a DWORD, you'll need to use the %lu format. Nonetheless, if received[] is only 40 bytes, trying to access the -858993460th position, or any value outside of 0-39, is obviously wrong. Check the return value of Read() beforehand. In your original post, you indicated that 58 characters were being sent to the serial port. You'll need room for that plus one more so received[] should be 59 not 40. This is not the root of the problem, however, but will eventually need to be addressed.


                                        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                        M Offline
                                        M Offline
                                        macmac38
                                        wrote on last edited by
                                        #19

                                        char received[59]; port.Read(received, sizeof(received), overlapped, &dwBytesRead); TRACE( "Bytes read: %lu",dwBytesRead); received[dwBytesRead] = '\0'; SetDlgItemText(IDC_SHIFT_X, received); Bytes read: 1 I donn't know where's the problem? Normaly it seems easy..? Like to solve it today ;-) Thanks, Mark

                                        D 1 Reply Last reply
                                        0
                                        • M macmac38

                                          char received[59]; port.Read(received, sizeof(received), overlapped, &dwBytesRead); TRACE( "Bytes read: %lu",dwBytesRead); received[dwBytesRead] = '\0'; SetDlgItemText(IDC_SHIFT_X, received); Bytes read: 1 I donn't know where's the problem? Normaly it seems easy..? Like to solve it today ;-) Thanks, Mark

                                          D Offline
                                          D Offline
                                          David Crow
                                          wrote on last edited by
                                          #20

                                          macmac38 wrote: I donn't know where's the problem? Are you sure a problem exists? Maybe there was only one byte available to read.


                                          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                                          M 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups