@jpearman I’m currently working on interfacing a custom sensor into the brain using vexcode c++. Could you please expand a bit more on the post I linked as to how to setup a generic serial port and receive and integer (or other datatype) input?
3 Likes
What exactly do you need help with? What format is the serial data that you are receiving?
so that thread was dealing with Python.
One option is the use the vexlink class in a raw mode, best way is to sub class serial_link to give access to baudrate setting ability, here’s some code to get you started, you can test this as a loopback test using ports 1 and 2.
If all you care about is receiving data from one sensor that just spits it out occasionally, just use a receive link.
example
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: james */
/* Created: 11/22/2024, 4:12:19 PM */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
#include "vex.h"
using namespace vex;
// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain Brain;
// subclass serial_link to allow access to baudrate set function
class sensor_link : public vex::serial_link {
public:
// name not used
// isWired not used, can only be wired if raw link
sensor_link(int32_t index) : serial_link(index, "name", vex::linkType::raw, true) {
baud( 115200 );
}
virtual ~sensor_link() {
}
};
// setup ports to send and receive data
sensor_link tx_link( PORT1 );
sensor_link rx_link( PORT2 );
int
tx_thread() {
char *tx_buf = "Hello";
while(1) {
tx_link.send(tx_buf, strlen(tx_buf) );
this_thread::sleep_for(100);
}
return 0;
}
int
rx_thread() {
uint8_t rx_buffer[128];
while(1) {
if( rx_link.isReceiving() ) {
int nRead = rx_link.receive( rx_buffer, sizeof(rx_buffer), 50 );
if( nRead > 0 ) {
for(int i=0;i<nRead;i++) {
printf("%02X(%c) ", rx_buffer[i], rx_buffer[i] );
}
printf("\n");
}
}
else {
this_thread::sleep_for(10);
}
}
return 0;
}
int main() {
thread tx( tx_thread );
thread rx( rx_thread );
while(1) {
// Allow other tasks to run
this_thread::sleep_for(10);
}
}
5 Likes