giiibs
March 4, 2023, 9:28am
1
Hello!
I've met a question: once I want to increase baud rate, the serial output become strange.
here's the arduino code:
#define BAUD 115200
#define DELAY_TIME 100
const char* helloWorld = "Hello, world!";
void setup() {
Serial.begin(BAUD);
}
void loop() {
Serial.println(helloWorld);
delay(DELAY_TIME);
}
I've changed the PC baud rate to 115200 too.
And it works fine with 9600 baud rate, I'm really confusing.
Could you do me a favor?
Thank u.
It is not clear what you are saying. If the baud rate in Serial.begin() is 115200 and the baud rate of the Serial monitor is also 115200 what happens after you upload the sketch ?
giiibs
March 4, 2023, 11:29am
3
actually I'm using a serialPort lib https://github.com/manashmandal/SerialPort
to receive/transmit data to arduino by a C++ program on PC rather than serial monitor. Sorry for unclear discription.
For example, if I try to send "Hello World" to PC, it happens just like the picture.
gcjr
March 4, 2023, 11:32am
4
how do you configure the "bit-rate" on the PC?
i only see manashmandal functions for specifying the port
giiibs
March 4, 2023, 11:35am
5
And the serial monitor gives the same result. I see that 115200 baud rate is valid in the forum but I cant find where the mistake is, it's such a short code.
gcjr
March 4, 2023, 11:36am
6
are you saying you don't see any output on the serial monitor when both the code and serial monitor are set to 115200?
giiibs
March 4, 2023, 11:36am
7
baud rate of COM in PC, actually
giiibs
March 4, 2023, 11:37am
8
I've posted a picture, is it shown?
gcjr
March 4, 2023, 11:40am
9
please answer the questions
how is what you call "baud-rate" set using the manashmandal functions?
do you see any output on the serial monitor when both the serial monitor and Arduino code are set to 115200?
giiibs
March 4, 2023, 11:40am
10
I dont know how to say it, sorry. just, "Hello World" turns to garbbage messages.
Have you tested with various baud rates using the Serial monitor in the IDE, to see at which baud rate the problem starts?
How long is your USB cable (you have not stated it, but we are assuming you are using the USB port on the Mega, and not going through an external serial to usb adapter).
giiibs:
a C++ program on PC
What baud rate does the C++ program set the baud rate to ?
giiibs
March 4, 2023, 11:55am
14
9600 works fine, any larger rate leads to mistake.
I use the blue color wire sold with the board together, about a little longer than 1 meter I think.
giiibs
March 4, 2023, 11:59am
15
It doesnt do this. May I ask how to do this? I suppose the transmitter and the receiver having the same rate is enough. But the cpp program is receiving data by USB (COM) . Is that wrong?
Perhaps it would help if you posted the C++ program that is running on the PC
giiibs
March 4, 2023, 12:03pm
17
just examples in manashmandal's lib.
#include <iostream>
#include "SerialPort.hpp"
#include <stdio.h>
#include <string.h>
using namespace std;
const char* portName = "\\\\.\\COM3";
#define MAX_DATA_LENGTH 255
char incomingData[MAX_DATA_LENGTH];
//Control signals for turning on and turning off the led
//Check arduino code
char ledON[] = "ON\n";
char ledOFF[] = "OFF\n";
//Arduino SerialPort object
SerialPort* arduino;
//Blinking Delay
const unsigned int BLINKING_DELAY = 1000;
//If you want to send data then define "SEND" else comment it out
//#define SEND
void exampleReceiveData(void)
{
int readResult = arduino->readSerialPort(incomingData, MAX_DATA_LENGTH);
printf("%s", incomingData);
Sleep(10);
}
void exampleWriteData(unsigned int delayTime)
{
arduino->writeSerialPort(ledON, MAX_DATA_LENGTH);
Sleep(delayTime);
arduino->writeSerialPort(ledOFF, MAX_DATA_LENGTH);
Sleep(delayTime);
}
void autoConnect(void)
{
//better than recusion
//avoid stack overflows
while (1) {
// ui - searching
std::cout << "Searching in progress";
// wait connection
while (!arduino->isConnected()) {
Sleep(100);
std::cout << ".";
arduino = new SerialPort(portName);
}
//Checking if arduino is connected or not
if (arduino->isConnected()) {
std::cout << std::endl << "Connection established at port " << portName << std::endl;
}
#ifdef SEND
while (arduino->isConnected()) exampleWriteData(BLINKING_DELAY);
#else // SEND
while (arduino->isConnected()) exampleReceiveData();
#endif // SEND
}
}
int main()
{
arduino = new SerialPort(portName);
autoConnect();
}
The SerialPort.cpp file appears to have a baud rate setting on line 42:
dcbSerialParameters.BaudRate = CBR_9600;
The full code:
/*
* Author: Manash Kumar Mandal
* Modified Library introduced in Arduino Playground which does not work
* This works perfectly
* LICENSE: MIT
*/
#include "SerialPort.hpp"
SerialPort::SerialPort(const char *portName)
{
this->connected = false;
this->handler = CreateFileA(static_cast<LPCSTR>(portName),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (this->handler == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
std::cerr << "ERROR: Handle was not attached.Reason : " << portName << " not available\n";
}
else
{
std::cerr << "ERROR!!!\n";
}
}
else
{
DCB dcbSerialParameters = {0};
if (!GetCommState(this->handler, &dcbSerialParameters))
{
std::cerr << "Failed to get current serial parameters\n";
}
else
{
dcbSerialParameters.BaudRate = CBR_9600;
dcbSerialParameters.ByteSize = 8;
dcbSerialParameters.StopBits = ONESTOPBIT;
dcbSerialParameters.Parity = NOPARITY;
dcbSerialParameters.fDtrControl = DTR_CONTROL_ENABLE;
if (!SetCommState(handler, &dcbSerialParameters))
{
std::cout << "ALERT: could not set serial port parameters\n";
}
else
{
this->connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
Sleep(ARDUINO_WAIT_TIME);
}
}
}
}
SerialPort::~SerialPort()
{
if (this->connected)
{
this->connected = false;
CloseHandle(this->handler);
}
}
// Reading bytes from serial port to buffer;
// returns read bytes count, or if error occurs, returns 0
int SerialPort::readSerialPort(const char *buffer, unsigned int buf_size)
{
DWORD bytesRead{};
unsigned int toRead = 0;
ClearCommError(this->handler, &this->errors, &this->status);
if (this->status.cbInQue > 0)
{
if (this->status.cbInQue > buf_size)
{
toRead = buf_size;
}
else
{
toRead = this->status.cbInQue;
}
}
memset((void*) buffer, 0, buf_size);
if (ReadFile(this->handler, (void*) buffer, toRead, &bytesRead, NULL))
{
return bytesRead;
}
return 0;
}
// Sending provided buffer to serial port;
// returns true if succeed, false if not
bool SerialPort::writeSerialPort(const char *buffer, unsigned int buf_size)
{
DWORD bytesSend;
if (!WriteFile(this->handler, (void*) buffer, buf_size, &bytesSend, 0))
{
ClearCommError(this->handler, &this->errors, &this->status);
return false;
}
return true;
}
// Checking if serial port is connected
bool SerialPort::isConnected()
{
if (!ClearCommError(this->handler, &this->errors, &this->status))
{
this->connected = false;
}
return this->connected;
}
void SerialPort::closeSerial()
{
CloseHandle(this->handler);
}
1 Like
giiibs
March 4, 2023, 12:16pm
19
yes...works fine now, havnt expected this thanks for help a newbie
system
Closed
August 31, 2023, 12:17pm
20
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.