|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
serial port problems
To people who handled with serial port programming, please help. I am trying to let two PCS
to generate communication between each other. I am new to serial port programming and have written two simple programs to run on each PC. The program is a DOS application in windows and is written in C++. One of PC will be the "reading" PC and the other PC will be the "writing" PC. The "reading" PC will just read and display how many bytes that is sent from the serial port of the "writing" PC. The problem now is, when i run the two programs on each PC, the "reading" PC is reading from its own serial port and the "writing" PC is writing to its own serial port. They are not handshaking or communicating. I have connected the two serial ports via a NULL modem cable but it is not working. I wonder is there is anything wrong with my code or the setting of the handshaking. Please guide! This is the code for the "reading" PC (the output of the program just printf 0 bytes, how to let it detect the 9 bytes from the "writing" PC?): Code:
#include "windows.h"
#include <stdio.h>
#include <io.h>
#include <conio.h>
#include <stdlib.h>
#define maxBytes 32
#define MAX 128
int main()
{
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
DWORD dwBytesRead = 0;
char szBuff[maxBytes] = {0};
int readbytes, i;
//opening the serial port
hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
printf("Serial port does not exist\n");
}
printf("Other errors\n");
}
//setting parameters
dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
//GetCommState is to retrieves the current control settings for a specific
communications device.
if (!GetCommState(hSerial, &dcbSerialParams))
{
printf("Not GetCommState, not able to retrieves the current control.\n");
}
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
//SetCommState configures a communications device according to the specifications
//in DCB. The function reinitializes all hardware control settings but it does not
//empty output or input queues
if (!SetCommState(hSerial, &dcbSerialParams))
{
printf("Not SetCommState, cannot configures serial port according to DCB
specifications set\n");
}
//setting timeouts
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 50;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 50;
//SetCommTimeouts set the time out parameters for all read and write operation
if (!SetCommTimeouts(hSerial, &timeouts))
{
printf("Not SetCommTimeouts, cannot set the timeout parameters to serial
port\n");
}
if(!ReadFile(hSerial, szBuff, maxBytes, &dwBytesRead, NULL)){
printf("error\n");
}
else{
while (ReadFile(hSerial, szBuff, maxBytes, &dwBytesRead, NULL))
{
readbytes = sscanf("%d", szBuff);
for (i = 0; i < readbytes; i++)
{
printf("%c",szBuff[i]);
}
printf("\n");
printf("Read %d bytes from serial port.\n",readbytes);
}
//closing down
CloseHandle(hSerial);
}
}
This is the code for "writing" PC: Code:
#include "windows.h"
#include <stdio.h>
#include <io.h>
#include <conio.h>
#define maxBytes 32
int main()
{
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
DWORD dwBytesWrite = 0;
DWORD dwBytesRead = 0;
char szBuff[maxBytes]= {0};
int i;
//opening the serial port
hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
printf("Serial port does not exist");
}
printf("Other errors");
}
//setting parameters
dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
//GetCommState is to retrieves the current control settings for a specific
communications device.
if (!GetCommState(hSerial, &dcbSerialParams))
{
printf("Not GetCommState, not able to retrieves the current control.");
}
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
//SetCommState configures a communications device according to the specifications
//in DCB. The function reinitializes all hardware control settings but it does not
//empty output or input queues
if (!SetCommState(hSerial, &dcbSerialParams))
{
printf("Not SetCommState, cannot configures serial port according to DCB
specifications set");
}
//setting timeouts
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 50;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 50;
//SetCommTimeouts set the time out parameters for all reand and write operation
if (!SetCommTimeouts(hSerial, &timeouts))
{
printf("Not SetCommTimeouts, cannot set the timeout parameters to serial
port");
}
//Writting data
//WriteFile write data from the specified file or i/o devices.
if (!WriteFile(hSerial, szBuff, maxBytes, &dwBytesWrite, NULL))
{
printf("Serial port cannot write file");
}
else
{
while(1)
{
i = sprintf(szBuff, "testing .\n");
printf("Writing %d bytes to serial port.\n", i);
}
}
//closing down
CloseHandle(hSerial);
}
Please help anyone! |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| talking to the serial port | aloishis89 | PHP Forum | 3 | 05-28-2009 09:55 PM |
| serial port problem | mld | Pascal/Delphi | 1 | 08-27-2008 11:44 AM |
| Bluetooth Serial Port | TcM | Technology Ramble | 2 | 02-27-2008 12:22 PM |
| Serial Port Close Problem in Pocket PC | HosseinSadeghi | C# Programming | 0 | 07-14-2007 02:40 AM |
All times are GMT -5. The time now is 09:45 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc