How to open comport more than 9 in VC++ code - tcomport

I want to open comport which is higher than 9 in VC++ code. The following code can open comport which is higher than 9. But, the result I am getting in combo box is "\.\COM10". I don't want "\.\" before COM name in combo box. Please help me to resolve this issue.
My code :
CString str;
int i;
for(i=1;i<30;i++)
{
str.Format("\\\\.\\COM%d",i);
ptrLC->comPort.CloseCommPort();
if(ptrLC->comPort.OpenCommPort(str))
{
m_cCommPort.AddString(str);
ptrLC->comPort.CloseCommPort();
}
}

Thank you for your contribution...The above issue is resolved. I did changes in above code which helps me to open comport higher than 9. And also combo box Drop Down list does not contain \.\ before COM name.
Corrected code:
CString str, str1;
int i;
for(i=1;i<30;i++)
{
str.Format("COM%d",i);
ptrLC->comPort.CloseCommPort();
str1 = "\\\\.\\" + str; // this is a I/O string format which helps to open comport higher than 9.
if(ptrLC->comPort.OpenCommPort(str1))
{
m_cCommPort.AddString(str);
ptrLC->comPort.CloseCommPort();
}
}

Related

Error C2228 left of '.size' must have class/struct/union on Winform

I'm creating my first program on Winform with C++. I have a function and a string input. However, when I run the program, I get the following error with text.size(): "Error C2228 left of '.size' must have class/struct/union"
Relevant code:
private: System::Void bntGet_Click(System::Object^ sender, System::EventArgs^ e) {
in_itext = txtText->Text;
itext = Convert::ToString(in_itext);
for (Int32 index = 1; index <= itext.size(); index++)
{
if (itext[index] == '#')
{
while (itext[index - 1] != ' ')
{
index--;
}
while (itext[index] != ' ')
{
next_email += itext[index];
index++;
if (itext[index] == '\0')
break;
}
break;
}
}
txtEmail->Text = next_email;
}
Any suggestions? Thanks everyone!
Standard warning: While it's certainly possible to write the main body of your application in C++/CLI, or even write the GUI in C++/CLI using WinForms, it is not recommended. C++/CLI is intended for interop scenarios: where C# or other .Net code needs to interface with unmanaged C++, C++/CLI can provide the translation between the two. For primary development, it is recommended to use C# with either WinForms or WPF if you want managed code, or C++ with MFC if you want unmanaged.
That said:
for (Int32 index = 1; index <= itext.size(); index++)
and
"Error C2039 'length': is not a member of 'System::String'"
Size is not how you get the length of a System::String in .Net. For that, you want the Length property. You were close on your second attempt, but you need a capital "L". (size() is how std::string does it, but std::string is a completely different beast from System::String.)
Other issues:
in_itext = txtText->Text;
itext = Convert::ToString(in_itext);
It's already a string, you don't need to convert it.
while (itext[index - 1] != ' ')
{
index--;
}
You can easily run off the beginning of the string if you do this. (You'll get an exception when index is equal to zero.) Add a check against zero.
while (itext[index] != ' ')
{
next_email += itext[index];
index++;
if (itext[index] == '\0')
break;
}
Strings in .Net are not null-terminated, you need to check against the Length.
If we take a step back, what problem are you actually trying to solve here? My best guess is that you've got some text with an email address somewhere in the middle, and you just want to extract the email address. If that's the case, then I'd probably try using a regular expression to extract the email address. You've got the full power of the .Net Library available to you, use it! See Using a regular expression to validate an email address for the regex to use, and read up on regular expressions with the introduction page and class reference.

How to convert String^ to float?

I have taken text from a textbox txt1 in Visual Studio 2012 for Windows 8, when i use txt1->Text, it returns the text in String^. How do I convert it to float? I want this app to run on Windows 8.
Looks like you have a .NET System::String^. .NET provides some conversion functions, of which my favorite is TryParse.
float value;
String^ str;
if (System::Single::TryParse(str, value)) {
/* ok, use value */
}
else {
/* problem : str isn't numeric */
}

Computer to Arduino mega via serial changing values

to start off, this might not be a problem with arduino code or arduino, but I figured I would post here because I really just can not figure out what is wrong.
I am working on this project just for fun to send key strokes from the keyboard, through the computer, and out through the USB to my arduino mega. No additional hardware is here, just the computer, the arduino, and the USB cable.
I am using Microsoft Visual Studio Express 2012 to write code to receive key strokes and send them to the USB. This is the code I am using:
#include "stdafx.h"
#include "conio.h"
using namespace System;
using namespace System::IO::Ports;
int main(array<System::String ^> ^args)
{
String^ portName;
String^ key;
int baudRate=9600;
Console::WriteLine("type in a port name and hit ENTER");
portName=Console::ReadLine();
//arduino settings
SerialPort^ arduino;
arduino = gcnew SerialPort(portName, baudRate);
//open port
try
{
arduino->Open();
while(1)
{
int k = getch();
key = k.ToString();
Console::WriteLine(key);
arduino->Write(key);
if (k == 32)
return 0;
}
}
catch (IO::IOException^ e )
{
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}
}
This code works fine, and sends commands to the arduino. I might as well ask this as well, but after 35 key strokes it just stops sending key strokes, I am unsure as to why, but that is not an arduino problem (I don't think).
So when the certain value for key gets sent to the arduino, it changes. For example, the values that are assigned to the variable key for pressing the number 1 and 2 are 49 and 50, respectively. However, when they get sent to the arduino, the values are different some how. 1 is now 57, and 2 is now 48. I am unsure as to why this is happening. I tried 4 and 5 and they both have their values shift down 2 like the key 2. This is the code I have on the arduino:
int ledPin = 13;
int key=0;
int c;
void setup()
{
pinMode(ledPin, OUTPUT); // pin will be used to for output
Serial.begin(9600); // same as in your c++ script
}
void loop()
{
if (Serial.available() > 0)
{
key = Serial.read(); // used to read incoming data
if (key == 57)
{
digitalWrite(ledPin, HIGH);
}
else if (key == 48)
{
digitalWrite(ledPin, LOW);
}
}
c = key;
Serial.println(c);
}
As of right now it is just to switch a light on and off. I am hoping to involve many more keys and having the values be consistent would be very convenient. Anyways, if anyone could help me with why the values are different that would be awesome. I am not completely new to programming but I am certainly no expert and have not gotten too far into advanced stuff.
Thank you for any help or advice.
This has to do with what you are sending through visual studio. You are converting a keypress to its ASCII value, then converting that ASCII value to string, then sending that string through serial. The arduino is expecting a number, not a string.
For example, if you press the 1 key, your visual studio code converts that to an ASCII number 49, which is then converted to string "49", which the Arduino receives - but since you are sending "49", which is a "4" and an "9", the Arduino is reading 9 which corresponds to 57, as you have seen.
Similarly, pressing 2 converts it to "50", and the Arduino reads "0" which corresponds to the value 48 which you were getting.
To fix this, send the number directly, don't convert it into a string.

Visual Studio 2012 - vshost32-clr2.exe has stopped working

I'm creating a WinForm Application in C# using Visual Studio 2012 and I'm getting an error when I debug it :
vshost32-clr2.exe has stopped working
I already searched but most results are for Visual Studio 2010 and lower and I get similar solutions which I think is not applicable to Visual Studio 2012 :
Properties -> Debug -> Enable unmanaged code debugging
Source : vshost32.exe crash when calling unmanaged DLL
Additional Details :
My project doesn't use any DLL.
As far as I progress in my project, it only occurs when the width is 17.
I use the following code :
Bitmap tmp_bitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Rectangle rect = new Rectangle(0, 0, 16, tmp_bitmap.Height);
System.Drawing.Imaging.BitmapData bmpData =
tmp_bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
tmp_bitmap.PixelFormat);
unsafe
{
// Get address of first pixel on bitmap.
byte* ptr = (byte*)bmpData.Scan0;
int bytes = Width * Height * 3; //124830 [Total Length from 190x219 24 Bit Bitmap]
int b; // Individual Byte
for (int i = 0; i < bytes; i++)
{
_ms.Position = EndOffset - i; // Change the fs' Position
b = _ms.ReadByte(); // Reads one byte from its position
*ptr = Convert.ToByte(b);
ptr++;
// fix width is odd bug.
if (Width % 4 != 0)
if ((i + 1) % (Width * 3) == 0 && (i + 1) * 3 % Width < Width - 1)
{
ptr += 2;
}
}
// Unlock the bits.
tmp_bitmap.UnlockBits(bmpData);
}
I think posting my code is necessary as it only occurs when such value is set to my method.
I hope you can help me fix this problem.
Thank you very much in advance!
Not sure if this is the same issue, but I had a very similar issue which resolved (vanished) when I un-checked "Enable the Visual Studio hosting process" under the Debug section of Project/Properties. I also enabled native code debugging.
This issue can be related with debugging application as "Any CPU" under x64 OS, set Target CPU as x86
Adding my 2 cents since I ran into this today.
In my case, a call to a printer was passing some invalid value, and it seems it send the debugger to sleep with the fishes.
If you run into this, see if you can pinpoint the line and make sure there are no funny business issues around a call out (like a printing service)
Below solution worked for me:
Go to the Project->Properties->Debug tab
Unchecked the 'Enable the Visual Studio hosting process' checkbox
Checked 'Enable native code debugging' option
Hope this helps.

Help, cannot how to run unsafe

How to run csc /unsafe *.cs
using System;
class UnsafeCode
{
unsafe static void Main()
{
int count = 99;
int* p;
p = &count;
Console.WriteLine("Initial value of count is " + *p);
*p = 10;
Console.WriteLine("New value of count is " + *p);
}
}
Error 1 Unsafe code may only appear if compiling with /unsafe C:\Documents and Settings\Eddy Ho\My Documents\Visual Studio 2010\Projects\608-UnsafeCode-Errors\608-UnsafeCode-Errors\Program.cs 5 26 608-UnsafeCode
It's a command line argument.
You use the Visual Studio command prompt and simply type it out.
You can also set this in the IDE, by going to the properties of the project, and in the Build tab select Allow unsafe code checkbox. See here.

Resources