int term_phoenix_send(uint8_t * buf, int len) { uint8_t *echobuf = NULL; int res; assert((echobuf = alloca(len))); res = rs232_send(fd, buf, len); if (res < 0) return res; else if (res != len) return -EIO; // kill the echo! //printf("echo\n"); res = rs232_read(fd, echobuf, len); //printf("echo done\n"); if (res < 0) return res; else if (res != len) return -EIO; // paranoia mode! if (memcmp(buf, echobuf, len)) return -EIO; return 0; }
u_int8_t bs_inverter_read(char* buffer) { int bytes_read=0; set_interface_attr(B9600, 0); sleep(RS232_TIMEOUT_BS_INVERTER); bytes_read=rs232_read(buffer, BUFFERSIZE_BS_INVERTER, RS232_TIMEOUT_BS_INVERTER, 0); return bytes_read; }
int rs232_flush(int fd) { uint8_t foobuf[8]; // flush buffer while (rs232_read(fd, foobuf, 8) > 0) { } return 0; }
int term_phoenix_pps(uint8_t *obuf, uint8_t *ibuf) { // uint8_t buf[16]; int res = term_phoenix_send(obuf, 4); // assert(res == 4); // TODO: switch baudrate // res = rs232_setbaud(fd, 115200); assert(!res); res = rs232_read(fd, obuf, 4); printf("%d\n",res); return 0; }
int iso_read(HANDLE_ISO iso, unsigned char *format, unsigned char *target, unsigned char *source, unsigned char *data, unsigned int *plen) { int err = 1, index; unsigned char tmp, tmp2; err = rs232_read(iso->rs232, &tmp, 1); if ( err != 1 || (tmp & 0xf0) != 0x80 ) { return -1; } tmp2 = (tmp & 7) - 2; err = rs232_read(iso->rs232, target, 1); if (err != 1) { return -1; } err = rs232_read(iso->rs232, source, 1); if (err != 1) { return -1; } err = rs232_read(iso->rs232, data, tmp2); if (err != tmp2) { return -1; } tmp = tmp + *target + *source; for(index = 0; index < tmp2; index++) { tmp += data[index]; } err = rs232_read(iso->rs232, &tmp2, 1); if (err != 1 || tmp2 != tmp) { return -1; } return 0; }
u_int8_t mppt_read(char* buffer) { struct mppt_elements mppt_stat; memset(&mppt_stat,0, sizeof(mppt_stat)); set_interface_attr(B19200, 0); sleep(MPPT_RS232_TIMEOUT); rs232_read(buffer, MPPT_BUFFERSIZE, MPPT_RS232_TIMEOUT, 0); mppt_buffer_extract(buffer, &mppt_stat); mppt_print_stat(&mppt_stat); return 0; }
void mppt_init_raw() { char* buffer=NULL; buffer=calloc(MPPT_BUFFERSIZE, 1); int idx=0; set_interface_attr(B19200, 0); sleep(MPPT_RS232_TIMEOUT); rs232_read(buffer, MPPT_BUFFERSIZE, MPPT_RS232_TIMEOUT, 0); rs232_close(); for (idx=0; idx<MPPT_BUFFERSIZE;idx++) { printf("0x%02x ", buffer[idx]); } printf("\n"); if (buffer!=NULL) buffer=NULL; }
int term_phoenix_reset(uint8_t * atr, int maxlen) { int res; rs232_flush(fd); // res = rs232_set_dtr(fd, 1); res = rs232_set_rts(fd, 1); if (res < 0) return res; // 20ms should do the trick usleep(20000); res = rs232_set_rts(fd, 0); if (res < 0) return res; return rs232_read(fd, atr, maxlen); }
int main(int argc, char *argv[]) { int it = 0; int done = 0; nmeaINFO info; nmeaPARSER parser; int select_result = -1; // value returned frome select() struct timeval select_timeout; int nfds = 0; fd_set rset; char rs232_device; int bytes_read; char rs232_buffer[1024]; char *token; char nmea_message[256]; if(argc < 2) { printf("Usage: %s <file to parse>\n", argv[0]); return -1; } rs232_device = open(argv[1], O_RDWR | O_NOCTTY); if(rs232_device < 0) perror("com_open"); // Select UART2_TX and set it as output printf("Setting tx. . .\n"); sprintf(rs232_buffer, "echo 11 > /sys/kernel/debug/omap_mux/spi0_d0"); if(system(rs232_buffer) < 0) perror("setting tx"); // Select UART1_RX and set it as input pulled up printf("Setting rx. . .\n"); sprintf(rs232_buffer, "echo 39 > /sys/kernel/debug/omap_mux/spi0_sclk"); if(system(rs232_buffer) < 0) perror("setting rx"); const char *buff[] = { "$GPRMC,173843,A,3349.896,N,11808.521,W,000.0,360.0,230108,013.4,E*69\r\n", "$GPGGA,111609.14,5001.27,N,3613.06,E,3,08,0.0,10.2,M,0.0,M,0.0,0000*70\r\n", "$GPGSV,2,1,08,01,05,005,80,02,05,050,80,03,05,095,80,04,05,140,80*7f\r\n", "$GPGSV,2,2,08,05,05,185,80,06,05,230,80,07,05,275,80,08,05,320,80*71\r\n", "$GPGSA,A,3,01,02,03,04,05,06,07,08,00,00,00,00,0.0,0.0,0.0*3a\r\n", "$GPRMC,111609.14,A,5001.27,N,3613.06,E,11.2,0.0,261206,0.0,E*50\r\n", "$GPVTG,217.5,T,208.8,M,000.00,N,000.01,K*4C\r\n" }; select_timeout.tv_sec = 1; select_timeout.tv_usec = 0; nmea_zero_INFO(&info); nmea_parser_init(&parser); while(!done) { fflush(stdout); FD_ZERO(&rset); if(rs232_device > 0) { FD_SET(rs232_device, &rset); nfds = max(nfds, rs232_device); } select_result = select(nfds + 1, &rset, NULL, NULL, NULL); if(select_result == -1 && errno == EAGAIN) { perror("select"); continue; } if(select_result == -1) { perror("main:"); return 1; } if(rs232_device > 0) { if(FD_ISSET(rs232_device, &rset)) { //bytes_read = read(rs232_device, rs232_buffer, sizeof(rs232_buffer)); bytes_read = rs232_read(rs232_device); if(bytes_read > 0) { if(rs232_check_last_char('\n')) { bytes_read = rs232_unload_rx(rs232_buffer); if(bytes_read > 0) { rs232_buffer[bytes_read] = 0; token = strtok(rs232_buffer, "\n"); while(token != NULL) { sprintf(nmea_message, "%s\n", token); nmea_parse(&parser, nmea_message, (int)strlen(nmea_message), &info); if(it > 0) { printf("\033[15A"); } else it++; printf("Time: %i/%i/%i %i:%i:%i.%i\n", info.utc.day, info.utc.mon + 1, info.utc.year + 1900, info.utc.hour, info.utc.min, info.utc.sec, info.utc.hsec); printf("Signal: %i\n", info.sig); printf("Operating Mode: %i\n", info.fix); printf("Position Diluition of Precision: %f\n", info.PDOP); printf("Horizontal Diluition of Precision: %f\n", info.HDOP); printf("Vertical Diluition of Precisione: %f\n", info.VDOP); printf("Latitude: %f\n", info.lat); printf("Longitude: %f\n", info.lon); printf("Elevation: %f m\n", info.elv); printf("Speed: %f km/h\n", info.speed); printf("Direction: %f degrees\n", info.direction); printf("Magnetic variation degrees: %f\n", info.declination); printf("\nSatellite: \tin view: %i\n\t\tin use: %i\n", info.satinfo.inview, info.satinfo.inuse); token = strtok(NULL, "\n"); } } } } } } //int smask; /**< Mask specifying types of packages from which data have been obtained */ //nmeaTIME utc; /**< UTC of position */ //int sig; /**< GPS quality indicator (0 = Invalid; 1 = Fix; 2 = Differential, 3 = Sensitive) */ //int fix; /**< Operating mode, used for navigation (1 = Fix not available; 2 = 2D; 3 = 3D) */ //double PDOP; /**< Position Dilution Of Precision */ //double HDOP; /**< Horizontal Dilution Of Precision */ //double VDOP; /**< Vertical Dilution Of Precision */ //double lat; /**< Latitude in NDEG - +/-[degree][min].[sec/60] */ //double lon; /**< Longitude in NDEG - +/-[degree][min].[sec/60] */ //double elv; /**< Antenna altitude above/below mean sea level (geoid) in meters */ //double speed; /**< Speed over the ground in kilometers/hour */ //double direction; /**< Track angle in degrees True */ //double declination; /**< Magnetic variation degrees (Easterly var. subtracts from true course) */ //nmeaSATINFO satinfo; /**< Satellites information */ /*it++; if(it > 6) done = 1; select_timeout.tv_sec = 1; select_timeout.tv_usec = 0;*/ } nmea_parser_destroy(&parser); return 0; }
/* * error, data, read_len = port:read(max_read_len [[, timeout_ms], forced]) * * if forced > 0 then read() blocks until 'timeout_ms' or there's 'max_read_len' * bytes available */ static int lua_port_read(lua_State *L) { int ret = 0; int argc = 0; int forced = 0; unsigned int timeout = 0; unsigned int len = 0; unsigned int bytes_read = 0; unsigned char *data = NULL; struct rs232_port_t *p = NULL; p = *(struct rs232_port_t**) luaL_checkudata(L, 1, MODULE_NAMESPACE); lua_remove(L, 1); if (p == NULL || !rs232_port_open(p)) { lua_pushinteger(L, RS232_ERR_PORT_CLOSED); lua_pushnil(L); lua_pushinteger(L, 0); return 3; } argc = lua_gettop(L); switch (argc) { case 1: len = (unsigned int) luaL_checkinteger(L, 1); data = (unsigned char*) malloc(len * sizeof(unsigned char *)); memset(data, 0, len); ret = rs232_read(p, data, len, &bytes_read); break; case 2: case 3: len = (unsigned int) luaL_checknumber(L, 1); data = (unsigned char*) malloc(len * sizeof(unsigned char *)); memset(data, 0, len); timeout = (unsigned int) luaL_checknumber(L, 2); forced = luaL_optint(L, 3, 0); if (forced > 0) ret = rs232_read_timeout_forced(p, data, len, &bytes_read, timeout); else ret = rs232_read_timeout(p, data, len, &bytes_read, timeout); break; default: lua_pushinteger(L, RS232_ERR_UNKNOWN); lua_pushnil(L); lua_pushinteger(L, 0); return 3; } DBG("ret=%d hex='%s' bytes_read=%d\n", ret, rs232_hex_dump(data, bytes_read), bytes_read); lua_pushinteger(L, ret); if (bytes_read > 0) lua_pushlstring(L, (char *) data, bytes_read); else lua_pushnil(L); if (data) free(data); lua_pushinteger(L, bytes_read); return 3; }
int term_phoenix_apdu(apdu_t * apdu) { uint8_t hdr[10]; uint8_t ack; int res; // TODO: honor debug hdr[0] = apdu->cla; hdr[1] = apdu->ins; hdr[2] = apdu->p1; hdr[3] = apdu->p2; hdr[4] = apdu->p3; // send header if ((res = term_phoenix_send(hdr, 5))) return res; // ACK? res = rs232_read(fd, &ack, 1); //printf("ack %x %x\n",ack,apdu->ins); if (res < 0) return res; else if (res != 1) return -EIO; else if (ack != apdu->ins) { res = rs232_read(fd, apdu->sw+1, 1); if(res != 1) return res; apdu->sw[0] = ack; return 0; } apdu->ack = ack; // data? if (apdu->p3) { // data out if (apdu->dout) { if ((res = term_phoenix_send(apdu->dout, apdu->p3))) return res; } // data in else if (apdu->din) { res = rs232_read(fd, apdu->din, apdu->p3); if (res < 0) return res; else if (res != apdu->p3) return -EIO; } } //printf("sw\n"); // receive SW1, SW2 res = rs232_read(fd, apdu->sw, 2); if (res < 0) return res; else if (res != 2) return -EIO; return 0; }