PC serial baud rate

Post Reply
victorb
Posts: 2
Joined: February 11th, 2014, 3:38 pm

PC serial baud rate

Post by victorb » February 11th, 2014, 3:48 pm

Hi everyone! I just got a Mojo a few days ago and I noticed a weird issue with the serial communication while writing a C++ application using boost. Basically whenever I tried to set the baud rate to 500k it either worked or crashed the board somehow... Figured it was something to do with the nonstandard baud rate and the way boost::asio::serial_port handles it but I didn't dig too far.

So here's a native win32 way of setting the baud rate while still retaining all the bloated deliciousness that boost offers:
Hint: the juicy part is in "int Open"

Code: Select all

#ifndef _SERIAL_H_
#define _SERIAL_H_
#include "boost/asio.hpp"
#include "boost/thread.hpp"
#include "boost/bind.hpp"
#include "boost/function.hpp"
#include <string.h>
#include "windows.h"

class SerialDriver
{
public:
	SerialDriver()
	{
		/// create serial port on io_service
		mWork = new boost::asio::io_service::work(mIoService);
		mThread = new boost::thread(boost::bind(&boost::asio::io_service::run,&mIoService));
		mConnected = false;
	};
	~SerialDriver()
	{
		if(mConnected)
		{
			mSerialPort->close();
		}
		delete mWork;
		delete mThread;
		delete mSerialPort;
	};
	int SetBaud(unsigned int baud)
	{
		mBaud = baud;
		return 0; /// success
	};
	int SetPath(std::string path)
	{
		if(mConnected)
			return -1;
		/// TODO: should check if path exists...
		mPath = path;

		return 0;
	};
	int Open()
	{
		if(mConnected)
			return -1;
		mSerialPort = new boost::asio::serial_port(mIoService);
		mSerialPort->open(mPath);

		/*
		/// this is how you normally do it...
		mSerialPort->set_option(boost::asio::serial_port::stop_bits(
			boost::asio::serial_port::stop_bits::one));
		mSerialPort->set_option(boost::asio::serial_port::character_size(8));
		mSerialPort->set_option(boost::asio::serial_port::parity(
			boost::asio::serial_port::parity::none));
		mSerialPort->set_option(boost::asio::serial_port::flow_control(
			boost::asio::serial_port::flow_control::none));
		mSerialPort->set_option(boost::asio::serial_port::baud_rate(baud));

		*/
		HANDLE hCom = mSerialPort->native_handle();
		/// set serial port params natively
		DCB dcb;
		SecureZeroMemory(&dcb, sizeof(DCB));
		dcb.DCBlength = sizeof(DCB);

		/// get current config so we don't have to fill in everything
		bool result = GetCommState(hCom, &dcb);

		if (!result) 
		{
			//  Handle the error.
			return -1;
		}
		dcb.BaudRate = 500000;
		dcb.ByteSize = 8;
		dcb.Parity = NOPARITY;
		dcb.StopBits = ONESTOPBIT;
		
		result = SetCommState(hCom,&dcb);
		if(!result)
		{
			return -1;
		}


		if(mSerialPort->is_open())
		{
			mConnected = 1;
			return 0;
		}
		else return -1;
	};
	int SendSync(boost::uint8_t * data, boost::uint32_t length)
	{
		return mSerialPort->write_some(boost::asio::buffer(data,length));
	}

	int Close()
	{
		if(!mConnected)
			return -1;
		mSerialPort->close();
		if(mSerialPort->is_open())
			return -1;
		else 
		{
			mConnected = 0;
			return 0;
		}
	}
protected:
	boost::thread *mThread;
	boost::asio::serial_port *mSerialPort;
	boost::asio::io_service mIoService;
	bool mConnected;
	std::string mPath;
	boost::asio::io_service::work *mWork;
	boost::uint32_t mBaud;
private:

};

#endif
I haven't checked on linux yet but if the problem exists there as well then it should be a simple matter of doing the same with ioctl.

embmicro
Site Admin
Posts: 834
Joined: March 24th, 2013, 12:45 pm

Re: PC serial baud rate

Post by embmicro » May 25th, 2014, 12:10 pm

The Mojo (with the latest firmware) doesn't care what baud rate you use as long as it's not 1200 baud. Connecting with 1200 baud will cause it to enter into the Arduino bootloader. Any other baud is simply ignored.

The only other thing that matters is the DTR pin. The Mojo looks for that pin to toggle a few times. If it does, then it enters loading mode. It used to have to just toggle once, but the newer firmware waits for 4-5 toggles to prevent false positives, if I remember correctly.

I hope this helps! I don't have any experience with Boost, but the library RXTX works for Java programs and that's what I usually use to interface with the Mojo.

Post Reply