MX Foundation 4
async_flexsmp_rs232.c
/*****************************************************************************
//
// File:
// async_flexsmp_rs232.c
//
// Copyright (c) MAX Technologies Inc. 1988-2023, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates the basic usage of ASYNC enhanced channel class
// for transmission and reception on FlexSMP Module with RS-232 Electrical Interface.
//
// Hardware requirements:
// - MAXT FlexMax Device with FMX-SMP-040804 Module
//
*****************************************************************************/
#include "example.h"
//#define LOCAL
//#define LOOPBACK
int main(void) {
uint32 rc = MAXT_SUCCESS;
HMXF_SERVER server = 0;
HMXF_DEVICE device = 0;
HMXF_MODULE module = 0;
HMXF_CHANNEL rxChannel = 0;
HMXF_CHANNEL txChannel = 0;
size_t bufferSize = 0;
HMXF_BUFFER mxfRxBuffer = 0;
HMXF_BUFFER mxfTxBuffer = 0;
uint64 rxAcqStatus = 0, deviceCount = 0, moduleCount = 0, msgCount = 0, byteCount = 0;
// Get Server Handle
#ifdef LOCAL
rc = mxfConnectPCIE(&server);
#else
rc = mxfConnectEthernet("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
// Initialize MX Foundation library
if (!rc) {
printf("Initializing ...\n");
rc = mxfSystemInit(server);
}
// Get Device handle
if (!rc) {
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_FLEXMAX, 1, &deviceCount, &device);
}
// Get Module handle
if (!rc && deviceCount) {
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_FLEXSMP, 1, &moduleCount, &module);
}
if(!rc && !moduleCount) {
rc = MAXT_ERROR_NOT_FOUND;
}
// Get Receiver(Rx) Channel Handle
if (!rc) {
rc = mxfModuleChannelGet(module, 28, &rxChannel);
}
// Get Transmiter(Tx) Channel Handle
if (!rc) {
rc = mxfModuleChannelGet(module, 8, &txChannel);
}
// Set Channel Class to Async
if (!rc) {
rc = mxfAttributeUint64Set(rxChannel, KMXF_CHANNEL_CLASS, MXF_CLASS_ASYNC_ENHANCED);
}
if (!rc) {
rc = mxfAttributeUint64Set(txChannel, KMXF_CHANNEL_CLASS, MXF_CLASS_ASYNC_ENHANCED);
}
// Set Electrical Termination to None
if (!rc) {
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_TERMINATION, VMXF_ASYNCEH_TERMINATION_NONE);
}
if (!rc) {
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_TERMINATION, VMXF_ASYNCEH_TERMINATION_NONE);
}
// Set Channel Speed to 115.2 Kbps
if (!rc) {
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_SPEED, 115200);
}
if (!rc) {
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_SPEED, 115200);
}
// Set Parity to Odd
if (!rc) {
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_PARITY, VMXF_ASYNCEH_PARITY_ODD);
}
if (!rc) {
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_PARITY, VMXF_ASYNCEH_PARITY_ODD);
}
// Set Rx Channel string gap to 4
if (!rc) {
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_RX_STRING_GAP, 4);
}
#ifdef LOOPBACK
// Set Rx Channel loopback
if (!rc) {
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_TX_RX_TEST_LB, VMXF_ENABLE);
}
#endif
bufferSize = (uint64)sizeof(MXF_ASYNCEH_DATAREC);
// Allocate acquisition static buffer
if (!rc) {
rc = mxfRxAcqBufferAlloc(rxChannel, bufferSize, &mxfRxBuffer, NULL);
}
// Start acquisition service
if (!rc) {
rc = mxfRxAcqStart(mxfRxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
}
// Allocate TX Aperiodic static buffer with HIGH priority queue
if (!rc) {
rc = mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_HIGH, bufferSize, &mxfTxBuffer, NULL);
}
// Transmit One Record
if (!rc) {
char* data = "Hello, World!";
txRec.reserved = 0;
txRec.timeTag = 0;
txRec.control = 0;
txRec.repeatCount = 1;
txRec.dataSize = (uint32)strlen(data)+1;
memcpy(txRec.data, data, txRec.dataSize);
printf("Transmitting ...\n");
rc = mxfASYNCEHTxAperiodicWrite(mxfTxBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, 1, &txRec);
}
//Wait
if (!rc) {
mxfSleep(1000);
}
// Read received record
if (!rc) {
printf("Receiving ...\n");
rc = mxfASYNCEHRxAcqRead(mxfRxBuffer, 1, bufferSize, &rxAcqStatus, &msgCount, &byteCount, &rxRec);
// Display received string
if(!rc && msgCount == 1) {
printf(" Timetag: %012"PRIu64", Size: %u\n", rxRec.timeTag, rxRec.dataSize);
printf(" String: %s\n", (char*)rxRec.data);
}
}
// Stop acquisition
if (!rc) {
rc = mxfRxAcqStop(mxfRxBuffer);
}
// Display Return Error Codes
if (rc) {
char errorString[200];
if (mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString)) {
sprintf(errorString, "ERROR # 0x%X", rc);
}
printf("%s\n\r", errorString);
}
printf("Terminating ...\n");
// Free mxf Buffers
if (mxfRxBuffer) {
mxfRxAcqBufferFree(mxfRxBuffer);
}
if (mxfTxBuffer) {
}
// Unload MX Foundation library
// Disconnect from MX Foundation library
printf("\nPress a key to exit\n");
getchar();
return 0;
}
Updated 10/23/2023