Serial Port problem

I am running Xubuntu 12.10 on Virtualbox and using Codeblocks 10.05 with gcc version 4.7. I am trying to write some code to read and write to a device on my serial port. The device is entirely ascii controlled and normally communicates with 9600,8,n,1.
I have tried two different libraries, serialib - http://serialib.free.fr/html/index.html and also RS-232 - http://www.teuniz.net/RS-232/ and they are both very good indeed.
I can open the serial port (/dev/ttyS0) without a problem and can also send a character or string out of the port. I have confirmed this as I am using jpnevulator to snoop on the serial port. I have also used putty and minicom to check the serial port and they both work fine, confirming the serial port functionality on Virtualbox..
However... my program does not receive any response to my requests. I get absolutely nothing and jpnevulator confirms this as it sees my request, but nothing else. Youy might be thinking that this is a permissions issue but I am a member of the dialout group and I have also ran the executable from the commandline with sudo.

At the moment, I am using the above mentioned serialib library. I am going round and round in circles trying to resolve this. It is certainly not a hardware issue as minicom and putty have proven this. This has to be something to do with the code but I am damned if I can see what is wrong. I would appreciate some help on this, please. My current messy code is as follows:-

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <stdio.h>
#include "serialib.h"


#if defined (_WIN32) || defined( _WIN64)
#define         DEVICE_PORT             "COM1"                               // COM1 for windows
#define         BAUD_RATE               9600                               // BAUD for windows
#endif

#ifdef __linux__
#define         DEVICE_PORT             "/dev/ttyS0"                         // ttyS0 for linux
#define         BAUD_RATE               9600                               // BAUD for linux
#endif

using namespace std;

int main()
{
    char buffer[100];
    int charcount = 0;
    unsigned i, n = 0;
    const char busreset = 'r';

    serialib comport;

    printf("(1)Bytes in buffer = %d.\n", comport.Peek());
    comport.FlushReceiver();
    if (comport.Open(DEVICE_PORT, BAUD_RATE))                          // Open com port
    {
        printf("COM port %s successfully opened.\n", DEVICE_PORT);
        if (comport.WriteChar(busreset))                     // reset the 1-wire bus
        {
            printf("Sent %c successfuly.\n", busreset);
            charcount = comport.Read(buffer, 100, 1000);
            if ((charcount > 0))
            {
                printf("Bytes in buffer = %d.\n", comport.Peek());
                printf("Received %i bytes: %s\n", n, buffer);
                buffer[n] = 0;                                       // make 0 the last character
                for (i = 0; i < n; i++)
                {
                    if (buffer[i] < 32)
                        buffer[i] = '.';
                }
            }
            else if (charcount == 0)
                printf("No data received.\n");
            else if (charcount == -1)
                printf("Error while setting the Timeout.\n");
            else if (charcount == -2)
                printf("Error while reading the byte.\n");
            else if (charcount == -3)
                printf("MaxNbBytes is reached.\n");
            else
                printf("Yikes!! Undocumented error.\n");
        }
        else
            printf("Send failed!!\n");
    }
    else
    {
        printf("Failed to open com port %s!!\n", DEVICE_PORT);
        exit(0);
    }

    comport.Close();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.