c++ cant transfer data through com port when hyperterminal is connected - c++

I am trying to make RS-232 communication between the PC and the PC MCU PIC. So I started making the PC program first in C++, and it was errorless, and according to the cout I made to output the status it should be working, but I wanted to be sure. So I downloaded Hyperterminal and connected Tx to Rx pin in serial com port, but whenever I try to connect the hyperterminal it gives an error, saying access denied (error 5) when I try to run my C++ program. I don't understand where the problem really is. Here's the full code, if the problem was the code's, just to make sure:
main.c:
#include <windows.h>
#include <winbase.h>
#include <iostream>
PDWORD sent;
char buf;
int main(){
DCB serial;
HANDLE hserial = CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
serial.DCBlength = sizeof(DCB);
serial.BaudRate = CBR_9600;
serial.fBinary = true;
serial.fParity = false;
serial.ByteSize = 8;
serial.Parity = NOPARITY;
serial.StopBits = ONESTOPBIT;
char result = BuildCommDCB("baud=9600 parity=N data=8 stop=1", &serial);
if(result != 0){
std::cout << "DCB Structure Successfully Created!" << std::endl;
}else{
std::cout << "DCB Structure Creation Failed!" << std::endl;
}
if(hserial != INVALID_HANDLE_VALUE){
std::cout << "COM Port Handle Successfully Created!" << std::endl;
}else{
std::cout << "COM Port Handle Creation Failed!" << std::endl;
std::cout << GetLastError() << std::endl;
}
char res = WriteFile(hserial, "0xFF", 1, sent, NULL);
if(res != 0){
std::cout << "Writing to COM Port Successfull!" << std::endl;
}else{
std::cout << "Writing to COM Port Failed!" << std::endl;
std::cout << GetLastError() << std::endl;
}
CloseHandle(&hserial);
return 0;
}

Only one program at a time can open a particular COM port. You can do your test if you have two COM ports available.

Related

Serial port read interval not applying in C++

