SerialPort Encoding - c++

I try to configure serial port in C++, every thing goes right except its Encoding.
My code is:
serialPort->PortName = "COM3";
serialPort->BaudRate = 115200;
serialPort->NewLine = "***";
serialPort->Encoding = Encoding->GetEncoding(28591);
I get the following error:
type name is not allowed.
How to use Text Encodings in C++?

I guess Encoding is a type name, and the last line in the code snippet must be
serialPort->Encoding = Encoding::GetEncoding(28591);

Related

CJson - Insert missing quotes around strings

I have a text file of format given below (this is just sample file, but actually file is big one with key, values). Some of the values for the key will have format in file (look for value of Port key in below file). This is input file for me.
I am converting this file into Json using CJson Library in C++. Since value corresponding to Port key is not a valid string or value, CJson gives error message saying "Insert missing quotes around strings". So I tried to convert Port value to string using below code. But I am unable to insert missing quotes around. Could you please help me how to "Insert missing quotes around strings".
Code I tried:
ifstream infile { "conf_file.conf" };
string file_contents{ istreambuf_iterator<char>(infile), istreambuf_iterator<char>() };
pParentJson = cJSON_Parse( (char*)file_contents.c_str() );
cJSON* pJsonObj = cJSON_GetObjectItem(pParentJson,"port");
char* pDataBuffer = cJSON_Print(pJsonObj);
cout<<pDataBuffer<<endl;
Text File
{
"port": <CbTcpPortVariable>
"IP" : "127.0.0.1"
"Message": "Hello"
}

How to get Body of the email using c++ builder

I want to get the email body from my Gmail account for an email so i use this code i found it in an example for how to read emails using c++ builder pop3
the code to extract body used
TIdText *EText;
int message = SpinEdit1->Value;
MyPoP3->Retrieve(message, MyEmail);
Edit1->Text = MyEmail->Subject + " | " + MyEmail->From->Address;
Memo1->Clear();
for (int i = 0; i < MyEmail->MessageParts->Count; i++) {
Memo1->Lines->Add(MyEmail->MessageParts->Items[i]->ContentType);
EText = dynamic_cast<TIdText*>(MyEmail->MessageParts->Items[i]);
Memo1->Lines->Add(EText->Body);
}
the problem is that i got undefine symbol to TidText and what i tried is to change it from TIdText to TIdMessage, but i got that i can't convert to it.
also i tried to try this without loop or something MyEmail->Body->Text
this return empty string.
the video i got this code from it here i don't know maybe the c++ builder he use is old. now i want to know how to extract the body text from the email address.
Thanks in advance.
the problem is that i got undefine symbol to TidText
Your code is missing an #include <IdText.hpp> statement.
what i tried is to change it from TIdText to TIdMessage, but i got that i can't convert to it.
Because TIdMessage does not contain nested TIdMessage objects.
also i tried to try this without loop or something MyEmail->Body->Text this return empty string.
If your email is MIME encoded, its text is not stored in the TIdMessage::Body property, but in a nested TIdText object within the TIdMessage::MessageParts collection. You have to look at the TIdMessage::ContentType property to know what kind of structure the email contains. For instance, if the CT begins with text/, the text is in the TIdMessage::Body. But if the CT begins with multipart/, the text is somewhere in the TIdMessage::MessageParts instead.
You should read this blog article on Indy's website for an example of how emails might be structured:
HTML Messages
the video i got this code from it here i don't know maybe the c++ builder he use is old.
No, it is not.

How to parse output from sse.client in Python?

I am new to Python and am trying to get my ahead around parsing SSE client code. I am using the SSE Client library. My code is very basic and follows the sample exactly. Here it is:
from sseclient import SSEClient
devID = "xxx"
AToken = "xxx"
sparkURL = 'https://api.spark.io/v1/devices/' + devID + '/events/?access_token=' + AToken
messages = SSEClient(sparkURL)
for msg in messages:
print(msg)
print(type(msg))
The code runs without a problem and I see some blank lines and SSE data coming through. Here is the sample output:
<class 'sseclient.Event'>
{"data":"0 days, 0:54:43","ttl":"60","published_at":"2015-04-09T22:43:52.084Z","coreid":"xxxx"}
<class 'sseclient.Event'>
<class 'sseclient.Event'>
{"data":"0 days, 0:55:3","ttl":"60","published_at":"2015-04-09T22:44:12.092Z","coreid":"xxx"}
<class 'sseclient.Event'>
The actual output above looks like a dictionary, but its type is "sseclient.Event". I am trying to figure out how to parse the output so I can pull out one of the fields and nothing I have tried has worked.
Sorry if this is basic questions, but can someone provide some simple guidance on how I would either convert the entire output to a dictionary or perhaps just pull out one of the fields?
Thank you in advance!
I figured this out. In case anyone else experiences the same problem, here is how I got it to work. The key was using msg.data and not just msg. I then converted the out using the JSON library and am good to go.
messages = SSEClient(sparkURL)
for msg in messages:
outputMsg = msg.data
if type(outputMsg) is not str:
outputJS = json.loads(outputMsg)
FilterName = "data"
#print( FilterName, outputJS[FilterName] )
print(outputJS[FilterName])

