dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Serial communication in c++ .net

  1. #1
    Join Date
    Jun 2003
    Location
    Perth - Australia
    Posts
    86

    Serial communication in c++ .net

    Hi,

    I have Visual Studio .NET installed in my computer, however I do not have Visual Studio 6.0, but I have the installation CDs, so I installed the ActiveX components (according to this microsoft page: http://msdn.microsoft.com/library/de...excontrols.asp)
    and now I am working with Mscomm in C++ .NET to access the serial port.

    The functions names generated on the CMscomm1 class (generated automatically) are different from the ones used in Visual C++ 6.0. For example SetOutput is now put_Output, GetInput is get_Input, etc. Is that correct?

    Another thing is that I could make it work with CString data type, that is, I can send/receive this type of data through the serial port, but when it comes to Byte, I always get "Invalid Property value" as an error message.

    Could you please help me?

    Thanks in advance,
    Best regards,
    Beatrice

  2. #2
    Join Date
    Jun 2003
    Location
    Perth - Australia
    Posts
    86
    Hi,
    I figured out a way to do it. Hope it helps someone.
    In my example I am sending 6 bytes.
    If you have any interest in using mscomm in Visual C++ .NET, maybe this thread helps.

    http://www.codeguru.com/forum/showth...light=beatrice

    ------------------------Sending data----------------------
    .........

    //set serial port
    m_comm.put_CommPort(1);

    //open serial port
    m_comm.put_PortOpen(TRUE);

    CString str;
    CString m_output;
    CByteArray tByteArray;
    COleVariant tOleVariant;

    //fill array with bytes
    char bytes[6]={78,79,80,81,82,83};

    //Transfer chars to array of bytes
    for(int c=0;c<6;c++)
    {
    //copy data to tByteArray
    tByteArray.Add(bytes[c]);

    //converts it to string
    str.Format("%c",bytes[c]);
    m_output += str;
    }

    //show data
    AfxMessageBox(m_output);

    //send binary data to serial port
    tOleVariant=tByteArray;
    m_comm.put_Output(tOleVariant);

    ............

    ------------------------Receiving data----------------------

    ........

    CString str;
    CString m_input;
    CByteArray tByteArray;
    COleVariant tOleVariant; //Variant Array

    //receive binary data
    tOleVariant = m_comm.get_Input();

    //put binary data in Byte Array
    tOleVariant.GetByteArrayFromVariantArray(tByteArray);

    //convert byte to string
    for(int c=0;c<6;c++)
    {

    str.Format("%c",tByteArray.GetAt(c));
    m_input = m_input + str;
    }

    //show data received
    AfxMessageBox(m_input);

    //close Serial Port
    m_comm.put_PortOpen(FALSE);


    ...........

  3. #3
    Join Date
    May 2003
    Posts
    20
    Hi.

    I'm using VC++.NET and I cannot find the functions you used in the help files or index. I couldn't find methods such as :
    put_CommPort(1), put_PortOpen(TRUE), etc.

    In what Classes are they contained?

    How did you do yours?

    omoba.

  4. #4
    Join Date
    Jun 2003
    Location
    Perth - Australia
    Posts
    86
    Hi,

    I'm using Microsoft Communications Control, version 6.0. I added it to the dialog. And then I used the option "Add Variable" by clicking with the right button of the mouse. When I added the control variable, the files called mscomm1.h and mscomm1.cpp were created automatically. The methods are in these files in the class there.
    I attached the files so that u can see. If you cannot get these methods, try to replace "put_" by "set" and "get_" by "get" in your methods and see if it works.

    Good luck
    Beatrice
    Attached Files Attached Files

  5. #5
    Join Date
    Jun 2006
    Posts
    2

    Re: Serial communication in c++ .net

    This won't compile in VC++ .NET 2003...

  6. #6
    Join Date
    Dec 2004
    Posts
    21

    Re: Serial communication in c++ .net

    You can try with this:

    #using <AxMSCommLib.dll>
    #using <MSCommLib.dll>

    using namespace AxMSCommLib;
    using namespace MSCommLib;

    ...
    AxMSComm* port;
    HandshakeConstants handshake;
    ...
    port = new AxMSComm();
    ...
    Controls->Add(port); // Adding a control to a form.
    ...
    // Event handler.
    port->OnComm += new EventHandler(this, &MyForm::OnCommEvent);
    // Settings.
    port->CommPort = 1;
    port->Settings = "9600,N,8,1";
    port->Handshaking = handshake;
    port->RThreshold = 1;
    port->InputLen = 1;

    // Open the CommPort.
    port->PortOpen = true;

    // Event handler.
    void OnCommEvent(Object* sender, EventArgs* e)
    {
    switch (port->get_CommEvent())
    {
    case 1:
    Console::WriteLine(S"comEvSend");
    break;
    case 2:
    Console::WriteLine(S"comEvReceive");
    // Taking data...
    String* data = port->Input->ToString();
    break;
    case 3:
    Console::WriteLine(S"comEvCTS");
    break;
    case 4:
    Console::WriteLine(S"comEvDSR");
    break;
    case 5:
    Console::WriteLine(S"comEvCD");
    break;
    case 6:
    Console::WriteLine(S"comEvRing");
    break;
    case 7:
    Console::WriteLine(S"comEvEOF");
    break;
    case 1001:
    MessageBox::Show(S"comEventBreak", S"Exception", MessageBoxButtons::OK, MessageBoxIcon::Error);
    break;
    case 1004:
    MessageBox::Show(S"comEventFrame", S"Exception", MessageBoxButtons::OK, MessageBoxIcon::Error);
    break;
    case 1006:
    MessageBox::Show(S"comEventOverrun", S"Exception", MessageBoxButtons::OK, MessageBoxIcon::Error);
    break;
    case 1008:
    MessageBox::Show(S"comEventRxOver", S"Exception", MessageBoxButtons::OK, MessageBoxIcon::Error);
    break;
    case 1009:
    MessageBox::Show(S"comEventRxParity", S"Exception", MessageBoxButtons::OK, MessageBoxIcon::Error);
    break;
    case 1010:
    MessageBox::Show(S"comEventTxFull", S"Exception", MessageBoxButtons::OK, MessageBoxIcon::Error);
    break;
    case 1011:
    MessageBox::Show(S"comEventDCB", S"Exception", MessageBoxButtons::OK, MessageBoxIcon::Error);
    break;
    default:
    break;
    }

    // Sending data.
    void SendData()
    {
    port->Output = S"Outgoing data...";
    }

  7. #7
    Join Date
    Nov 2006
    Posts
    12

    Re: Serial communication in c++

    CByteArray tByteArray;
    COleVariant tOleVariant; //Variant Array

    //receive binary data
    tOleVariant = m_comm.get_Input();

    //put binary data in Byte Array
    tOleVariant.GetByteArrayFromVariantArray(tByteArray);



    ...........[/QUOTE]

    Hi, I am using Visual C++ 6.0. I tried to your way but the compiler generates error message which the COleVariant doesn't have the GetByteArrayFromVariantArray() as member function..

    May I know if there is another way to implement this function?? I also tried using my own code...Can somebody tell me is this the correct implementation?? See below listing:


    if (m_comm.GetCommEvent()==2 )
    {
    VARIANT in_dat;
    unsigned char* pointer;
    BYTE m_display;

    in_dat = m_comm.GetInput();
    pointer =in_dat.pbVal;
    m_display=*pointer;

    UpdateData(FALSE);
    }

    I want to read 1 byte of binary data which will be store in m_display.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)