CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 73
  1. #1
    Join Date
    Feb 2013
    Posts
    34

    Sending Byte to serial port

    Hi all

    I need to send a packet of type byte: 0x00, 0x01, 0x02, 0x11, 0x00, 0x00 to a device via the serial port and then I receive and store data coming from device like this: 0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00.

    Someone could send me lines of code through which:

    1) I create the array
    2) the sending the array to serial port
    3) I'll see what I'm sending
    4) I get the data


    I'm using Visual Studio 2010 and C + + or VC++ language.
    I hope someone can help me
    greetings
    f

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sending Byte to serial port

    Just use one of the existing Serial Port classes like:
    http://www.planet-source-code.com/vb...d=144&lngWId=3
    http://www.flounder.com/serial.htm
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Thank you very much mr. VictorN

    I'm trying to test the code: http://www.planet-source-code.com/vb...d=144&lngWId=3
    I have to send the bytes via bluetooth, so I'm looking for the part that deals with the trasmission and reception of bytes in this code. I'd like to add this part in my code . Now i'm able to open serial port and to find the bluetooth device, but maybe i'm not able to send and to receive the bytes correctly. I'll show you my code (i modified a code that i have found in internet):

    PHP Code:
    private: System::Void button3_Click(System::Object^  senderSystem::EventArgs^  e) {

                  
    // add sender name
                     
    Stringname this->serialPort1->PortName;
                     
    // grab text and store in send buffer
                     
    Stringmessage this->textBox2->Text;
                      
    //write to serial
                     
    if(this->serialPort1->IsOpen){
                        array<
    Byte>^TxBfr={0x00,0x02,0x06,0x13,0x01,0x01,0x00};
                        
    this->serialPort1->Write(TxBfr,0,7);

                        try{
                            
    //this->textBox1->Text=this->serialPort1->ReadLine();
                            
    this->textBox1->Text=this->serialPort1->ReadByte();
                            
    //printf("%02X",this->serialPort1->ReadByte());
                            //printf ("%s \n", "A string");
                        
    }
                        catch(
    TimeoutException^){
                            
    this->textBox1->Text="Timeout Exception";
                        } 

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sending Byte to serial port

    The code that you "have found in internet" has nothing to do with Visual C++ one, nor is it a valid native C++.
    It looks like a managed C++ code that is discussed in another (Managed C++/CLI) forum.
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Ah ok thank you.
    I'm not an expert. I prefer programming in native C + + or Visual C + +, but for now I only found that code for communication Bluethooth. Maybe I could use the code in http://www.planet-source-code.com/vb...d=144&lngWId=3

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sending Byte to serial port

    AFAIK it doesn't matter whether you use Bluethooth or infraRed or USB or some other communications. It is up to Bluethooth drivers to emulate correct serial port communications. So you can use the same code to communicate with Bluethooth port as it would be a "classic" serial port. (otherwise, it had to supply some SDK or APIs for communications with it!)
    Victor Nijegorodov

  7. #7
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    When i debug the code in http://www.planet-source-code.com/vb...d=144&lngWId=3, i see this kind of error messages:

    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\devobj.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\serialui.dll'
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\setupapi.dll'
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\devobj.dll'
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\cfgmgr32.dll'
    Failed in call to TransmitCommChar
    Warning: throwing CSerialException for error 1
    Eccezione first-chance a 0x7686c41f in SerialPort.exe: Eccezione di Microsoft C++: CSerialException nella posizione di memoria 0x002efc00..
    Handle Exception, Message:Funzione non corretta.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sending Byte to serial port

    Sorry, I cannot understand the output you have provided. Could you translate it?
    Besides, your could try the more recent version of CSerialPort class from http://www.naughter.com/serialport.html
    Victor Nijegorodov

  9. #9
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    The same errors...:



    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\ntdll.dll', Impossibile trovare o aprire il file PDB (trad:Impossibile trovare o aprire il file PDB=Can not find or open the PDB file)
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\kernel32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\KernelBase.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\mfc100d.dll', Simboli caricati.
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\msvcr100d.dll', Simboli caricati.
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\user32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\gdi32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\lpk.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\usp10.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\msvcrt.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\advapi32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\sechost.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\rpcrt4.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\sspicli.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\cryptbase.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\shlwapi.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7601.17514_none_ec83dffa859149af\comctl32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\msimg32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\oleaut32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\ole32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\imm32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\msctf.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Program Files (x86)\Kaspersky Lab\Kaspersky Anti-Virus 6.0 for Windows Workstations MP4\adialhk.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Program Files (x86)\Kaspersky Lab\Kaspersky Anti-Virus 6.0 for Windows Workstations MP4\kloehk.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\uxtheme.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\dwmapi.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\mfc100ita.dll', Il file binario non è stato compilato con le informazioni di debug.
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\cfgmgr32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\cfgmgr32.dll'
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\serialui.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\setupapi.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\cfgmgr32.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': caricato 'C:\Windows\SysWOW64\devobj.dll', Impossibile trovare o aprire il file PDB
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\serialui.dll'
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\setupapi.dll'
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\devobj.dll'
    'SerialPort.exe': scaricato 'C:\Windows\SysWOW64\cfgmgr32.dll'
    'SerialPort.exe': scaricato 'C:\Program Files (x86)\Kaspersky Lab\Kaspersky Anti-Virus 6.0 for Windows Workstations MP4\adialhk.dll'
    Il thread 'Thread Win32' (0x1138) è terminato con il codice 0 (0x0).
    CSerialPort::Open, Failed to open the comms port, Error:121
    Warning: throwing CSerialException for error 121

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sending Byte to serial port

    Well, I see only one "error" message. It is
    CSerialPort::Open, Failed to open the comms port, Error:121
    Look at the code in the CSerialPort::Open method to understand the reason of this message.
    Victor Nijegorodov

  11. #11
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Ok, the error changes sometimes in :

    CSerialPort::TransmitChar, Failed in call to TransmitCommChar, Error:1

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sending Byte to serial port

    Where do you call this TransmitChar from?
    How do you open a port? Please, show your code.
    Victor Nijegorodov

  13. #13
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Sending Byte to serial port

    The "error" 'Impossibile trovare o aprire il file PDB' is just an information that the loaded dll doesn't have any debugging info.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  14. #14
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Sending Byte to serial port

    Even though you may actually manage to get your C++/CLI program working with the native CSerialPort class, I'd strongly recommend against using it. The .NET framework provides the handy SerialPort class which you should use instead.

    Unnecessarily combining managed and native code can considerably complicate things, which is particularly true when multithreading is involved, and that's the case in properly designed serial communication code. The complication may be so drammatic that you need to take more effort to have the managed and native parts of your program cooperate properly (the so-called interop) than to write the code that your program actually needs to do what it is supposed to do.

    BTW, this is the correct forum section for C++/CLI questions: http://forums.codeguru.com/forumdisp...ed-C-and-C-CLI. Please post further questions of that kind over there.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  15. #15
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    @Eri523: Thank you very much for your reply! I'm trying to use only native C++ now.
    @VictorN: I read that error when i compile only the class in http://www.naughter.com/serialport.html. Now I'm trying to find a sample code in C++ native, able to send and to receive arrays of bytes(using the class that you showed me).

Page 1 of 5 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured