Hi Jim
Thanks...yes I read the link that you provided and it has expanded my understanding on this subject. With my basic understanding of C I kind of understand what’s going on but I am by no means an expert. I correlated the notes to the snippet of code found at Command Line Interface (CLI) using C
Here is the code:
Code:
#include <stdlib.h>
#include <windows.h>
HANDLE GetSerialPort(char *);
int main(void)
{
HANDLE h1, h2;
char h1_buffer[] = ("Hello from Com1:");
char h2_buffer[24];
DWORD byteswritten = 0, bytesread = 0;
char c1[] = {"COM3"};
char c2[] = {"COM4"};
h1 = GetSerialPort(c1);
h2 = GetSerialPort(c2);
WriteFile(h1, h1_buffer, 17, &byteswritten, NULL);
ReadFile(h2, h2_buffer, strlen(h1_buffer) + 1, &bytesread, NULL);
if (bytesread)
printf("%s\n", h2_buffer);
else
printf("Nothing read\n");
CloseHandle(h1);
CloseHandle(h2);
getch();
}
HANDLE GetSerialPort(char *p)
{
HANDLE hSerial;
hSerial = CreateFile(p,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
SetCommState(hSerial, &dcbSerialParams);
return hSerial;
}
OK. So from the above code I understand the following. The string "Hallo from COM1:" is transmitted to the serial TX of COM3 and COM 4 is listening on the RX line to check for the string being received. I also understand that when communicating serially you have to use "handles" i.e. the way I understand it is that when a handle is given to a port, all other ports cannot communicate otherwise there will be a clash of data.
To get the code to compile on my PC I did some minor changes as shown below
Code:
#include <windows.h>
#include<conio.h>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
HANDLE GetSerialPort(char *);
void delay();
int main(void)
{
do
{
HANDLE h1, h2;
char h1_buffer[] = ("Hello World:");
char h2_buffer[24];
DWORD byteswritten = 0, bytesread = 0;
char c1[] = {"COM14"};
char c2[] = {"COM14"};
h1 = GetSerialPort(c1);
h2 = GetSerialPort(c2);
WriteFile(h1, h1_buffer, 17, &byteswritten, NULL);
ReadFile(h2, h2_buffer, strlen(h1_buffer) + 1, &bytesread, NULL);
if (bytesread)
{
printf("%s\n", h2_buffer);
}
else
{ printf("Nothing read\n");
}
CloseHandle(h1);
CloseHandle(h2);
delay();
//getch();
}while(1);
}
HANDLE GetSerialPort(char *p)
{
HANDLE hSerial;
hSerial = CreateFile(p,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
SetCommState(hSerial, &dcbSerialParams);
return hSerial;
}
void delay ()
{
int i = 1000000000;
printf("In delay\n");
while(i>0)
{
i--;
}
}
An explanation of why I did this is as follows. First, I got my USB to TTL converter (FT232RL) and hooked it up to my PC. I opened Hyperterminal and noted that it came up with COM14. I did this to see what COM port value the TTL converter was assigned.
I then decided to take the TX of the USB to TTL converter (FT232RL) and loop it to the RX pin with a wire. I modified the code to make "COM14" the transmitting port and "COM14" the receiving port. So the logic behind this thought experiment is that the string "Hallo World:" could be sent to the TX pin of the USB to TTL converter (FT232RL) from COM14, get looped back to the RX pin and read back into "COM14".
I also added a do while loop to keep on "listening" for the "Hallo World:" string...but it does not seem to read it. When I compile and run it always prints the message "Nothing read In delay"
Can you advise how I can loop back the “Hallo World:” string? Have I missed out something obvious?