|
| 1 | +// 631_COMMAND_LINE_TEST.cpp |
| 2 | +// Ultra-simple 631 command-line communications example |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include "typedefs.h" |
| 9 | +#include "serial.h" |
| 10 | +#include "cf_packet.h" |
| 11 | +#include "show_packet.h" |
| 12 | + |
| 13 | +//============================================================================ |
| 14 | +int main(int argc, char* argv[]) |
| 15 | + { |
| 16 | + printf("Ultra-simple CFA-631 command-line communications example.\n"); |
| 17 | + printf("Crystalfontz America, Inc. http://www.crystalfontz.com\n\n"); |
| 18 | + printf("Usage:\n"); |
| 19 | + printf("%s PORT BAUD\n",argv[0]); |
| 20 | + printf("PORT is something like \"/dev/ttyS0\" or \"/dev/usb/ttyUSB0\"\n"); |
| 21 | + printf("BAUD is 19200 or 115200\n"); |
| 22 | + printf("To clear the display, enter \"clear\" as an optional third parameter\n\n"); |
| 23 | + |
| 24 | + |
| 25 | + //If only 0 or 1 parameter is entered, prompt for the missing parameter(s) |
| 26 | + if(argc < 3) |
| 27 | + { |
| 28 | + printf("\nMISSING A PARAMETER. Enter both PORT and BAUD.\n\n"); |
| 29 | + return(0); |
| 30 | + } |
| 31 | + |
| 32 | + //Check for optional "clear" parameter and set flag if found |
| 33 | + int |
| 34 | + cleardisplay=0; |
| 35 | + if((argc > 3) && (!strcmp(argv[3],"clear"))) cleardisplay=1; |
| 36 | + |
| 37 | + |
| 38 | + int |
| 39 | + baud; |
| 40 | + //default the baud to 19200 |
| 41 | + if(strcmp(argv[2],"115200")) |
| 42 | + baud=19200; |
| 43 | + else |
| 44 | + baud=115200; |
| 45 | + |
| 46 | + if(Serial_Init(argv[1],baud)) |
| 47 | + { |
| 48 | + printf("Could not open port \"%s\" at \"%d\" baud.\n",argv[1],baud); |
| 49 | + return(1); |
| 50 | + } |
| 51 | + else |
| 52 | + printf("\"%s\" opened at \"%d\" baud.\n\n",argv[1],baud); |
| 53 | + |
| 54 | +//Outgoing command packets. Either clear the screen |
| 55 | +//or send our line information |
| 56 | +//********************************************** |
| 57 | + if(cleardisplay) |
| 58 | + { |
| 59 | + outgoing_response.command = 6; |
| 60 | + outgoing_response.data_length = 0; |
| 61 | + send_packet(); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + //Send line 1 to the 631 using command 31 |
| 66 | + outgoing_response.command = 31; |
| 67 | + outgoing_response.data[0]=0; //col |
| 68 | + outgoing_response.data[1]=0; //row |
| 69 | + memcpy(&outgoing_response.data[2],"* >This is line 1< *",20); |
| 70 | + outgoing_response.data_length = 22; |
| 71 | + send_packet(); |
| 72 | + |
| 73 | + //CFA-631 / CFA-633 communications protocol only allows |
| 74 | + //one outstanding packet at a time. Wait for the response |
| 75 | + //packet from the CFA-631 / CFA-633 before sending another |
| 76 | + //packet. |
| 77 | + int |
| 78 | + k; |
| 79 | + int |
| 80 | + |
| 81 | + timed_out; |
| 82 | + timed_out = 1; //default timed_out is true |
| 83 | + for(k=0;k<=10000;k++) |
| 84 | + if(check_for_packet()) |
| 85 | + { |
| 86 | + ShowReceivedPacket(); |
| 87 | + timed_out = 0; //set timed_out to false |
| 88 | + break; |
| 89 | + } |
| 90 | + if(timed_out) |
| 91 | + printf("Timed out waiting for a response.\n"); |
| 92 | + |
| 93 | +//Send line 2 to the 631 using command 31 |
| 94 | + outgoing_response.command = 31; |
| 95 | + outgoing_response.data_length = 22; |
| 96 | + outgoing_response.data[0]=0; //col |
| 97 | + outgoing_response.data[1]=1; //row |
| 98 | + memcpy(&outgoing_response.data[2],"* >This is line 2< *",20); |
| 99 | + send_packet(); |
| 100 | + |
| 101 | + //CFA-631 / CFA-633 communications protocol only allows |
| 102 | + //one outstanding packet at a time. Wait for the response |
| 103 | + //packet from the CFA-631 / CFA-633 before sending another |
| 104 | + //packet. |
| 105 | + timed_out = 1; //reset timed_out to default false value |
| 106 | + for(k=0;k<=10000;k++) |
| 107 | + if(check_for_packet()) |
| 108 | + { |
| 109 | + ShowReceivedPacket(); |
| 110 | + timed_out = 0; //set timed_out to false |
| 111 | + break; |
| 112 | + } |
| 113 | + if(timed_out) |
| 114 | + printf("Timed out waiting for a response.\n"); |
| 115 | + } |
| 116 | +//********************************************** |
| 117 | + |
| 118 | + printf("Ctrl-C to exit.\n"); |
| 119 | + printf("Updated display, now waiting for packets\n\n"); |
| 120 | + |
| 121 | + while(!cleardisplay) |
| 122 | + { |
| 123 | + usleep(100000); // 1/10 second |
| 124 | + if(check_for_packet()) |
| 125 | + ShowReceivedPacket(); |
| 126 | + } |
| 127 | + |
| 128 | + if(cleardisplay) printf("Display Cleared.\n"); |
| 129 | + printf("Done.\n\n"); |
| 130 | + Uninit_Serial(); |
| 131 | + return 0; |
| 132 | + |
| 133 | + } |
| 134 | +//============================================================================ |
0 commit comments