The 1st boost serial demo

Source: Internet
Author: User

// Testboost. cpp: defines the console applicationProgram.
//

# Include "stdafx. H"
# Include "stdio. H"
# Include <afx. h>
# Include <winsock2.h>
# Include "stringutil. H"
# Include <string>
# Include <iostream>
# Include <boost/shared_ptr.hpp>
# Include <boost/ASIO. HPP>
# Include <boost/thread. HPP>
# Include <boost/function. HPP>
# Include <boost/Bind. HPP>
# Include <boost/date_time/posix_time/posix_time.hpp>
# Include <boost/RegEx. HPP>

Wchar_t g_szsourcefile [2, 256];
Wchar_t g_szdestfile [2, 256];

//////////////////////////////////////// //////////////////////////////////
//
// Serial port Test

Typedef boost: System: error_code boost_error_code;
Typedef boost: ASIO: deadline_timer asio_timer;
Typedef boost: ASIO: serial_port asio_serial;

//////////////////////////////////////// //////////////////////////////////

Static STD: String fileencodepath (const wchar_t * FS)
{
Cw2a afullpath (FS );
Char buffer [1024];
Char temptxt [4];
STD: String retstr;
Int num;

Num = multibytetowidechar (cp_acp, mb_precomposed, afullpath,-1,
(Wchar_t *) buffer, sizeof (buffer)/2 );
Num --; // not count the null

For (INT I = 0; I <num * 2; I ++) // fill data
{
If (I % 2) = 0)
* (Wchar_t *) & (buffer [I]) = htons (* (wchar_t *) & (buffer [I]);
Zeromemory (temptxt, sizeof (temptxt ));
Sprintf (temptxt, "% 02x", (unsigned char) buffer [I]);
Retstr + = temptxt;
}
Return retstr;
}

Static bool check_buffer (boost: ASIO: streambuf & BUF)
{
STD: istream is (& BUF );
Is. unsetf (STD: ios_base: skipws );

STD: String SZ;
SZ. append (STD: istream_iterator <char> (is), STD: istream_iterator <char> ());

STD: cout <SZ;

Return (SZ. Find ("\ r \ NOK \ r \ n ")! =-1 );
}

Static bool send_command (boost: shared_ptr <asio_serial> sp, const char * cmd)
{
Boost: RegEx reg_util ("\ r \ NOK \ r \ n | \ r \ nerror \ r \ n ");
Boost: ASIO: streambuf sbuf;
Boost: ASIO: Write (* sp, boost: ASIO: buffer (CMD, strlen (CMD )));
Boost: ASIO: read_until (* sp, sbuf, reg_util );
Return check_buffer (sbuf );
}

# Define block_size 400

