|
hi,
i have a read write communication form one port of my pc to another working fine, but i can't send spacial characters like é#§èçà i get a ? on the received side.
does anybody know what i should add to my project?
thx
|
|
|
|
|
Hello
If you are using byte arrays to comunicate with your post, then it should work fine, and the interpretaion is the responsibility of the recipient.
If you are using strings to transfer data to/from your port, then check the Encoding of your string.
One last note... Are you sing .Net 2.0 framework?
Regards
|
|
|
|
|
yes i am and i use textbox.text = port.readline() function
so were should i check the encoding?
thx
|
|
|
|
|
Hello
Try this conversion. It's not the best practice but fever got my mind blind. I can't really think of something better at the moment.
textbox.Text = Encoding.UTF32.GetString(Encoding.UTF32.GetBytes(port.ReadLine()));
If it doesn't work, try using other encodings provided in the Encoding class.
Regards
|
|
|
|
|
to bad non of the encodings work, perhaps i should encode the sended text?
but i'm not sure how i convert the txtbox string to a byte[] or char[]
so i can use: port.writeline(encoding.utf32.getbyte(textbox.text))
thx anyway
|
|
|
|
|
Hello
To send encoded bytes array...
byte[] MyBuffer = Encoding.UTF8.GetBytes(textBox1.Text);
MyPort.Write(MyBuffer, 0, MyBuffer.Length);
...and reverse the process on the other side.
Regards
|
|
|
|
|
lo, thx for the tip,
but i dont know how to reverse it :s
|
|
|
|
|
no need to bother i found the solution :
public Form1()
{
InitializeComponent();
port.Encoding = System.Text.Encoding.UTF8;
}
and it works!
thx for all the help :p now i can start with databases
grz
|
|
|
|
|
I would just like to run my model past the forum and get some comments, hopefully even start a discussion. Convention tells me that I should not catch exceptions unless absolutely necessary, to recover from something, or to add more information. However, I don't like to have too much diversity and permutations of try-catch blocks etc.
An example is a recent project, with a UI and a class library. The class library exposes two methods used by the UI, and a custom exception class (UpdateException). The library performs database updates that must be rolled back if any is not successfull. Deep inside my classes, I only catch one exception, which masks a real cause of failure, and rethrow adding a description. In the actual update public method though, I catch all exceptions because any exception should cause a rollback. I then throw my UpdateException for the client to catch.
In this way, my client can maintain normal operation after an UpdateException, maybe even try the update again, but respond differently, if at all, to all other exceptions.
|
|
|
|
|
You might want to look at the Enterprise Library Exception Handling Block. Using this will provide consistent handling and give you the ability to easily configure and log exceptions, without changing your code.
only two letters away from being an asset
|
|
|
|
|
I have an am thanks, but I was thinking in a more general manner. I consider some applications a bit small for all that, but then maybe I should put so much thought into their error handling, no?
|
|
|
|
|
How large must an application be before defensive, secure programming techniques are utilized?
only two letters away from being an asset
|
|
|
|
|
Ouch, you are right. Maybe I shouldn't have added the last bit; it was late and I was tired.
|
|
|
|
|
Hello,
how can I convert int[] array to object[] array? I have written a method that compares two arrays and takes (object[] FirstArray, object[] SecondArray) as input arguments.
If I pass string[] arrays as arguments, it works fine, but if I pass int[] array, compiler reports an error "Cannot convert type 'int[]' to 'object[]'".
Example code:
string[] StringArray;
int[] IntArray;
object[] ObjectArray;
ObjectArray = (object[])StringArray;
ObjectArray = (object[])IntArray;
I thought that by converting from int[] to object[] I am doing a standard boxing operation. How can I resolve this?
Thanks much,
Michal
|
|
|
|
|
object[] ObjectArray = new object[IntArray.Length];
IntArray.CopyTo(ObjectArray, 0);
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Thanks, but this doesn't solve the problem of a method that takes object[] as a universal argument. Why string[] is compatible to object[] while int[] is not? It's not too much elegant to always convert int[] to object[] by using CopyTo before passing the parameters to the method. There must be some more conceptual way.
Thanks,
Michal
|
|
|
|
|
michal.kreslik wrote: Why string[] is compatible to object[] while int[] is not?
string and object are reference types (defined as class ) whereas int is a value type (defined as struct ). Obviously it's not possible to simply cast an array of value types to an array of reference types. I guess that's due to the necessary boxing of array items.
I agree that the use of CopyTo isn't the most elegant solution but I think there's no other way when dealing with value type arrays.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Actually, after some fiddling, I've found a way how to do it.
You don't need to "cast" the type if you use a generic method:
class Program
{
static void MyMethodWithUnknownInputType<T>(T[] Argument)
{
Console.WriteLine("Current input type is: {0}", Argument.GetType().ToString());
Console.WriteLine("Array contents:");
foreach (T Item in Argument)
{
Console.Write("{0} ", Item.ToString());
}
Console.WriteLine("\n");
}
static void Main()
{
string[] MyStringArray = { "String1", "String2", "String3", "String4", "String5" };
int[] MyIntArray = { 2, 4, 8, 16, 32 };
MyMethodWithUnknownInputType(MyStringArray);
MyMethodWithUnknownInputType(MyIntArray);
}
}
The output of this program:
Current input type is: System.String[]
Array contents:
String1 String2 String3 String4 String5
Current input type is: System.Int32[]
Array contents:
2 4 8 16 32
Michal
-- modified at 18:36 Wednesday 13th September, 2006
|
|
|
|
|
How to implement a drawer control in C#?like the VS Toolbox?and the auto-docking control,like the solution explorer?Can those be made manully?
|
|
|
|
|
Actually I also wanted to know that ....please anyone help us both....
<marquee>nishu
|
|
|
|
|
Hi,
I am writing a application where I need my form to be at fixed location and of fixed side. This means it should not be possible for user to move the from one location to another. How can I do this?
Regards,
Hemant.
|
|
|
|
|
Fixed size : Set property FormBorderStyle to Fixed... (Single, 3D, etc.)
Fixed Location: Use event handler "Move", in this event handler just set your location, that means every time the users move this fenster, it will be reset to it start location.
|
|
|
|
|
Hi,
I am trying to access unmanaged memory which allocated from a dll function.
The function signature that is exported is as follows
extern "C" __declspec( dllexport ) BOOL ResizeImage(LPVOID pbySrcBytes,<br />
LONG lnSrcSize,<br />
LPVOID pbyDestBytes,<br />
LPLONG lnTgtSize<br />
);
Through the parameter "pbySrcBytes" I pass memory to the unmanaged code using marshalling.
In return the unmanaged function returns memory through "pbyDestBytes" on unmanaged heap which is having dynamic size which I get through "lnTgtImgSize".
I am not able to copy the memory returned. I am getting an error as "Access Violation" & "the memory is corrupted".
I have imported the function as follows.
[DllImport("ImgProcessor.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]<br />
public static extern Int32 ResizeImage([Out] byte[] pbySrcImgBytes,<br />
Int32 lnSrcImgSize,<br />
IntPtr ppbyDestImgBytes,<br />
[Out] IntPtr lnTgtImgSize<br />
);
I have done the C# part as follows:-
byte []byInputArray = File.ReadAllBytes(txtInput.Text);
Int32 nTgtImgSize = new Int32();<br />
IntPtr ptrTargetImageBytes = Marshal.AllocHGlobal(4);<br />
IntPtr ptrTgtImgSize = Marshal.AllocHGlobal(4);<br />
IntPtr ptrSourceImageBytes = Marshal.AllocHGlobal(byInputArray.Length);<br />
Marshal.StructureToPtr(byInputArray[byInputArray.Length-1], ptrSourceImageBytes, false);<br />
ImageResizerClient.ResizeImage(byInputArray, (int)byInputArray.Length, 400, 300, ptrTargetImageBytes, ptrTgtImgSize);<br />
nTgtImgSize = Marshal.ReadInt32(ptrTgtImgSize);<br />
byte []byOutputArray = new byte[nTgtImgSize];<br />
Marshal.Copy(ptrTargetImageBytes, byOutputArray, 0, nTgtImgSize);
The Marshal.Copy method call is throwing the error.
Can anyone help me understand why this is happening?
Thank You,
Amit.
-- modified at 23:59 Monday 11th September, 2006
|
|
|
|
|
Hi,
I am writing a application where I need my form to be at fixed location and of fixed side. This means it should not be possible for user to move the from one location to another. How can I do this?
Regards,
Hemant.
|
|
|
|
|
Use FormBorderStyle property of form ,make it fixed so that it will not allow user to change the size of the form and for moving i dont have any idea.
nishu
|
|
|
|