CANBUS nmea 2000 phrase protocol packet on STM32 - embedded

I have STM32 microcontroller F4
I want to configure the GPS sensor for getting data, I used CANBUS cable and the protocol is nmea 2000, and communicate with HAL_CAN interrupt.
my question is, how can I phrase the data from the sensor (on nmea 2000).
I want to get data from PGN 127257, I need to transmit data (PGN 127257) for reading?
or only phrase the data, and if phrase how?
thanks a lot!
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan){
HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RXheader, an_packet.NemaData);
u32_2b.bytes[0] = an_packet.NemaData[0];
u32_2b.bytes[1] = an_packet.NemaData[1];
u32_2b.bytes[2] = an_packet.NemaData[2];
u32_2b.bytes[3] = an_packet.NemaData[3];
mid1 = u32_2b.data;
mid = (mid1 >> 8) & 0x01ffff;
if((mid == 126992) || (mid == 127257) || (mid == 127252) || (mid == 129540)) {
GPS.test1 = 5;
}
}
an_packet.NemaData = getting data from the sensor.
u32_2b.bytes[0] = an_packet.NemaData[0];
u32_2b.bytes[1] = an_packet.NemaData[1];
u32_2b.bytes[2] = an_packet.NemaData[2];
u32_2b.bytes[3] = an_packet.NemaData[3];
mid1 = u32_2b.data;
mid = (mid1 >> 8) & 0x01ffff;
if((mid == 126992) || (mid == 127257) || (mid == 127252) || (mid == 129540)) {
GPS.test1 = 5;
}
trying to phrase it...

Related

How to send multiple characteristics under one BLE Service stm32wb15

