The recent use of node's serial library found very slow, I wrote a driver in C + + using node to call it and test pass, the speed is also very fast, the original version recorded:
Test.js
var test = require ('./build/release/test '); test. Arduinodevice (function(data) { console.log (data);});
Gyp configuration:
{ "targets":[ { "target_name": "Test", "sources": ["test.cc"] } ]}
test.cc
#include <node.h>#include<v8.h>#include<stdio.h>#include<unistd.h>//used for UART#include <fcntl.h>//used for UART#include <termios.h>//used for UART#include <string.h>using namespaceV8;//two parameters passed in, Args[0] string, Args[1] callback functionvoidHelloConstfunctioncallbackinfo<value>&args) { //use Handlescope to manage life cyclesisolate* Isolate =isolate::getcurrent (); Handlescope scope (isolate); /////////////////////////////////////////////////////////////////// intUart0 =-1; printf ("----------starting-------------------\ n"); Uart0= Open ("/dev/ttyama0", O_RDWR | O_noctty | O_ndelay);//Open in non blocking read/write mode if(Uart0 = =-1) {printf ("error-unable to open UART. Ensure it is not in use by another application\n"); } structTermios options; Tcgetattr (Uart0,&options); Options.c_cflag= B9600 | CS8 | clocal |Cread; Options.c_iflag=Ignpar; Options.c_oflag=0; Options.c_lflag=0; Tcflush (Uart0, Tciflush); Tcsetattr (Uart0, Tcsanow,&options); //-----TX BYTES-----UnsignedChartx_buffer[ -]; unsignedChar*P_tx_buffer; unsignedChar*P_tx_buffer2; unsignedCharStr[] ="Closel\0"; //P_tx_buffer = &tx_buffer[0]; //*p_tx_buffer++ = ' O '; //*p_tx_buffer++ = ' P '; //*p_tx_buffer++ = ' E '; //*p_tx_buffer++ = ' n '; //*p_tx_buffer++ = ' l ';printf"--1--\n"); P_tx_buffer2= &tx_buffer[0]; printf ("--then str is%s and strlen is%d--\n",(Const Char*) Str,strlen ((Const Char*) (str)); //printf ("%s", strlen (str)); for(intI=0; I<strlen (reinterpret_cast<Const Char*> (str)); i++) {printf ("--for Inner 1 is%d--\n", i); *p_tx_buffer2++ =Str[i]; printf ("--for Inner 2 is%d--\n", i); //printf ("%s", Str[i]);} printf ("--2--\n"); if(Uart0! =-1) { intCount = Write (uart0, &tx_buffer[0], (P_tx_buffer2-&tx_buffer[0]));//Filestream, bytes to write, number of bytes to write if(Count <0) {printf ("UART TX error\n"); } close (uart0); } /////////////////////////////////////////////////////////////////// //determine parameter format and format if(args. Length () <2|| !args[0]->isstring ()) {Isolate-throwexception (Exception::typeerror (String::newfromutf8 (Isolate,"wrong arguments"))); return; } //callback, use the cast method to convertLocal<function> callback = Local<function>::cast (args[1]); Local<Value> argv[1] = { //Stitching StringString::Concat (Local<string>::cast (args[0]), String::newfromutf8 (Isolate,"Fuck World")) }; //invoke callback, Parameters: current context, number of arguments, parameter listCallback->call (Isolate->getcurrentcontext ()->global (),1, argv);}//equivalent to adding {Hello:hello} to the exports objectvoidInit (handle<object>exports) {Node_set_method (Exports,"Arduinodevice", hello);}//exposing the Export object//prototype ' Node_module (module_name, Initialize) 'Node_module (test, init);
Char *foo (); std::string str;str.append (reinterpret_cast<const Char*> (foo ()));
Reinterpret_cast Usage:
reinterpret_cast<type-id> (expression)
Type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer.
The reinterpret_cast operator is used to handle conversions between unrelated types, and it produces a new value that has exactly the same bit as the original parameter (Expressoin). For example: int *n= new int; Double *d=reinterpret_cast<double*> (n); After the calculation, D contains a useless value. This is because reinterpret_cast only replicates n bits to D and does not perform the necessary analysis. Therefore, it is prudent to use reinterpret_cast.
The IBM C + + guide clearly tells us where reinterpret_cast can, or where it should be used as a conversion operator:
- From a pointer type to an integer type that is large enough
- From an integer type or enum type to a pointer type
- Pointers from one pointer to a function to another for a different kind of pointer to a function
- Pointer to an object from one pointer to another, to a different type
- From a pointer to a class function member to another pointer to a function member of a different type
- From a pointer to a class data member to another pointer to a different type of data member
In fact, the use of reinterpret_cast is not limited to the above mentioned items, any type of pointers can be converted to each other, will not get compile errors. Some of the items listed above may be a limitation of reinterpret_cast usage under Linux, or it may be the way IBM recommends that we use reinterpret_cast.
C++:invalid conversion from ' unsigned char * ' to ' const char * '