int main () { int fd; unsigned char data=3; if ((fd = serialOpen ("/dev/ttyAMA0", 9600))<0) { fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)); return 1; } printf ("\nRaspberry Pi UART Test \n"); while(1) { serialPutchar(fd,data); // printf ("\nPC > RPi = %c", (char)data); // serialPutchar(fd, data); // serialPuts(fd, "\n"); fflush(stdout); } return 0 ; }
void get_size() { serialPutchar(fd,0x56); serialPutchar(fd,0x00); serialPutchar(fd,0x34); serialPutchar(fd,0x01); serialPutchar(fd,0x00); }
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) { int vHi, vLo ; serialPutchar (node->fd, 'a') ; serialPutchar (node->fd, pin - node->pinBase) ; vHi = serialGetchar (node->fd) ; vLo = serialGetchar (node->fd) ; return (vHi << 8) | vLo ; }
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) { /**/ if (mode == OUTPUT) serialPutchar (node->fd, 'o') ; // Input else if (mode == PWM_OUTPUT) serialPutchar (node->fd, 'p') ; // PWM else serialPutchar (node->fd, 'i') ; // Default to input serialPutchar (node->fd, pin - node->pinBase) ; }
void loadCustomChar(int fd, uint8_t location, uint8_t img[8]){ int i; cmd(fd, CUSTOMCHAR); // address of customchar (1-8) serialPutchar(fd, location); // load 8 bytes top to bottom for (i = 0; i < 8; i++){ serialPutchar(fd, img[i]); } }
int main () { int fd ; int count ; unsigned int nextTime ; if ((fd = serialOpen ("/dev/ttyAMA0", 9600)) < 0) { fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; return 1 ; } if (wiringPiSetup () == -1) { fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; return 1 ; } char s1[41] ={ 0xdd, 0xdd, 0xdd, 0xdd, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x72, 0x68, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x72, 0x68, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xee, 0xee, 0xee, 0xee, '\0'}; char s2[41] ={ 0xdd, 0xdd, 0xdd, 0xdd, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x72, 0x68, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x72, 0x68, 0x02, 0xee, 0xee, 0xee, 0xee, '\0'}; nextTime = millis () + 3000 ; int flag = 0; while(1) { if (millis () > nextTime) { char a = 0x72; char b = 0x68; nextTime += 3000 ; //serialPuts (fd, s1); serialPutchar (fd, a); serialPutchar (fd, b); s1[10] = s1[10] + 1; s1[22] = s1[22] + 1; } //fflush (stdout) ; } return 0 ; }
void SerialComm::SendData() { // unsigned char dataOut[100]; unsigned char CRC = 0; // printf("%d\n", sizeof(sMapOut)); // memset(&VariablesOut, 0, sizeof(sVarsOut)); // bufferOut.data.s16[5] = 0; // bufferOut.data.s16[6] = 4; //// printf("%d", dataOut[5]); // bufferOut.data.s16[25] = 100; printf("%s\n", __FUNCTION__); unsigned char u8Retry=0; // do { serialPutchar(fd, 's'); CRC = 's'; // sMapOut *map = &VariablesOut.Names; // map->DelayBlower = 45; // map->SaunaDelayLight = 99; // map->Digital[0] = (1<<0); // map->O_Blower = 1; for (int i=0; i<90; i++) { CRC = VariablesOut.Buffer.u8[i]^CRC; // printf("%X %x", CRC, VariablesOut.Buffer.u8[i]); serialPutchar(fd, VariablesOut.Buffer.u8[i]); // printf("%d:%d ", i ,VariablesOut.Buffer.u8[i]); } printf("\nCRC:%X\n", CRC); serialPutchar(fd, CRC); for (int i=0; i<3; i++) { // printf("%d:%d ", i, VariablesOut.Names.Digital[i]); } for (int i=0; i<42; i++) { // printf("%d:%d ", i, VariablesOut.Names.Analog[i]); } for (int i=0; i<3; i++) { VariablesIn.Names.Digital[i] = VariablesOut.Names.Digital[i]; } for (int i=0; i<42; i++) { VariablesIn.Names.Analog[i] = VariablesOut.Names.Analog[i]; } // printf("\n"); // serialPrintf(fd) delay(200); // u8Retry++; // } while ((u8Retry < 3)&&(false == ReadRAW())); }
void change_size() { serialPutchar(fd,0x56); serialPutchar(fd,0x00); serialPutchar(fd,0x31); serialPutchar(fd,0x05); serialPutchar(fd,0x04); serialPutchar(fd,0x01); serialPutchar(fd,0x00); serialPutchar(fd,0x19); serialPutchar(fd,0x00); }
void sendchar(char varchar) { int val=varchar; fflush(stdout); serialPutchar(fd,varchar); //printf("send number is: %d\n",val); }
int main () { int fd,i=0; fd= serialOpen("/dev/ttyAMA0",115200); if(fd < 0) { printf("Opening serial failed.\n"); return 0; } while(i<10) { delay(500); serialPutchar(fd,i); i++; //printf("serialDataAvail: %d\n", serialDataAvail(fd)); if(serialDataAvail(fd) >= 1) { printf ("->%d\n", serialGetchar(fd)); fflush (stdout); } } printf("Serial port closing.\n"); serialClose(fd); return 0; }
int main(int argc, char *argv[]){ if (argc < 3 ){ printf("Usage:\nlcdprint line col [text]\n"); return 1; }else{ int col=atoi(argv[2]); if (col<0 || col> 15){ printf("col must be between 0 and 15\n"); return 2; } int line=atoi(argv[1]); if (line<0 || line> 1){ printf("col must be between 0 and 1\n"); return 3; } lcd=serialOpen(SERIAL, BAUD_RATE); setLCDCursor(line*16+col); if (argc>3){ int i; for (i=3;i<argc;i++){ serialPuts(lcd, argv[i]); if (i<argc-1){ serialPuts(lcd, " "); } } }else{ char c; while ((c=getchar()) !=255){ serialPutchar(lcd, c); } } serialClose(lcd); return 0; } }
int main () { printf("Program started.\n"); int ser_handle; // the serial connection (file descriptor) if ((ser_handle = serialOpen("/dev/ttyAMA0", BAUD_RATE)) < 0) { fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno)); return 1 ; } else printf("Serial open\n"); int counter = 0; int avail_bytes; for(;;) { if(avail_bytes = serialDataAvail(ser_handle)) { serialPutchar(ser_handle,serialGetchar(ser_handle)); counter = counter + 1; if(counter%100 == 0) { printf("Byte %i has been passed on\n", counter); } } } }
void Serial::PutChar(unsigned char c){ if(Open()){ // std::cout << "Wrote character: " << c << std::endl; serialPutchar(this->fd, c); // Close(); } }
void transmit(void) { // Pong every 3 seconds if (millis() - time >= 3000) { serialPuts(fd0, "Pong!\n"); serialPuts(fd1, "Pong1!\n"); serialPuts(fd2, "Pong2!\n"); // you can also write data from 0-255 // 65 is in ASCII 'A' serialPutchar(fd0, 65); serialPutchar(fd1, 65); serialPutchar(fd2, 65); time = millis(); } }
void cleanup(void){ serialPrintf(fd, "%cSDMP", 13); for(i = 0; i < 25; i++){ serialPutchar(fd,0); } fclose(fp); serialClose(fd); }
void changeBaud(int fd, uint32_t rate){ int i = 0; while (rate != bauds[i]){ i++; } cmd(fd, BAUD); serialPutchar(fd, i); }
void RS485::puts(char *data, int len) { digitalWrite(DIRPIN, DIR_WRITE); usleep(5000); for(int i=0; i<len; i++) serialPutchar(dev, data[i]); usleep(5000); digitalWrite(DIRPIN, DIR_READ); }
int main (void) { int handle = serialOpen ("/dev/ttyAMA0", 19200); //115200) ; serialParity(handle,1); while(1) { serialPutchar(handle,0b10100000); usleep (800); } return 0 ; }
int start_capturing() { serialPutchar(fd,0x56); serialPutchar(fd,0x00); serialPutchar(fd,0x36); serialPutchar(fd,0x01); serialPutchar(fd,0x00); int i = 0; char temp[5]; char return_val[] = {0x76,0x0,0x36,0x0,0x0}; while(i<5) { temp[i] = serialGetchar(fd); if(temp[i] != return_val[i]) return -1; i++; } return 1; }
int drcSetup (const int pinBase, const int numPins, const char *device) { int fd ; int ok, tries ; time_t then ; struct wiringPiNodeStruct *node ; if ((fd = serialOpen (device, 115200)) < 0) return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ; delay (10) ; // May need longer if it's an Uno that reboots on the open... // Flush any pending input while (serialDataAvail (fd)) (void)serialGetchar (fd) ; ok = FALSE ; for (tries = 1 ; tries < 5 ; ++tries) { serialPutchar (fd, '@') ; then = time (NULL) + 2 ; while (time (NULL) < then) if (serialDataAvail (fd)) { if (serialGetchar (fd) == '@') { ok = TRUE ; break ; } } if (ok) break ; } if (!ok) { serialClose (fd) ; return wiringPiFailure (WPI_FATAL, "Unable to communidate with DRC device") ; } node = wiringPiNewNode (pinBase, numPins) ; node->fd = fd ; node->pinMode = myPinMode ; node->pullUpDnControl = myPullUpDnControl ; node->analogRead = myAnalogRead ; node->digitalRead = myDigitalRead ; node->digitalWrite = myDigitalWrite ; node->pwmWrite = myPwmWrite ; return 0 ; }
int serial_writebyte(int serial_id, uint8_t byte) { struct serial_bus *sb = serial_buses[serial_id]; if(!sb) return -1; if(sb->fd < 0) { sb->fd = serialOpen(sb->bus, sb->speed); } serialPutchar (sb->fd, byte) ; return 0; }
int main(void) { int handle = serialOpen ("/dev/ttyAMA0", 9600); //Open the Serial port serialFlush(handle); //Clear the stream of old data packets while(1) { serialPutchar (handle, 's'); //Send 's' to the Arduberry char inp=serialGetchar(handle); //Receive the characters from the Arduberry printf("%c",inp); } return 0; }
static ERL_NIF_TERM serial_put_char_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { int handle, character; if (!enif_get_int(env, argv[0], &handle) || !enif_get_int(env, argv[1], &character)) { return enif_make_badarg(env); } serialPutchar(handle, (uint8_t)character); return atom_ok; }
void WpiEngine::serialWrite() { static unsigned int lastTime = millis(); unsigned int thisTime = millis(); if ( thisTime - lastTime > 60e3 ) { lastTime = thisTime; time_t timer = time(NULL); unsigned short thetime = round( difftime(timer, mktime(&time2016)) * 1000 ) ; avgHumidity = Humidity / (double) nHumidity; avgTemperature = Temperature / (double) nTemperature; avgPressure = Pressure / (double) nPressure; avgWind = Wind / (double) nWind; unsigned short humidity = round( avgHumidity * 100 ); short temperature = round( avgTemperature * 100 ) ; unsigned short pressure = round( avgPressure ) ; unsigned short wind = round( avgWind * 100 ) ; char winddir = 0; char message[34]; sprintf(message, "AT+DTX=22,"); sprintf(message+10, "%04X%04X%04X%04X%04X%02X", thetime, humidity, temperature, pressure, wind, winddir); message[32] = '\r'; message[33] = '\n'; for (int i=0; i<34; i++) serialPutchar (fd, message[i]); printf("Sent to serial: %s\n", message); windoo_humidity = avgHumidity; windoo_temperature = avgTemperature; windoo_pressure = avgPressure; windoo_wind = avgWind; location_heading = winddir; post(); Temperature = Wind = Pressure = Humidity = 0; nTemperature = nWind = nPressure = nHumidity = 0; } /*if (serialDataAvail (fd)) { printf (" --> "); printf ("%c", serialGetchar (fd)); while (serialDataAvail (fd)) printf ("%c", serialGetchar (fd)) ; printf ("\n"); }*/ }
int send_usart(uint16_t len,uint8_t *data) //id,state1,state2,state3 1=开,0=关,2=保持 { uint8_t i=0; if(len>100) { printf("send usart failed! len>100"); return 1; } for(i=0;i<len;i++) serialPutchar(usart_fd,data[i]); return 0; }
int8_t CProtocol::sendAddress(uint8_t address, uint32_t data){ // Only write if there is a Stream/filedescriptor to write to #ifdef RaspberryPi if(mSerial<0) return-1; #else //Arduino if (mSerial==NULL) return-1; #endif // block buffer for sending uint8_t b[6]; // b[5] has the ‘address’ byte b[5] = 0x80 | ((address-1) & 0x3F); // fill in the rest of the data, b[0]-b[4] is going to have data // in MSB order, e.g. b[4] will have the lowest 7 bits, b[3] the next // lowest, etc... b[4] = data & 0x7F; uint8_t blocks=2; // only loop/shift if there's data to put out while(data>0x7F) { data >>= 7; b[6-(++blocks)] = data & 0x7F; } // if we can fit our highest bits in the first block, add them here if((blocks==6) || b[6-blocks] < 8) { // add to existing data } // if not just initialize the next byte else b[6-(++blocks)] = 0; // add in the block count to lead b[6-blocks] |= 0xC0 | (blocks<<3); // now write out the data - the blocks array in reverse, which will // get your data written out in LSB order #ifdef RaspberryPi for(int i=0;i<blocks;i++){ serialPutchar (mSerial, b[6-blocks+i]); } #else //Arduino // we need to write the buffer as array to work for the Wire library mSerial->write(&b[6-blocks],blocks); #endif // return the number of blocks written to the Serial return blocks; }
//initialize the GPIO and UART pins for the Raspberry Pi 2 int initGPIO_Uart(void) { //manually configure the GPIO pins for inputs or outputs using terminal commands //terminal commands: https://projects.drogon.net/raspberry-pi/wiringpi/the-gpio-utility/ system("gpio mode 0 out"); //set GPIO pin 1 to output (breakBeamLEDOutput pin 17) indicates puck is held by robot system("gpio mode 1 out"); //set GPIO pin 1 to output (shootpin pin 18) system("gpio mode 3 out"); //set GPIO pin 3 to output (controllerConnectedLEDOutput pin 22) system("gpio mode 4 out"); //set GPIO pin 4 to output //GPIO pin 23 output controls the solenoid discrete output system("gpio mode 2 in"); //set GPIO pin 2 to input //GPIO pin 27 input from break beam (garage-door-like sensor) system("gpio mode 5 in"); //set GPIO pin 5 to input //GPIO pin 24 input from break beam (garage-door-like sensor) //Initialize the Wiring Pi Libary pinMode(breakBeamInput, INPUT); pullUpDnControl(breakBeamInput, PUD_UP); // Enable pull-down resistor on button pinMode(shutdownPiSwitchInput, INPUT); pullUpDnControl(shutdownPiSwitchInput, PUD_DOWN); // Enable pull-down resistor on button pinMode(breakBeamLEDOutput, OUTPUT); pinMode(shootPinOutput, OUTPUT); pinMode(controllerConnectedLEDOutput, OUTPUT); pinMode(enableAndGateOutput, OUTPUT); //initialize the UART @ 19200 BAUD if ((UART_ID = serialOpen(UART_TXD0, 19200)) < 0) { fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno)); return 0; } //Initialize WiringPi -- using Broadcom processor pin numbers wiringPiSetupGpio(); digitalWrite(enableAndGateOutput, HIGH); //enable the AND GATE and allow the UART and breakBeam outputs to turn on usleep(2000000); //wait 2 seconds (in microseconds) to act as a power up delay for the Sabertooth Motor Controller serialPutchar(UART_ID, 0xAA); //Send the autobauding character to Sabertooth first to stop motors from twitching! usleep(100000); //wait 100ms (in microseconds) before commanding motors if (wiringPiSetup() == -1) { fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno)); return 0; } printf("Initialized GPIO and UART!\r\n"); //if return(0), something did not get initialized correctly }
void command_ctl(const char cmd[]) { if(strcmp(cmd, "rd!") == 0) { // read serialPutchar(fd, 114); serialPutchar(fd, 100); serialPutchar(fd, 33); } else if(strcmp(cmd, "vo!") == 0) { // ventil open serialPutchar(fd, 118); serialPutchar(fd, 111); serialPutchar(fd, 33); } else if(strcmp(cmd, "vo!") == 0) { // ventil close serialPutchar(fd, 118); serialPutchar(fd, 99); serialPutchar(fd, 33); } else { printf("wrong command\n"); } }
int serial_writebytes(int serial_id, uint8_t *bytes, uint8_t length) { struct serial_bus *sb = serial_buses[serial_id]; int i; if(!sb) return -1; if(sb->fd < 0) { sb->fd = serialOpen(sb->bus, sb->speed); } for (i=0; i<length; i++) { serialPutchar (sb->fd, bytes[i]); } return 0; }
void setCursor(int fd, uint8_t y, uint8_t x){ uint8_t pos; // line one starts at 0x00 line two at 0x40 if (y == 1) pos = 0x00 + x; else if (y == 2) pos = 0x40 + x; else if (y == 3) pos = 0x14 + x; else if (y == 4) pos = 0x54 + x; else pos = 0x00 + x; cmd(fd, POSITION); serialPutchar(fd, pos); }