Serial Communication using wxCTB

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
ebroz
In need of some credit
In need of some credit
Posts: 1
Joined: Fri Sep 01, 2006 2:20 am

Serial Communication using wxCTB

Post by ebroz » Fri Sep 01, 2006 2:38 am

I'm working on an application to send data over the COM ports to a circuit card that will decode and act on the data. I have decided to use wxCTB for this. I'll be running this application on a WinXP machine.
The circuit card has be design, manufactured, and tested. I can send data, but the circuit card does not recognize the data. Data is being sent as an ASCII character with a line feed at the end.
I ran a simple test with HyperTerm and a VC++ project and the card recognized the data.
How do I set baud rate, parity, etc?
Does wxCTB send a \r or \n as a line feed?
Thanks

tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post by tiwag » Fri Sep 01, 2006 5:14 am

Here you have a small ctb-test for wxctb-0.8
where you can see all settings and basic commands
how to send & receive something over serial port ...

Code: Select all

#include "wx/ctb/serport.h"
#include "wx/ctb/timer.h"
#include <stdio.h>

wxBaud baudrate = wxBAUD_38400;
char *devname = wxCOM1;
int timeout = 250;
int main(int argc,char* argv[])
{
    // device is a serial port
    wxSerialPort* dev;
    dev = new wxSerialPort();
    // try to open the given port
    if (dev->Open(devname) < 0)
    {
        printf("Cannot open %s\n",devname);
        delete dev;
        return -1;
    }
    // set the baudrate
    dev->SetBaudRate(baudrate);

    // set the RTS DTR lines
    dev->SetLineState( wxSERIAL_LINESTATE_RTS );
    dev->SetLineState( wxSERIAL_LINESTATE_DTR );

    // ok, device is ready for communication
    char buf[20];
    dev->GetSettingsAsString(buf,20);
    printf("Settings : %s\n",buf);

    // we are using a timer for timeout test. After the given timeout
    // is reached, the to value will be set to 1
    int to = 0;
    timer t(timeout,&to,NULL);
    t.start();
    char sendstr[] = "#Id\n";
    printf("sending  : %s\n",sendstr);
    int  sendstrsize = sizeof( sendstr ) / sizeof( char ) - 1 ;
    dev->Writev(sendstr,sendstrsize,&to);
    t.stop();

    char* receivedBytes = NULL;
    int rd = dev->ReadUntilEOS(&receivedBytes,"\n",timeout);
    if (rd > 0)
    {
        receivedBytes[rd] = 0;
        printf("answer (%i): %s\n",rd,receivedBytes);
    }
    else
    {
        printf("timeout, got no answer\n");
    }

    dev->Close();
    delete dev;
    return 0;
}
Does wxCTB send a \r or \n as a line feed?
You have to define yourself what chars you are sending :D
-tiwag

Post Reply