MAPI, HrQueryAllRows: Filter messages on subject

I'm pretty much new to MAPI and haven't wrote much C++ Code.
Basically I want to read all emails in the inbox and filter them based on their subject text. So far I'm using the source code provided at the microsoft msdn website which basically reads all emails from the inbox. What I want now is to not get all emails but filter them on the subject, lets say: I want all emails in my Inbox with the subject title "test".
So far I figuered out that the following line of code retrieves all the mails:
hRes = HrQueryAllRows(lpContentsTable, (LPSPropTagArray) &sptCols, &sres, NULL, 0, &pRows);
The parameter &sres is from the type SRestriction.
I tried to implement a filter on 'test' in the subject:
sres.rt = RES_CONTENT;
sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;
sres.res.resContent.ulPropTag = PR_SUBJECT;
sres.res.resContent.lpProp = &SvcProps;
SvcProps.ulPropTag = PR_SUBJECT;
SvcProps.Value.lpszA = "test";
ScvProps is from the type SPropValue.
If i execute the application then I get 0 lines returned. If I change the String test to an empty String then I get all emails.
I'm assuming i'm using the "filter" option wrong, any ideas?
Edit: When I change the FuzzyLevel to:
sres.res.resContent.ulFuzzyLevel = FL_SUBSTRING;
then I can look for subjects that contain a single character but as soon as I add a second character I get 0 rows as result. I'm pretty sure this is just some c++ stuff that I don't understand that causes all this problems ...
I figured the problem out.
Replacing
sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;
sres.res.resContent.ulPropTag = PR_SUBJECT;
SvcProps.ulPropTag = PR_SUBJECT;
with
sres.res.resContent.ulFuzzyLevel = FL_SUBSTRING;
sres.res.resContent.ulPropTag = PR_SUBJECT_A;
SvcProps.ulPropTag = PR_SUBJECT_A;
fixed the problem.

How do I read binary C++ protobuf data using Python protobuf?

The Python version of Google protobuf gives us only:
SerializeAsString()
Where as the C++ version gives us both:
SerializeToArray(...)
SerializeAsString()
We're writing to our C++ file in binary format, and we'd like to keep it this way. That said, is there a way of reading the binary data into Python and parsing it as if it were a string?
Is this the correct way of doing it?
binary = get_binary_data()
binary_size = get_binary_size()
string = None
for i in range(len(binary_size)):
string += i
message = new MyMessage()
message.ParseFromString(string)
Update:
Here's a new example, and a problem:
message_length = 512
file = open('foobars.bin', 'rb')
eof = False
while not eof:
data = file.read(message_length)
eof = not data
if not eof:
foo_bar = FooBar()
foo_bar.ParseFromString(data)
When we get to the foo_bar.ParseFromString(data) line, I get this error:
Exception Type: DecodeError
Exception Value: Too many bytes when decoding varint.
Update 2:
It turns out, that the padding on the binary data was throwing protobuf off; too many bytes were being sent in, as the message suggests (in this case it was referring to the padding).
This padding comes from using the C++ protobuf function, SerializeToArray on a fixed-length buffer. To eliminate this, I have used this temproary code:
message_length = 512
file = open('foobars.bin', 'rb')
eof = False
while not eof:
data = file.read(message_length)
eof = not data
string = ''
for i in range(0, len(data)):
byte = data[i]
if byte != '\xcc': # yuck!
string += data[i]
if not eof:
foo_bar = FooBar()
foo_bar.ParseFromString(string)
There is a design flaw here I think. I will re-implement my C++ code so that it writes variable length arrays to the binary file. As advised by the protobuf documentation, I will prefix each message with it's binary size so that I know how much to read when I'm opening the file with Python.
I'm not an expert with Python, but you can pass the result of a file.read() operation into message.ParseFromString(...) without having to build a new string type or anything.
Python strings can contain any character, i.e. they are capable of holding "binary" data directly. There should be no need to convert from string to "binary".