Serial Port: Different answers from device - c#

I have a little problem with translating my code. I'm trying to make some relays (set on a custom made PCB) to turn on and off while I press a button.
The problem is I made the code that's doing it (so it is turning on and off depending on my actions) but I also need to read the answer from the machine. The point is I had an older version of the code written in C#, and when sending the code through that program, I get the OK answer, while using the new version of the code written in C++, I get only the first letter of the answer.
I was following this using a serial port monitor to see what the machine sends back, and I had the same result (is not like my code only reads one letter). So, the point is: the C# code is getting the full answer ("AWOK11") while the C++ code gets only the first letter of it ("A").
C# Code done in SharpDevelop and C++ code done in Qt Creator.
C++ code:
QSerialPort portverf;
portverf.setBaudRate(QSerialPort::Baud9600);
portverf.setDataBits(QSerialPort::Data8);
portverf.setParity(QSerialPort::NoParity);
portverf.setStopBits(QSerialPort::OneStop);
portverf.setFlowControl(QSerialPort::NoFlowControl);
portverf.setPortName("COM41");
portverf.open();
port.write(command);
QString result = portverf.readAll();
Thanks in advance for helping!

The method QIODevice::readAll() reads all available data from the device. If there was only an A available, it will return an A. It won't wait for additional characters. Since you read right after writing, reading only the first character is to be expected.

Related

Unable to load a COM visible dll within a PHP based webpage as a COM object

