0

I am trying to establish a serial connection with a device via RS232 and the C code. The purpose is to connect arduino to computer later and show the ip adress retrieved from the device on the LED screen.

Usually I connect the device to the computer via RS232, open PuTTY and establish the serial connection at 115200 baud rate. Then I press enter, type login, press enter, type password, press enter, type 'ip show' and then retrieve the ip adress.

The problem is I am not good at C programming (studied it only for 1 year in University). The code I come up with (copy-pasted and edited) is attached below. The questions are:

1) How do I get the information printed on the terminal screen. For example, after I type login and then press enter, there is a sentence saying type your password. How do I retrieve that to IDE's console? 2) On the final step (retrieving the ip), how do I retrieve the ip? It is in text format, after it's shown I need to copy it and paste it into another document).

As for now, my limited amount of knowledge about C prohibits me to go further.

Any kind of help (even the name of the helpful function) is appreciated!

//
// serial.c / serial.cpp
// A simple serial port writing example
// Written by Ted Burke - last updated 13-2-2013
//
// To compile with MinGW:
//
//      gcc -o serial.exe serial.c
//
// To compile with cl, the Microsoft compiler:
//
//      cl serial.cpp
//
// To run:
//
//      serial.exe
//

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    // Define the five bytes to send ("hello")
    char bytes_to_send[15];
    bytes_to_send[0] = '\n';
    bytes_to_send[1] = 'a';
bytes_to_send[2] = 'd';
bytes_to_send[3] = 'm';
bytes_to_send[4] = 'i';
bytes_to_send[5] = 'n';
bytes_to_send[6] = '\n';
bytes_to_send[7] = 's';
bytes_to_send[8] = 'h';
bytes_to_send[9] = 'o';
bytes_to_send[10] = 'w';
bytes_to_send[11] = ' ';
bytes_to_send[12] = 'i';
bytes_to_send[13] = 'p';
bytes_to_send[14] = '\n';
// Declare variables and structures
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};

// Open the highest available serial port number
fprintf(stderr, "Opening serial port...");
hSerial = CreateFile(
            "\\\\.\\COM6", GENERIC_READ|GENERIC_WRITE, 0, NULL,
            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hSerial == INVALID_HANDLE_VALUE)
{
        fprintf(stderr, "Error\n");
        return 1;
}
else fprintf(stderr, "OK\n");

// Set device parameters (38400 baud, 1 start bit,
// 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
    fprintf(stderr, "Error getting device state\n");
    CloseHandle(hSerial);
    return 1;
}

dcbSerialParams.BaudRate = CBR_115200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
    fprintf(stderr, "Error setting device parameters\n");
    CloseHandle(hSerial);
    return 1;
}

// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0)
{
    fprintf(stderr, "Error setting timeouts\n");
    CloseHandle(hSerial);
    return 1;
}

// Send specified text (remaining command line arguments)
DWORD bytes_written, total_bytes_written = 0;
fprintf(stderr, "Sending bytes...");
if(!WriteFile(hSerial, bytes_to_send, sizeof(bytes_to_send), &bytes_written, NULL))
{
    fprintf(stderr, "Error\n");
    CloseHandle(hSerial);
    return 1;
}

fprintf(stderr, "%d bytes written\n", bytes_written);

// Close serial port
fprintf(stderr, "Closing serial port...");
if (CloseHandle(hSerial) == 0)
{
    fprintf(stderr, "Error\n");
    return 1;
}
fprintf(stderr, "OK\n");
fprintf(stderr, "the sent sentence is: ");
for(int i=0;i<sizeof(bytes_to_send);i++){
    fprintf(stderr,"%c",bytes_to_send[i]);
}

// exit normally
return 0;
}
druumnbass
  • 1
  • 1
  • 2
  • You cannot receive data in a terminal program and your own program at once. Only one program can grab the port. You used WriteFile to send. Use Readfile to receive. Check out the MSDN examples. – Lundin Aug 17 '17 at 06:33
  • .NET has System.IO.Ports.SerialPort which has everything you need. – jthort Aug 18 '17 at 14:18

1 Answers1

0

For setting up the port and read/write commands you can use the code from this answer. Then you only have to worry about what you send and what you receive.

Putty actually reads the serial port in the background and then prints it on the console so you can see it. You will have to do the same thing in your app. After you read the data, you have to print it.

For example, for login, as you described it:

write (fd, "login\r\n", 7);          // send 7 character login command
                                     // (similar to writing login + enter in Putty)

usleep ((7 + 25) * 100);             // allow time for sending & receiving
                                     // (sleep enough to transmit the 7 plus
                                     // receive 25:  approx 100 uS per char transmit)

char buf [100];
int n = read (fd, buf, sizeof(buf)); // read rx characters (Putty does this automatically)
                                     // expect to have "the sentence saying type your password" in this buffer
                                     // 100 characters are read, adjust it if you expect a longer answer

printf("Rx:");                       // display the rx data (Putty does this automatically)
for (int i = 0; i < sizeof(buf); i++) 
    printf("%c",buf[i]);            
printf("\n");

// continue with sending the password + reading the response
// then sending the ip command + reading the answer
addmoss
  • 622
  • 10
  • 14