I try to read 2 characteristics ( accelerometer and distance laser sensor )under one BLE Service. I can read distance data successfully but other AccelValueZ data is always zero.SensorHeightValue is int16_t type. How to read this two data from my phone ? Code is :
void MPU6050_Receive_Sensor_Data(void){
double xAccel ,yAccel ,zAccel;
mpu6050_get_measurement(&xAccel, &yAccel, &zAccel);
P2P_Server_App_Context.SensorControl.MPU6050AccelValueX = xAccel;
P2P_Server_App_Context.SensorControl.MPU6050AccelValueY = yAccel;
P2P_Server_App_Context.SensorControl.MPU6050AccelValueZ = zAccel;
P2PS_STM_App_Update_Char(P2P_READ1_CHAR_UUID, (uint8_t *)&P2P_Server_App_Context.SensorControl.MPU6050AccelValueZ);
if((P2P_Server_App_Context.SensorControl.MPU6050AccelValueZ <= 100) && (P2P_Server_App_Context.SensorControl.MPU6050AccelValueZ >= -100)){
VL53L1X_Receive_Sensor_Data_Action();
}
return;
}
void VL53L1X_Receive_Sensor_Data(void){
P2P_Server_App_Context.SensorControl.SensorHeightValue = vl53l1x_get_measurement();
P2PS_STM_App_Update_Char(P2P_READ_CHAR_UUID, (uint8_t *)&P2P_Server_App_Context.SensorControl.SensorHeightValue);
// P2PS_STM_App_Update_Char(P2P_READ1_CHAR_UUID, (uint8_t *)&P2P_Server_App_Context.SensorControl.MPU6050AccelValueZ);
return;
}```

STM32F4 - USART - Receive data array - reads only first frame

today I'm fighting with controlling my uC from the PC.
On a button click, I'm sending data to uC which is to change some values in the program. First of all, the problem is that my program reads only first frame received. Secondly, my program gets stuck when I reinitialize PDM_Filter Structure
What is important - Rx buffer of USART is 16-bit.
Data format is like 6x byte (uint8) that are:
0) ASCII symbol - ! or # or & - symbol indicates what happens next;
1) mic_gain; 2)3) HP Filter cutoff freq; 4)5) LP Filter cutoff freq
So I am receiving 3 x uint16
How it looks like on uC code:
union
{
uint16_t szes[3];
uint8_t os[6];
}RxBuffer;
Interrupt Handler - I had cut the part that works just fine.
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
j=0;
while (j<3)
{
RxBuffer.szes[j] = USART_ReceiveData(USART1);
j++;
}
if(RxBuffer.os[0] == 39)
{
DoFlag = false;
I2S_Cmd(SPI2, DISABLE);
//_PDM(sampling_frequency, RxBuffer.szes[2], RxBuffer.szes[1]);
Filter.HP_HZ = RxBuffer.szes[1];
Filter.LP_HZ = RxBuffer.szes[2];
PDM_Filter_Init(&Filter);
GPIO_SetBits(GPIOD, LED_Blue);
mic_gain = RxBuffer.os[1];
Delay(1000);
GPIO_ResetBits(GPIOD, LED_Blue);
I2S_Cmd(SPI2, ENABLE);
DoFlag = true;
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
How intialization of PDM_Filter structure (from libPDMFilter_Keil.lib) looks for me:
Filter.Fs = frequency_s;
Filter.HP_HZ = high_pass_cutoff;
Filter.LP_HZ = low_pass_cutoff;
Filter.In_MicChannels = 1;
Filter.Out_MicChannels = 1;
PDM_Filter_Init(&Filter);
I'm trying to change cutoff frequencies - in order to do so, I reinitialize structure.

Issues in ADC of PIC16F887 reading LM35 temperature value is frequently varying

I have connected the LM35 temperature sensor in analog channel of AN1 then transmitting the temperature through serial RS232 in PIC16F887. I have read the temperature value but the value is frequently varying as 31 and 32, also when touch the LM35 temperature is varying very fast as 32, 33 and etc. How to control the constant temperature value. Here is my code, Please any one help me.
#include <htc.h>
void main(void)
{
TRISA1 = 1;
ANS1 = 1;
OSCCON = 0x78; // OSCILLATOR CONTROL REGISTER
TXSTA = 0x26;
RCSTA = 0x90;
SPBRG = 10;
ADCON1 = 0x80;
unsigned int current_temp, initial_temp = temperature();
transmit_char(initial_temp);
flag = 0;
while (1)
{
current_temp = temperature();
if((current_temp == (initial_temp + 1)) || (current_temp == (initial_temp - 1)))
{
flag = 1;
}
if(flag == 1)
{
flag = 0;
transmit_char(current_temp);
initial_temp = current_temp;
}
}
}
int temperature(void)
{
ADCON0 = 0xC5;
GODONE = 1;
while(GODONE);
int temp;
temp = (ADRESH << 8) + ADRESL;
temp = temp/2;
return temp;
}
Do this,
Collect ten adc data,
Remove min and max from that values.
sum the remaining 8 ADC values and divide the value by 8.
this should be continuous process, average for every ten samples
note: sampling and averaging should be ... 1 to 10 , next 2 to 11 and 3 to 12. at every reading you will get the adc value. sudden fluctuations also get filtered here.
Hope this helps.. good luck..

libusb-1.0 - Where does the data go after a successful libusb_bulk_transfer() call?

I ran the following code sample obtained from a tutorial here: http://www.dreamincode.net/forums/topic/148707-introduction-to-using-libusb-10/
I ran it against a tablet running Android 4 attached via USB to a MacBook Pro running Mac OS X Mountain Lion.
The output contained "Writing Successful!" so I assume libusb_bulk_transfer() worked correctly. My question is where did the 4 bytes of data got transferred to? Can I save it to a file on the Android tablet?
Thanks for your help!
#include <iostream>
#include <libusb.h>
using namespace std;
int main() {
libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
libusb_device_handle *dev_handle; //a device handle
libusb_context *ctx = NULL; //a libusb session
int r; //for return values
ssize_t cnt; //holding number of devices in list
r = libusb_init(&ctx); //initialize the library for the session we just declared
if(r < 0) {
cout<<"Init Error "<<r<<endl; //there was an error
return 1;
}
libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
if(cnt < 0) {
cout<<"Get Device Error"<<endl; //there was an error
return 1;
}
cout<<cnt<<" Devices in list."<<endl;
dev_handle = libusb_open_device_with_vid_pid(ctx, 5118, 7424); //these are vendorID and productID I found for my usb device
if(dev_handle == NULL)
cout<<"Cannot open device"<<endl;
else
cout<<"Device Opened"<<endl;
libusb_free_device_list(devs, 1); //free the list, unref the devices in it
unsigned char *data = new unsigned char[4]; //data to write
data[0]='a';data[1]='b';data[2]='c';data[3]='d'; //some dummy values
int actual; //used to find out how many bytes were written
if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached
cout<<"Kernel Driver Active"<<endl;
if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
cout<<"Kernel Driver Detached!"<<endl;
}
r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1)
if(r < 0) {
cout<<"Cannot Claim Interface"<<endl;
return 1;
}
cout<<"Claimed Interface"<<endl;
cout<<"Data->"<<data<<"<-"<<endl; //just to see the data we want to write : abcd
cout<<"Writing Data..."<<endl;
r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0); //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129
if(r == 0 && actual == 4) //we wrote the 4 bytes successfully
cout<<"Writing Successful!"<<endl;
else
cout<<"Write Error"<<endl;
r = libusb_release_interface(dev_handle, 0); //release the claimed interface
if(r!=0) {
cout<<"Cannot Release Interface"<<endl;
return 1;
}
cout<<"Released Interface"<<endl;
libusb_close(dev_handle); //close the device we opened
libusb_exit(ctx); //needs to be called to end the
delete[] data; //delete the allocated memory for data
return 0;
}
The libusb_bulk_transfer() function writes the data at the specified endpoint. Its an address, access it, print it, or you can copy it, do some case conversion and dump it to an outgoing endpoint. And, then perform a read through libusb_bulk_transfer()

How to get the mac address in WinRT (Windows 8) programmatically?

I am looking for an API in WinRT to access the mac address.
You can't retrieve the MAC Address per say, but you do can retrieve hardware specific information to identify a machine if that's what you're trying to do.
Here's a complete msdn article discussing the subject: Guidance on using the App Specific Hardware ID (ASHWID) to implement per-device app logic (Windows)
Be careful to use just the information you need and not the complete id, as it might change based on information that are useless to you (such as the Dock Station bytes for instance).
Here's a code sample of a computed device id based on a few bytes (CPU id, size of memory, serial number of the disk device and bios):
string deviceSerial = string.Empty;
// http://msdn.microsoft.com/en-us/library/windows/apps/jj553431
Windows.System.Profile.HardwareToken hardwareToken = Windows.System.Profile.HardwareIdentification.GetPackageSpecificToken(null);
using (DataReader dataReader = DataReader.FromBuffer(hardwareToken.Id))
{
int offset = 0;
while (offset < hardwareToken.Id.Length)
{
byte[] hardwareEntry = new byte[4];
dataReader.ReadBytes(hardwareEntry);
// CPU ID of the processor || Size of the memory || Serial number of the disk device || BIOS
if ((hardwareEntry[0] == 1 || hardwareEntry[0] == 2 || hardwareEntry[0] == 3 || hardwareEntry[0] == 9) && hardwareEntry[1] == 0)
{
if (!string.IsNullOrEmpty(deviceSerial))
{
deviceSerial += "|";
}
deviceSerial += string.Format("{0}.{1}", hardwareEntry[2], hardwareEntry[3]);
}
offset += 4;
}
}
Debug.WriteLine("deviceSerial=" + deviceSerial);
There is no way to do it. The Windows Store App APIs are sandboxed and are pretty restrictive on the information that you can get about the user, mainly because of privacy concerns.