AsyncSerial
Introduction
_ ____ _ _ / \ ___ _ _ _ __ ___ / ___| ___ _ __(_) __ _| | / _ \ / __| | | | '_ \ / __| \___ \ / _ \ '__| |/ _` | | / ___ \\__ \ |_| | | | | (__ ___) | __/ | | | (_| | | /_/ \_\___/\__, |_| |_|\___| |____/ \___|_| |_|\__,_|_| |___/
DiscussionAn external object for Max/MSP on Mac OSX that interfaces with serial devices. This is intended to provide more useful functionality than the built-in [serial] object. Namely, [AsyncSerial] listens for incoming data on a separate thread and spits it out whenever it arrives. It does not need to be polled. This is similar to how UDPReceive works. Similar care must be taken when dealing with multithreaded Max-patches. [AsyncSerial] can also have its input buffer flushed, which is necessary when sending commands to a device and awaiting a reply. [AsyncSerial] can also be put in and out of 'canonical' input mode, in which data is only spit out once a newline char '\n' is received, which is useful for parsing text-based replies. [AsyncSerial] also allows users to set VMIN and VTIME, as described in the unix manual, sub verbo 'termios': man termios [AsyncSerial] cannot be polled using 'bang', because, depending on VMIN, VTIME and ICANON, that could cause the main thread to hang. [AsyncSerial] can either output received data as a sequence of bytes, as [serial] does, or as a list of text symbols, which is useful with canonic input processing. for help using [AsyncSerial] from within Max/MSP, consult async-serial.maxhelp, which should have come with this file. Groupsstandard Max SDK functionsGroup members:
private from Max/MSPGroup members:
public to Max/MSPGroup members:
Functions
asyncSerial_newvoid* asyncSerial_new ( t_symbol *s, long argc, t_atom *argv); ParametersReturn Valuethe new object Discussioncreate a new object asyncSerial_freevoid asyncSerial_free ( t_asyncSerial *x); ParametersDiscussiondestroy object asyncSerial_assistvoid asyncSerial_assist ( t_asyncSerial *x, void *b, long m, long a, char *s); ParametersDiscussionshow help from with Max/MSP asyncSerial_opensuccess_t asyncSerial_open ( t_asyncSerial *x, t_symbol *name); ParametersReturn ValueSUCCESS or FAILURE. upon FAILURE, an error message is posted to the 'Max Window' DiscussionIf there is already a serial port open, close it and wait one second. Then attempt to open the specified port. asyncSerial_closevoid asyncSerial_close ( t_asyncSerial *x); ParametersDiscussionIf there is a serial port open, close it and restore its settings so it can be used by another program. asyncSerial_printvoid asyncSerial_print( t_asyncSerial *x, t_symbol *s, long argc, t_atom *argv); ParametersDiscussionFormat data as ASCII chars and send it to the serial port. asyncSerial_intvoid asyncSerial_int( t_asyncSerial *x, long n); ParametersDiscussionSend binary data to the serial device. [AsyncSerial] sends one byte at a time, so this method is intended to receive messages in the range 0 ~ 255 Larger values will be sent in newtork byte order (most significant bit first), in as few bytes are required t encode the entire number. For example 1234 will be sent as [4, 210], because ((4 << 8) & 210) = 1234. Negative values are sent as 2s compliment equivalents. asyncSerial_setBaudsuccess_t asyncSerial_setBaud ( t_asyncSerial *x, int baud); ParametersReturn ValueSUCCESS or FAILURE. upon FAILURE, an error message is posted to the 'Max Window' DiscussionSet the baud rate for communication with the currently open serial device, and remember this value so it can be used subsequent calls to "asyncSerial_open". The defaut baud rate for a new object is 9600. asyncSerial_flushsuccess_t asyncSerial_flush ( t_asyncSerial *x); ParametersReturn ValueSUCCESS or FAILURE. upon FAILURE, an error message is posted to the 'Max Window' DiscussionFlush the serial receive buffer. This deletes data that has been received by the operating system but not yet spit out by the [AsyncSerial] object. asyncSerial_listensuccess_t asyncSerial_listen ( t_asyncSerial *x, bool shouldListen); ParametersReturn ValueSUCCESS or FAILURE. upon FAILURE, an error message is posted to the 'Max Window' DiscussionStart or stop listening asynchronously for incoming data. New objects start listening upon creation. asyncSerial_setInputModesuccess_t asyncSerial_setInputMode( t_asyncSerial *x, int vtime, int vmin, bool icanon); ParametersReturn ValueSUCCESS or FAILURE. upon FAILURE, an error message is posted to the 'Max Window' Discussionset the input processing mode. see the UNIX manual sub verbo 'termios' for a more detailed description, and more information about how the paramaters interact. asyncSerial_setOutputModevoid asyncSerial_setOutputMode( t_asyncSerial *x, bool isBinary); ParametersDiscussionset the output function to either asyncSerial_binOut or asyncSerial_asciiOut. asyncSerial_printPortsvoid asyncSerial_printPorts( t_asyncSerial *x); ParametersDiscussionprint a list of serial ports to the Max window. asyncSerial_writeint asyncSerial_write ( t_asyncSerial *x, void *data, int numChars); ParametersReturn Valuethe number of bytes sucessfully sent to the device Discussionsend data to the serial device asyncSerial_readint asyncSerial_read ( t_asyncSerial *x, void *data, int numChars); ParametersReturn Valuethe number of bytes sucessfully read from the device Discussionread data from the serial device asyncSerial_listenThreadvoid* asyncSerial_listenThread ( void *x); Discussionthe main run loop for listening for incoming serial data. asyncSerial_signalHandlervoid asyncSerial_signalHandler( int sig); Discussioncatch signals sent to the serial listen thread, so that the program does not exit when the tread is killed. asyncSerial_binOutvoid asyncSerial_binOut ( t_asyncSerial *x, void *buffer, int numValidBytes); Discussionsend received data out of the left outlet as binary data, ie as sequence of chars sent out one at a time. This function is not called directly, but via a pointer that selects the output function. asyncSerial_asciiOutvoid asyncSerial_asciiOut( t_asyncSerial *x, void *buffer, int numValidBytes); ParametersDiscussionsend received data out of the left outlet as a list of symbols. Input is broken into pieces based on the location of spaces, packed into a list, and sent out. Even numbers are put in the list as symbols, so in Max, [fromsymbol] has to be used to convert them to numbers, if desired. This is particularly useful with canonic input processing. This function is not called directly, but via a pointer that selects the output function. |