Here's my entire program:
#include <iostream>
#include <windows.h>
int main() {
//? create the serial port file with read and write perms
HANDLE hPort = CreateFileW(L"COM3",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if (hPort == INVALID_HANDLE_VALUE) {
std::cout << "INVALID_HANDLE_VALUE/6\n";
if (GetLastError() == 2) {
std::cout << "serial port doesn't exist. error code: 2/ERROR_FILE_NOT_FOUND\n";
} else {
std::cout << "error occured with serial port file creation (CreateFileW). error code: " << GetLastError() << std::endl;
}
CloseHandle(hPort);
} else {
std::cout << "serial port created successfully (probably)\n";
}
DCB port_conf;
int err = GetCommState(hPort, &port_conf);
if (err == 0) {
std::cout << "GetCommState failed. error code: " << GetLastError() << "\n";
CloseHandle(hPort);
}
port_conf.BaudRate = 9600;
port_conf.Parity = NOPARITY;
port_conf.ByteSize = 8;
port_conf.StopBits = ONESTOPBIT;
port_conf.DCBlength = sizeof(port_conf);
err = SetCommState(hPort, &port_conf);
COMMTIMEOUTS timeouts_conf;
timeouts_conf.ReadIntervalTimeout = 1;
timeouts_conf.ReadTotalTimeoutConstant = 1;
timeouts_conf.ReadTotalTimeoutMultiplier = 1;
timeouts_conf.WriteTotalTimeoutConstant = 1;
timeouts_conf.WriteTotalTimeoutMultiplier = 1;
err = SetCommTimeouts(hPort, &timeouts_conf);
DWORD buffer_size_read;
char buffer_read[512]{};
int buffer_read_size;
char buffer_read_last[512]{};
while (1){
ReadFile(hPort,
buffer_read,
512,
&buffer_size_read,
0);
std::cout << buffer_read;
// if (buffer_read_last != buffer_read) {
// std::cout << buffer_read;
// }
// buffer_read_size = strlen(buffer_read);
// for (int i = 0; i <= buffer_read_size; i++) {
// buffer_read_last[i] = buffer_read[i];
// }
if (GetKeyState(VK_SPACE) != 0) {
break;
}
}
CloseHandle(hPort);
}
The problem with it is that everything is spit out too fast into cout. I made a miserable attempt at limiting this (it is commented out), but the program just doesn't do anything then. Another attempt was using the timeouts_conf.ReadIntervalTimeout, which Microsoft describes this way:
The maximum time allowed to elapse before the arrival of the next byte on the communications line, in milliseconds. If the interval between the arrival of any two bytes exceeds this amount, the ReadFile operation is completed and any buffered data is returned. A value of zero indicates that interval time-outs are not used.
but it didn't change anything. The serial port is continuously receiving data from a microcontroller in which I will not do the limiting for a pretty specific reason.
I need some sort of reliable way of not spitting everything out to cout at the speed of light. Thanks in advance

Named Pipes Issue

I am trying to learn how named pipes work, and created 2 consoles to test the connectivity between server and client. Client will send a message to the server and the server will display the message, but instead of the message, it returns a value of "nullptr" as shown in the error exception break from VS.
below are my codes, do enlighten me if you found any problem with my code, and I am still learning..
Server.cpp
#include "cust_ostream.hpp"
#include <Windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
LPVOID buffer = NULL;
DWORD readbyte;
cout << "---Named Pipe Server Test---" << endl << endl;
cout << "Creating named pipe: \\\\.\\pipe\\mypipe" << endl;
HANDLE hPipe = CreateNamedPipeA("\\\\.\\pipe\\mypipe", PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, 1024, 1024, 0, NULL);
if (!hPipe || hPipe == INVALID_HANDLE_VALUE)
{
cout << "Pipe creation failed." << endl;
return 0;
}
cout << "Connecting pipe to client..." << endl;
BOOL connect = ConnectNamedPipe(hPipe, NULL);
if (!connect)
{
cout << "Connect named pipe failed" << endl;
}
cout << "Success! Reading pipe message from client..." << endl;
ReadFile(hPipe, buffer, sizeof(buffer), &readbyte, NULL);
c_cout << "Pipe message = " << *(int *)buffer << endl;
_getch();
return 0;
}
cust_ostream.hpp
#include <Windows.h>
#include <iostream>
#include <sstream>
using namespace std;
#define endl "\n"
class cust_ostream
{
public:
~cust_ostream()
{
cout << m_buffer.str();
}
template <typename T>
cust_ostream &operator<<(T const &value)
{
m_buffer << value;
return *this;
}
private:
ostringstream m_buffer;
};
#define c_cout cust_ostream()
and my client
#include <Windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
LPVOID data;
DWORD writebyte;
int i = 2;
cout << "---Named Pipe Client---" << endl << endl;
cout << "Creating pipe file: \\\\.\\pipe\\mypipe" << endl;
HANDLE pipe = CreateFileA("\\\\.\\pipe\\mypipe", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (!pipe || pipe == INVALID_HANDLE_VALUE)
{
cout << "Pipe client failed." << endl;
return 0;
}
cout << "Pipe connected to server, sending data..." << endl;
WriteFile(pipe, &i, sizeof(i), &writebyte, NULL);
_getch();
return 0;
}
You need to wait for the NamedPipe to have a ConnectPipeReady event on it. As it stands, you are trying to create the pipe without actually seeing if it was succesfull. See the MSDN documentation for Named Pipes here: https://msdn.microsoft.com/en-ca/library/windows/desktop/aa365592(v=vs.85).aspx
Specifically, this block:
while (1)
{
hPipe = CreateFile(
lpszPipename, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// Break if the pipe handle is valid.
if (hPipe != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY)
{
_tprintf( TEXT("Could not open pipe. GLE=%d\n"), GetLastError() );
return -1;
}
// All pipe instances are busy, so wait for 20 seconds.
if ( ! WaitNamedPipe(lpszPipename, 20000))
{
printf("Could not open pipe: 20 second wait timed out.");
return -1;
}
}
Also you shouldn't use #define endl "\n", use std::endl
You have initialized your buffer as NULL which means that by default its length is zero. Now when you use the sizeof operator in your read function in server (to retrieve the message received by server from client), what happens is that you are asking the sizeof operator in Read function to read 0 bytes! which means that nothing will be read.
To solve this, you can declare a char array of size 100 or a size of a message which you are sure that won't be exceeded by client. Like if you are assuming that message by client is not going to be longer than lets say 60 characters, then you can create your char buffer to be of size 100 just to make sure that you do accommodate all the message by client.
And one more thing, if problem still persists, instead of using sizeof in read, use 100 or whatever the size of of your char buffer array. This should solve your problem.

Ubuntu C++ termios.h example program

I searched a lot and tried many different ways, but I cannot send data to gtkterm via virtual serial bridge (for testing!).
My idea is to communicate with an Atmega uC later on, but first I wanted to test the serial communication by setting up a virtual serial bridge with the help of soccat and controlling the output serial port with gtkterm. The problem is that I'm just receiving useless things in gtkterm... (see screenshots)
soccat command:
socat -d -d PTY: PTY:
The soccat virtual serial port bridge seems to be ok, because I can send data from one serial terminal to another...
gtkterm port preferences:
Port: /dev/pts/6
Baudrate: 9600
Parity: none
Bits: 8
Stopbits: 1
Flow control: none
My little GUI compiles and runs fine, with the input path "/dev/pts/6" and the input baudrate 9600. The program seems to run fine, but in gtkterm are just question marks and quadangles with symbols in every corner coming up. Let's say its not interpretable and independent by the signs as input, BUT the lengths of the output in gtkterm changes by the length of the input (the amount of signs I type in).
Finally here's my code:
main.cpp:
#include <iostream>
#include "serial/serial_communication.cpp"
std::string inputStringUser = "";
int inputIntUser = 0;
std::string pathSerial = "/dev/...";
int baudrate = 19200;
int main()
{
std::cout << "main() [communication_serial_uC] started..." << std::endl;
//GETTING STARTED
std::cout << "Use default port? " << pathSerial << " (Yes = y/ change port = insert the new path" << std::endl;
std::cin >> inputStringUser;
if(inputStringUser != "y" && inputStringUser != "Y")
pathSerial = inputStringUser;
std::cout << "Serial Port is set to: " + pathSerial << std::endl;
std::cout << "Use default baudrate? " << baudrate << "Yes = 0/ change baudrate = insert new baudrate" << std::endl;
std::cin >> inputIntUser;
if(inputIntUser > 0)
baudrate = inputIntUser;
std::cout << "Baudrate is set to: " << baudrate << std::endl;
Serial_communication myPort(pathSerial, baudrate);
//OPEN/ CONFIGURATE PORT
if(myPort.openPort(pathSerial, baudrate) < 0)
{
std::cout << "Error: opening" << std::endl;
return -1;
}
//WRITE PORT
std::cout << "Insert your 'message': (exit = 'exit')" << std::endl;
std::cin >> inputStringUser;
while(inputStringUser != "exit")
{
if(myPort.sendPort(inputStringUser) < 0)
{
std::cout << "Error: sending" << std::endl;
return -1;
}
std::cout << "Insert your 'message': (exit = 'exit')" << std::endl;
std::cin >> inputStringUser;
}
//CLOSE PORT
if(myPort.closePort() < 0)
{
std::cout << "Error: closing" << std::endl;
return -1;
}
std::cout << "main() [communication_serial_uC] beendet..." << std::endl;
return 0;
}
serial/serial_communication.hpp:
#include <iostream>
#include <string>
#include <cstring>
#include <fcntl.h>
#include <termios.h>
class Serial_communication{
public:
Serial_communication(std::string paramPathSerial, int paramBaudrate);
~Serial_communication();
int openPort(std::string pathSerial, int baudrate);
int sendPort(std::string testString);
int closePort();
private:
std::string pathSerial;
int baudrate;
//filedescriptors
int fd;
};
serial/serial_communcation.cpp
#include <iostream>
#include "serial_communication.h"
Serial_communication::Serial_communication(std::string paramPathSerial, int paramBaudrate)
{
fd = 0;
pathSerial = paramPathSerial;
baudrate = paramBaudrate;
}
Serial_communication::~Serial_communication()
{
}
int Serial_communication::openPort(std::string pathSerial, int baudrate)
{
std::cout << "openPort() [serial_communication] started with the following paramters... pathSerial = " << pathSerial << ", baudrate = " << baudrate << std::endl;
//OPENING PORT
//open serial port
fd = open(pathSerial.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
if(fd < 0)
{
std::cout << "Error [serial_communcation]: opening Port: " << pathSerial << std::endl;
return -1;
}
//struct termios
struct termios serial, serial_old;
//get parameters associated with the terminal
if(tcgetattr(fd, &serial) < 0)
{
std::cout << "Error [serial_communication]: getting configuration" << std::endl;
return -1;
}
//safe old parameters
serial_old = serial;
std::cout << "[serial_communication]: Port opened" << std::endl;
//SERIAL CONFIGURATION
/* Set Baud Rate */
cfsetospeed (&serial, (speed_t)baudrate);
cfsetispeed (&serial, (speed_t)baudrate);
// Setting other Port Stuff
serial.c_cflag &= ~PARENB; // Make 8n1
serial.c_cflag &= ~CSTOPB;
serial.c_cflag &= ~CSIZE;
serial.c_cflag |= CS8;
serial.c_cflag &= ~CRTSCTS; // no flow control
serial.c_cc[VMIN] = 1; // read doesn't block
serial.c_cc[VTIME] = 5; // 0.5 seconds read timeout
serial.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
/* Make raw */
cfmakeraw(&serial);
/* Flush Port, then applies attributes */
tcflush( fd, TCIFLUSH );
//set attributes to port
if(tcsetattr(fd, TCSANOW, &serial) < 0)
{
std::cout << "Error [serial_communication]: set attributes" << std::endl;
return -1;
}
//CONFIGURATION FINISHED
std::cout << "openPort() [serial_communication] finished..." << std::endl;
return 1;
}
int Serial_communication::sendPort(std::string textString)
{
std::cout << "write() [Serial_communication] started with the following parameter... textString = " << textString << std::endl;
//attempt to send
if(write(fd, &textString, std::strlen(textString.c_str())) < 0)
{
std::cout << "Error [serial_communcation]: write";
return -1;
}
//SENDING FINISHED
std::cout << "write() [serial_communcation] finished..." << std::endl;
return 1;
}
int Serial_communication::closePort()
{
close(fd);
return 1;
}
So... thats all I got. I tried my best and joined the information from many websites and tried a lots of example codes. My problem is that I dont even know where to search for, so I'm appreciate for any clue...
If there are any questions or information missing, pls let me know it!
Thanks in advance
Thorben
BTW: I'm not THAT experienced with C++ and I'm opened up for comments about my style, but that should not be the main problem...

How to read serial data from a bluetooth port? C++

I created a program to read serial data from a COM port using C++, however due to changes in my project I have to read data from a BT port. Since Im using a bluetooth adapter to connect my computer to the device I expected the reading process to be the same but apparently it is not. Since Im using the Window OS to perform this task GetLastError() returns 2 which means the specified file is not found. However when I use Arduino's serial monitor the data is read just fine. Does anyone know how to read from a BT port in C++? Im using Windows 8 by the way, here is my code:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
///-----------------------------Open port-----------------------------------------------------------------
// Name of port where device is found
LPCWSTR port = L"COM40";
// Open port for reading
HANDLE hComm = ::CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
// Check if port has been opened succesfully
if (hComm == INVALID_HANDLE_VALUE) std::cout << "Failed to open " << port << " error: " << GetLastError() << std::endl;
else std::cout << port << " has been opened succesfully\n";
///-----------------------------Configure port------------------------------------------------------------
// Create DCB structure
DCB dcb = { 0 };
// Get Comm state
if (!::GetCommState(hComm, &dcb)) std::cout << "Failed to get Comm state, error: " << GetLastError() << std::endl;
// Configure strcutre
dcb.DCBlength = sizeof(DCB);
// Set Baud rate
dcb.BaudRate = CBR_9600;
// Set number of bytes in bits that are recieved through the port
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
// Check if port has been configured correctly
if (!::SetCommState(hComm, &dcb)) std::cout << "\nFailed to set Comm State, error: " << GetLastError();
else std::cout << "Comm state has been set succesfully\n";
///-----------------------------Read data-------------------------------------------------------------------
char buffer;
DWORD maxBytes = 1;
if (!::ReadFile(hComm, &buffer, maxBytes, NULL, NULL)) std::cout << "\nFailed to read from " << port << " error: " << GetLastError() << std::endl;
else std::cout << "File has been read succesfully\n";
///-----------------------------Write to text file----------------------------------------------------------
std::fstream file;
int counter = 0;
// Writing to text file will be done later
while (ReadFile(hComm, &buffer, maxBytes, NULL, NULL))
{
std::cout << buffer;
}
///-----------------------------Close port------------------------------------------------------------------
CloseHandle(hComm);
file.close();
std::cout << "\nCOM40 has been closed!\n";
return 0;
}

getting an error associated with WSAIoctl

hey guys i have been trying to make a simple console application to see my network traffic :P im trying to just read it.
anyways im getting an error from the WSAIoctl function and the error code WSAGetLastError is giving me is 10022 and the MSDN tells me that it is associated with this:
Invalid argument.
Some invalid argument was supplied (for example, specifying an invalid level to the setsockopt function). In some instances, it also refers to the current state of the socket—for instance, calling accept on a socket that is not listening.
i have tried messing with the arguments without luck :/ please help me :P
here's my code:
WSADATA wsaData;
int startup = WSAStartup(0x0202, &wsaData);
if(startup != 0) {
cout << "Error: could not initalize WSADATA for target socket." << endl;
system("pause");
}
unsigned long BytesReturned;
int InBuffer, OutBuffer, LPCVoid;
int optValue = 1;
SOCKET sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
Sleep(await);
cout << "creating and configuring RAW_SOCK" << endl;
int listening = listen(sock, SOMAXCONN); // tried debugging using this.
int sockopt = setsockopt(sock, IPPROTO_IP, 2, (char*)&optValue, sizeof(optValue));
int SockMode = WSAIoctl(sock, SIO_RCVALL, &InBuffer, sizeof(InBuffer), &OutBuffer, sizeof(OutBuffer), &BytesReturned, NULL, NULL);
//0x98000001
if(SockMode == 0) {
Sleep(await);
cout << "RAW_SOCKET created successfully!" << endl << "Trying to listen for incoming network packets..." << endl;
int listeningk = listen(sock, SOMAXCONN);
if(listening == 0) {
Sleep(await);
cout << "socket listening without problems, looking for incoming request..." << endl;
}
else {
Sleep(await);
cout << "Error: could not listen on socket." << endl;
exit(EXIT_FAILURE);
}
}
else {
Sleep(await);
cout << "Error: could not create RAW_SOCKET..." << endl << "Dumping SockMode!\r\nint SockMode = " << SockMode << endl;
cout << "setsockopt = " << sockopt << endl;
cout << "WSAGetLastError: " << WSAGetLastError() << endl;
system("pause");
}
Your socket needs to be bound before you can listen. Moreover for this WSAIoctl option you have to obey (from the MSDN docs):
The socket also must be bound to an explicit local IPv4 or IPv6
interface, which means that you cannot bind to INADDR_ANY or
in6addr_any.
I'd suggest some basic self-education is in order before trying to progress this code. There are code samples for common ops like socket setup in MSDN.