// Download file implementation method:
Static void uploadfile (boost: shared_ptr <asio_serial> SP)
{
// At + efsw = 0 // Create File
// At + efsw = 2 // Write File
// At + efsw = 1 // close file
STD: String strcommand;
Cfile fileinput;
STD: String strname;

// Switch
Strcommand = "at + esuo = 3 \ r ";
Printf ("at + esuo = 3 \ n ");
If (! Send_command (SP, strcommand. c_str ()))
Return;

Strcommand = "at + efsr \ r ";
Printf ("at + efsr \ n ");
If (! Send_command (SP, strcommand. c_str ()))
Goto esuo;

Strcommand = "at + esuo = 4 \ r ";
Printf ("at + esuo = 4 \ n ");
If (! Send_command (SP, strcommand. c_str ()))
Return;

Strcommand = "at + eslp = 0 \ r ";
Printf ("at + eslp = 0 \ n ");
If (! Send_command (SP, strcommand. c_str ()))
Goto esuo;

Strcommand = "at + esuo = 3 \ r ";
Printf ("at + esuo = 3 \ n ");
If (! Send_command (SP, strcommand. c_str ()))
Return;

// create a file
strname = fileencodepath (g_szdestfile);
string: Format (strcommand, "At + efsw = 0, \ "% s \" \ r ", strname. c_str ();
printf ("at + efsw = 0 \ n");
If (! Send_command (SP, strcommand. c_str ()
goto esuo;

// write a file
fileinput. open (g_szsourcefile, cfile: moderead);
byte bufferinput [block_size];
int nreaded = 0;
while (nreaded = fileinput. read (bufferinput, block_size)> 0)
{< br> If (nreaded = block_size)
{< br> string: Format (strcommand, "At + efsw = 2, 0, % d, \" ", block_size);
}< br> else
{< br> string :: format (strcommand, "At + efsw = 2, 1, % d, \" ", nreaded);
}

char TMP [16];
for (INT I = 0; I {< br> sprintf (TMP, "% 02x", bufferinput [I]);
strcommand. append (TMP);
}< br> strcommand. append ("\" \ r ");
If (! Send_command (SP, strcommand. c_str ()
{< br> fileinput. Close ();
goto esuo;
}

Double DPOs = fileinput. getposition ();
Double dtotal = fileinput. getlength ();
Double dpercent = 100 * DPOs/dtotal;
Printf ("percent: % 02d \ n", (INT) dpercent );
}

Fileinput. Close ();
// Close the file
Printf ("at + efsw = 1 \ n ");
String: Format (strcommand, "At + efsw = 1, \" % s \ "\ r", strname. c_str ());
Send_command (SP, strcommand. c_str ());

Esuo:
// Switch back
Printf ("at + esuo = 4 \ n ");
Strcommand = "at + esuo = 4 \ r ";
Send_command (SP, strcommand. c_str ());

}

//////////////////////////////////////// //////////////////////////////////

Void handle_read (boost: shared_ptr <boost: ASIO: streambuf> pbuf,
Boost_error_code EC,
STD: size_t bytes_transferred,
Boost: shared_ptr <asio_timer> T)
{
Printf ("length readed: % d \ n", bytes_transferred );

STD: istream is (pbuf. Get ());
Is. unsetf (STD: ios_base: skipws );
STD: String SZ;
SZ. append (STD: istream_iterator <char> (is), STD: istream_iterator <char> ());
STD: cout <SZ;

T-> cancel ();
}

Void handle_wait (boost_error_code & error,
Boost: shared_ptr <asio_serial> SP)
{
If (error! = Boost: ASIO: Error: operation_aborted)
{
// Printf ("Serial Port timeout, cancel it \ n ");
SP-> cancel ();
}
Else
{
// Printf ("Wait has been canceled \ n ");
}
}

Int _ tmain (INT argc, _ tchar * argv [])
{
If (argc! = 4)
{
STD: cout <"Upload File to mobile" <STD: Endl
<"Usage testboost <COM port> <local File> <mobile File>"
<STD: Endl;
Return 1;
}

Boost: ASIO: io_service iosev;

// Serial port COM1, "/dev/ttys0" in Linux"
Char COM [32];
Int ncomport = _ ttoi (argv [1]);

_ Tcscpy (g_szsourcefile, argv [2]);
_ Tcscpy (g_szdestfile, argv [3]);
Sprintf (COM, "Com % d", ncomport );

Boost: shared_ptr <asio_serial> Sp (New asio_serial (iosev, com ));
// Set parameters
SP-> set_option (asio_serial: baud_rate (115200 ));
SP-> set_option (asio_serial: flow_control: None ));
SP-> set_option (asio_serial: parity: None ));
SP-> set_option (asio_serial: stop_bits: One ));
SP-> set_option (asio_serial: character_size (8 ));
/* Long T1 = gettickcount ();

For (INT I = 0; I <1; I ++)
{

Char * b1 = "at + cpbr = 1 \ r ";
// Write data to the serial port
Boost: ASIO: Write (* sp, boost: ASIO: buffer (B1, strlen (B1 )));

boost: shared_ptr T (
New asio_timer (iosev);
T-> expires_from_now (boost: posix_time :: millisec (3000);

// read data from the serial port
boost: shared_ptr pbuf (new boost: ASIO: streambuf ());
// boost: ASIO: async_read (* sp, boost: ASIO: buffer (BUF),
// boost: BIND (handle_read, buf, _ 1, _ 2);
// SP-> async_read_some (boost: ASIO: buffer (BUF),
// boost :: BIND (handle_read, Buf, _ 1, _ 2, T);
boost: ASIO: async_read_until (* sp, * pbuf,
boost :: regEx ("\ r \ NOK \ r \ n | \ r \ nerror \ r \ n | \ r \ n> \ r \ n"),
boost :: BIND (handle_read, pbuf, _ 1, _ 2, T);

T-> async_wait (boost: BIND (handle_wait, _ 1, SP ));

Size_t M = iosev. Run ();
// Printf ("# % d run result % d \ n", I, m );

Iosev. Reset ();
}
Long T2 = gettickcount ();

Printf ("\ r \ n \ r \ ntotal used % d \ n", t2-t1 );*/

Uploadfile (SP );

SP-> close ();

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.