Here's what I've done so far:
I wrote a little C# class library to run GET requests to a REST API.
I compiled it, made sure that it worked within another c# application before trying to import this thing into my webpage. Pulled what I wanted to, worked like a charm whilst operating within another c# console app.
I followed this guide to make sure my dll was COM interoperable and reachable:
http://timheuer.com/blog/archive/2007/03/10/14009.aspx
Now, I was given this PHP webpage by someone else, and while I am not an expert in PHP, from what I've gathered it doesn't take much to be.
The only real important stuff is here:
function APIGet($url) {
$object = new COM("API_Call.ApiCaller");
$result = $object->cURLGET($token, $url);
return $result;
}
I wrote that into my function file, which has many other functions that work just fine on the page. The dll contains the namespace API_Call and a public static class, ApiCaller, as well as some other classes that aren't used here.
So then I go to call it on the actual page itself here:
$output = APIGet($apicall);
echo $output;
From which I get this error (edited for readability):
Fatal error: Uncaught com_exception: Failed to create COM object `API_Call.ApiCall':
Invalid syntax in C:\xampp\htdocs\canvasAPI.php:32
Stack trace: #0 C:\xampp\htdocs\canvasAPI.php(32):
com->com('API_Call.ApiCal...') #1 C:\xampp\htdocs\CAForm.php(169):
APIGet('api/v1/courses') #2 {main} thrown in C:\xampp\htdocs\canvasAPI.php on line 32
I have my file, canvasAPI.php with all my functions in it and then the CAForm.php file is the form itself. I've searched high and low for an answer here, but everything either says make the class library COM visible and interoperable, or to enable [COM] com.allow_dcom in php.ini, both of which I already did.
For anyone googling this, I just ended up using C# the whole way. I didn't see a point in continuing down the path of messing with custom COM objects in PHP.

Opening foreign file extensions, like in game files, How is it done?

In the sake of this game I'm modding at the moment, which has archives ending in ".rpf" (which is short for 'rage engine package file')
How exactly is it that programmers, can find ways to open these types of files/archives, without having access to the rage engine?
What would one need to know in order to even begin trying to open a foreign file extension? The files can be opened thanks to the OpenIV Team who created the program necessary for opening the files, but how exactly does a developer, figure out or even know where to start when it comes to developing an application that is to fulfill the task of opening another file?
It's called Reverse engineering
You look into file with hex editor, notice some texts, or numbers that look like offsets. You apply your own experience writing similar stuff, make some assumptions and check if it is correct for multiple entries, and so on and so forth.
There many ways to open such files.
First you can use specific programs to open them like OpenIV for RPF archive.
If you would try to Mod or write a cheat for example, most people disassemble the program or open them with a Hex-Editor.
Programs like HexEdit change the binary values of a program into hexadecimal numbers, for example the byte 10100101 into Hex 0xA5h (A5).
Another way is to disassemble the program. Programs like ollyDbg, IDA Pro or other disassemble the binary values into assembly-code. Now you're able to search for some offsets, adresses and texts and you can remove or edit them to let the program do what you want.
Some programs are able to generate a pseudocode to C or C# e.g. (.NET Reflector) which helps you to understand what the program do.
After you read for example some memory adresses and their offsets, you can change them in the disassemble program itself (JNZ (Jump if not Zero) to JMP (Jump) for example to jump directly in every case) and write these code on the executable or you can implement them in a own program which changes them or patch them.
That is the princip you looking up for to understand how the program does work and then you add some features of your own or write a complete new application to fulfill the task of opening any file. Like Vlad said thats simply called reverse engineering.

.NET 4.0 Automating/Scripting certain actions

I am a junior at university and quite new to the .NET framework.
Currently at my work in IT, there is a certain process in which an employee checks a MS office file, opens x applications, one after the other, and copies y files and waits on z conditions, one by one.
This process is quite long and tedious and very prone to human error. As such, I was wondering if .NET allows for some application to script this given sample procedure:
open a program,
input a string argument from an excel file,
get the output of the program,
paste the output of the program into another program,
get the output of the 2nd program,
open the output as a folder,
etc
The user should do as little work as possible (supplying some file paths and log-in credentials once and pressing some Start button).
If so, if someone could recommend a few good libraries/API to look at, it would be much appreciated.
Thank you for your time.
edit 1: System.Diagnostic.Process seems to not handle argument passing very well
Try using Powershell, it is much better suited to what you are trying to do. Good place to start:
http://technet.microsoft.com/en-us/library/ee221102
I think you could do that more easily with AutoIt.
.NET/c# isn't really suited for that task as others have already pointed out.

PHP class to C# class?

I work for a company that makes application's in C#.
recently we got a customer asking us to look in to rebuilding an application written in PHP.
This application receives GPS data from car mounted boxes and processes that into workable information.
The manufacturer for the GPS device has a PHP class that parses the received information and extracts coordinates. We were looking in to rewriting the PHP class to a C# class so we can use it and adapt it. And here it comes, on the manufacturers website there is a singel line of text that got my skin krawling:
"The encoding format and contents of the transmitted data are subject to constant changes.
This is caused by implementations of additional features by new module firmware versions which makes it virtually impossible to document it and for you to properly decode it yourself."
So i am now looking for a option to use the "constantly changing" PHP class and access it in C#. Some thing link a shell only exposing some function's i need. Except i have no idea how i can do this. Can any one help me find a solution for this.
I know it's a really hacky solution, but if you need a bit of PHP code that you don't want to have to repeatedly port to C# each time, you could try the following approach, although it means that you would need the php command line tool on the target machine.
First step is to have a php script that continously reads data off stdin, decodes it using this special class from the vendor, and writes the result out to stdout. Really simple example:
<?php
include("VendorDecodingClass.php");
while(true)
{
$input = fgets(STDIN); //read off of the stdin stream
//can't remember if this is valid, but somehow check that there is some data
if($input)
{
//pass it off to the vendor decoding class
$output = VendorDecoding::decode($input);
fwrite(STDOUT, $output); //write the results back out
}
//sleep here so you don't suck up CPU like crazy
//(1 second may be a bit long tho, may want usleep)
//Edit: From Tom Haigh, fgets will block, so the sleep isn't necessary
//sleep(1);
}
?>
Anyway, once you have that in place, in your C# application, right at the start, create a new Process to run that script and then save the Process instance somewhere, so you can reference the STDIN and STDOUT at a later point. Example:
ProcessStartInfo procStartInfo = new ProcessStartInfo("php", "yourscript.php");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process(); //store this variable somewhere
proc.StartInfo = procStartInfo;
proc.Start();
Then, when you want to decode your data, you just write to the stdin of the php process you created, and wait for a response on the stdout. Using the stdin/stdout approach is a lot more efficient than creating a new process each time you want to decode some data, because the overhead of creating that process can be noticeable.
proc.StandardInput.WriteLine(somedata); //somedata is whatever you want to decode
//may need to wait here, or perhaps catch an exception on the next line?
String result = proc.StandardOutput.ReadLine();
//now result should contain the result of the decoding process
Disclaimer here, I haven't tested any of this code, but that is the general gist of how I might do it.
Something else I just thought of, you will want some mechanism for terminating that PHP process. It may be OK to use Process.Kill, but if the decoding does any file IO, or anything critical you may want to send an interrupt signal to the php script somehow.
I assume the php script is on your machine and returns usefull data. The first -not very elegant solution- that pops into my mind is the following:
Make sure your machine has the php commandline installed, so that you are able to run the php script from commandline. To execute a commandlinetool from C# see code for that here. The returned data now probably needs to get processed my your C# program.
I have never tried this and do not know anyone that has, but I remember comming across this sometime ago and thought I would throw it out there as a possible option for you.
Phalanger is a compiler project that compiles PHP code to IL, so you can use that then have a managed assembly that you reference from your code directly.
If the format is a regex you can try to put it in an application setting file (not resources, these are compiled WITH the application, you can't change them without recompiling the app).
Application settings are not changeable by the user but you can do that by editing the XML.
Or you can set the settings to user mode and then you can change the format from inside your application code.
Why don't you just launch the PHP script from C#, have it output its results to a file and then use that file as input for your C# program?
Personally, I would setup a PHP web service with a proper and stable API that the C# project can access, implement the manufacturers supplied PHP class in the web service and let it be.

debug matlab function called from c#

I call a *.m function from C# :
matlab.Feval("shoulderDetector", 3, out result, pic, colorPicR, colorPicG, colorPicB);
When I compile, this line opens the Command Window for matlab and sends the 4 variables(All variables are from live camera) to shoulderDetector.m that calls another 3 matlab funcitions... There is an error there "matrix exceeds it indices..." Is there a way knowing what's going on with data in matlab without printing everything in the CMD matlab window ?
If not, what is the best way detecting a bug ?
Unless you want to debug in software designed for debugging Matlab code, there is no way to do it. I would put debugging output to the console in the Matlab code where you think you can catch the issue, (e.g. near the problematic matrix) and output the indexes and any other information that might be helpful.
Unfortunately there is no good solution debugging solution for Matlab when you come across different programming languages.
I reccomend to replace your function temporarily with this one:
function varargout=genericSaveArgs(varargin)
varargout=cell(nargout,1);
callArgs=varargin;
save('callArgs.mat','callArgs');
end
which saves the parameters, then call your original function using:
cc=load('callArgs.mat')
[a,b,c]=shoulderDetector(cc.callArgs{:})

Categories