I am trying to use the following to send over an RS232 connected projector to turn it on:
commProj.Parity = "None";
commProj.StopBits = "One";
commProj.DataBits = "8";
commProj.BaudRate = "19200";
commProj.PortName = "COM6";
commProj.CurrentTransmissionType = PCComm.CommunicationManager.TransmissionType.Text; //.Hex
commProj.OpenPort();
commProj.WriteData((char)33 + (char)137 + (char)1 + (char)80 + (char)87 + (char)49 + "\n"); //turn on proj
Problem being is that it doesnt work.
I have done this with a VB6 port and it works just fine:
public static PCComm.CommunicationManager commProj = new PCComm.CommunicationManager();
MSCommProj.CommPort = 6
MSCommProj.Settings = "19200,N,8,1"
MSCommProj.PortOpen = True
MSCommProj.Output = Chr(33) & Chr(137) & Chr(1) & Chr(80) & Chr(87) & Chr(49) & Chr(10)
What am i missing?
David
CommunicationManager.cs: http://snipt.org/xmklh
Okay, the manual helps a lot. Try changing the CurrentTransmissionType to TransmissionType.Hex and sending the string 21890100000a
commProj.CurrentTransmissionType = TransmissionType.Hex;
commProj.WriteData("21890100000a");
EDIT
Sorry, that was "connection check". Use 2189015057310a for on and 2189015057300a for off.
The plus(+) operator for char's doesn't concatenate the values it adds them. So you end up passing "387\n" to write data.
You need to create a char array and then convert that to a string instead:
commProj.WriteData(new string(new char[] { (char)33, (char)37, (char)1, (char)80, (char)87, (char)49, '\n' }));
I don't know what kind of object commProj is (specifically) but my guess is that the problem comes from casting each numeric value to a char. A char is 2 bytes in size. I recommend either trying to write a Byte array with your data in it, or concatenating a string with these chars and then converting the string to ascii text.
Related
I have a VB6 desktop app and iOS and Android smartphone apps. When someone enters a name such as O'Hara in the iOS app it comes through to the VB6 desktop app as unreadable characters. I assume this is a UTF8 to ASCII conversion issue? I would like to intercept this data server side and change the ' and " to the proper character that will read properly in VB6. How should I do this in C# .NET?
You can replace the problematic characters with a version that fits in the character set used by VB 6:
string value = "The problem string";
value = value.Replace("\u2022", "-");
value = value.Replace("\u2013", "-");
value = value.Replace("\u2014", "-");
value = value.Replace("\u2015", "-");
value = value.Replace("\u2017", "_");
value = value.Replace("\u2018", "'");
value = value.Replace("\u2019", "'");
value = value.Replace("\u201a", ",");
value = value.Replace("\u201b", "'");
value = value.Replace("\u201c", "\"");
value = value.Replace("\u201d", "\"");
value = value.Replace("\u201e", "\"");
value = value.Replace("\u2026", "...");
value = value.Replace("\u2032", "'");
value = value.Replace("\u2033", "\"");
This is not an exhaustive list of all Unicode characters, of course. You may end up finding you need to replace more, or alarm on them in some way.
There is a code part in my program. Lets say buf.Substring(0, 4) is a string which is 326 for that moment in the loop. String buf.Substring(0, 4) is updated in a for loop.
if (buf.Substring(0, 4).Equals("GG:"))
{
label22.Text = buf.Substring(4) + "Z" ;
}
This outputs on label22 as:
326
Z
If you replace it as
label22.Text = "Z" + buf.Substring(4);
then it concatenates properly as:
Z326
But I want the output to be:
326Z
Whatever I tried it dodn't work. I tried concatenating different ways or sizing label width very long ect. What could be the problem here?
You could try trimming the output of buf.Substring(4) like this
String output = buf.Substring(4).Trim(new char[] {'\r','\n'});
Or just plain Trim() like so
String output = buf.Substring(4).Trim();
So I have a program that does a for loop, reading every character one by one and replacing it with a four digit number correlating to that particular letter using a case statement.
My problem is it is not reading NewLine characters ('\n') and I don't know how to fix this.
Here is my code:
for (int i = 0; i < inputTextBox.Text.Length; i++)
{
//Encryption
switch (inputTextBox.Text[i])
{
// I got rid of the rest of the cases
// as they are not relevant
case '\n':
encryptedString = encryptedString + "8024";
break;
}
}
and since it does not accept the new line as a character, it doesn't add it to the encryptedString.
This might seem like a duplicate question but the other posts that I found were actually in completely different situations.
EDIT ----------------------------------------------------------------------------------------------------------------------------
So after debugging, it turns out it is actually reading the '\n' it is just not writing it to the string when decoding it.
here's the code for the decoding section:
for (int i = 0; i < readString.Length; i = i + 4)
{
//Decryption
switch (readString.Substring(i, 4))
{
case "8024":
decryptedString = decryptedString + "\n";
break;
}
}
inputTextBox.Text = decryptedString;
So it is reaching the "decryptedString = decryptedString + "\n";" line it is just not adding a new line to the string for some reason. I have also tried '\n' instead of "\n" just to be sure.
I replaced the "\n" when decoding the newlines with a "System.Environment.NewLine" and it fixed the problem.
Try first replace new lines with this Regex.Replace(inputTextBox.Text, "\r\\D\n?|\n\\D\r?", "\n");
EDIT: Are you sure it didn't worked?
For examlpe if encryptedstring is:
First string
second
3rd
fourth
Using this:
encryptedString = Regex.Replace(encryptedString, "\r\\D\n?|\n\\D\r?", "\n");
encryptedString = encryptedString.Replace("\n", "8024");
Will make encryptedString = First string8024second80243rd8024fourth is that what you wanted?
In C# I have a string that goes on to be inserted into a db table using codepage 37 US. So for instance, the '€' will cause the insert operation to fail.
What is a good way to clean my string of characters not represented in code page 37 and possible replace those charaters with some default character?
Something like this?
var euroString = "abc?€./*";
var encoding37 = System.Text.Encoding.GetEncoding(
37,
new EncoderReplacementFallback("_"), //replacement char
new DecoderExceptionFallback());
var byteArrayWithFallbackChars = encoding37.GetBytes(euroString);
var utfStringFromBytesWithFallback = new string(encoding37.GetChars(byteArrayWithFallbackChars));
//returns "abc?_./*"
P.S.: you can just use GetEncoding(37), but in this case replacement char is ? which I think is not really OK for DB :)
Here is a regex to restrict input to a range of allowed characters:
https://dotnetfiddle.net/WIrSSO
const string Allowed = #"1-9\."; //Add allowed chars here
string cleanStr = Regex.Replace("£1.11", "[^" + Allowed + "]", "");
I have one python script which i am trying to convert and stuck in one place and unable to proceed. Please check where ever i have mentioned "Stuck here" in below code. any help would be appreciated
Original Python script:
import hashlib
meid = raw_input("Enter an MEID: ").upper()
s = hashlib.sha1(meid.decode('hex'))
#decode the hex MEID (convert it to binary!)
pesn = "80" + s.hexdigest()[-6:].upper()
#put the last 6 digits of the hash after 80
print "pESN: " + pesn
My C# conversion:
UInt64 EsnDec = 2161133276;
string EsnHex=string.Format("{0:x}", EsnDec);
string m = Convert.ToString(Convert.ToUInt32(EsnHex, 16), 2);
/*---------------------------------------------
Stuck here. Now m got complete binary data
and i need to take last 6 digits as per python
script and prefix "80".
---------------------------------------------*/
Console.WriteLine(m);
Console.Read();
Use String.Substring:
// last 6 characters
string lastsix = m.Substring(m.Length - 6);
Console.WriteLine("80{0}", lastsix);
How about something like this:
static void Main(string[] args)
{
UInt64 EsnDec = 2161133276;
Console.WriteLine(EsnDec);
//Convert to String
string Esn = EsnDec.ToString();
Esn = "80" + Esn.Substring(Esn.Length - 6);
//Convert back to UInt64
EsnDec = Convert.ToUInt64(Esn);
Console.WriteLine(EsnDec);
Console.ReadKey();
}