Hello,pls can someone help me with a code or giudeline on this project am currently working on. my project is on reading data from a microcontroller (89s52) via serialport to Eight textboxes on a pc GUI using C#,am using Visual studio 2008.Am having problems on how to display the ASCII characters e.g(55,23,12,34,42,33,22,23,N 55,23,12,34,42,33,22,23,N,55,etc) also to store the received data on an access database
19 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sorry, I missed your question -
it was not on top on my page account.
Your error - thrived by DB engine, and, probably it because error in query
string.
Check the string yourself – set
breakpoint before exact query, copy value of string to query generator (I do
not know the MS Access – absolutely, but sure, must be something) and try to
run it.
Error will be shown – syntaxes,
column name or something else.
So, the code should be
corrected, it must build correct query string.
Hello Danatas,
its been a long time,i have a problem regarding this my project, is there any way i can display those data on a graph like appearance,histogram or pie chart??
sorry for bothering you again.
Dear Danatas Gervi,
Would you tell me please how to display incoming data from serialport in 3 textboxes?
I have simple serial terminal software in c#. Its possible to receive rx data via sertial port.
I see rx data in console view like hyperterminal in win xp.
this is incoming data string:
1. X:1023, Y:0, Z:1023
2. X:1023, Y:0, Z:1023
3. X:1023, Y:0, Z:1023
4. X:1023, Y:0, Z:1023
5. X:1023, Y:0, Z:1023
6. X:1023, Y:0, Z:1023
7. X:1023, Y:0, Z:1023
8. X:1023, Y:0, Z:1023
...
I have 3 textboxes or labels that should display each Value and update everytime value change.
I upload a picture of my mainform as referenz:
http://img402.imageshack.us/img402/377/readdata.png
Reagrds
Toby
Hello Danatas,
i hv tried ur recent codes for storing data on a Database but when i run the program i usually have have errors which states like this
ArgumentException was Unhandled
format of the initialisation string does not conform to specification starting at index 0.
pls do you have any idea where d problem might be from.
Thanks for your Anticipated response.
Hello Danatas i forgot one point on the last question,
the data should be collected peridically say every 2-3 minutes in other to minimize database memory usage in terms of storage.Also if there is any point/feature u may add to this my projects,pls do,i want it to be a standard device.(laughs).
This is example for MS-SQL client, but for every other will
be same prinsip.
I have written the simplest example of dynamic query building
– without protection and optimisations (that can be implemented using sql
params and exeption catching) and so on…
Also – you should change the table name and colums names in
sSql string – I do not know exact names in your DB.
Hello Danatas,
pls am sorry to disturb you so soon.
am calling on you for one more problem am having now,pls hw can i be retrieving and storing these data on the 8 textboxes to an Access database using SQL or any.
Danatas Gervi
YOUR DA BOMB!!!!!.
the code is working perfectely,Men i am very happy for your Help.THANKS A MILLION!!!!!!!!!!!!!!.
pls i like to be communicating with,i u to be my MENTOR in programming.here is my address [email protected].
Am into embedded programing but i want to be intergrating them to my PC,am an electronic Enthuasist.
once again THANKS BROTHER.can i be free to ask you any Question in future?
Danatas Gervi i must say that am very happy with the attention ur givin me,i dnt knw how to say thank you. u asked wat i will do with the other stream of data.okay let me give you more info about my project. i will be measuring 8 temperature levels via 8 sensors(8 #Lm35) which is converted to digital signal using 8-channels ADC0808 chip(Analogue To Digital Converter),then fed to the microcontroller (89S52) that now does the conversion to Decimals(ASCii).The micocontroller now transmits the converted data to the serial port thereby displaying those values on 8 textboxes,it should also be updating those values on the textbox based on what the temperature level from the sensors. (Realtime) i used hyperterminal to view the incoming data from the 89S52 which shows these stream of datas continuosly (i.e N44,34,12,54,32,11,23,43,N34,51,50,40,30,22,31,00,N05,02,03,07,09,08,18,16,N19.(the N signifies start of new reading)) the 89S52 continously supplies data about the cuurent temperature value to the serialport(GUI) so that it will continously be updating those textboxes. The textboxes are updated (Realtime)as long as there is incoming data on the serial port. These data which are displayed is also used to populate an Access Database for reference or storage purposes.
thank you very much for the code but it gives me "Error in received data" when i plug my device.but if run it without it displays fine,i dnt know where the problem is.the data from the microcontroller comes like this N22,11,23,34,54,44,22,13,N55,34,22etc the N shows the begining of a new reading.i tryed increasing my baudrate but is nt working.
unsigned char sensor=0,reading; P1=0; initSerial(); usDelay(50); while(1) { if(sensor==8) sensor=0; if(sensor==0) sendChar('N'); P1=(P1&0xf8)+sensor; latch(); startConv(); wait(); get(); reading=P2; sendReading(reading); sensor++; } }
[/code]
my problem is,afta creating an instance of serial port,uner the DataReceived event Handler,i could receive the data from the 89s52 ASCII e.g N55,43,23,12,32,22,12,N32,41,25,51,etc.so i want each of the 8 data to be displayed on 8 textboxes intead of one. my GUI design has 8 textboxes to display these data
Danatas GerviPosted Sep 16, 2009, 9:01 AM
Sorry, I missed your question - it was not on top on my page account.
Your error - thrived by DB engine, and, probably it because error in query string.
Check the string yourself – set breakpoint before exact query, copy value of string to query generator (I do not know the MS Access – absolutely, but sure, must be something) and try to run it.
Error will be shown – syntaxes, column name or something else.
So, the code should be corrected, it must build correct query string.
Lu SunPosted Mar 5, 2014, 3:49 AM
I try the codes in the similar situation, but it is unstable,
I remove this line:
//textData = "55,43,23,12,32,22,12,44,"; // example string - i cannot get anything from my port...
sometimes it work, sometime I got an error message
the error message is about "textData" in this function:
private void SetTextToTextboxes(string textData)
it said that the textData was null,
how to solve this problem?
Thank you very much!
Mahesh ChandPosted Aug 27, 2010, 9:50 PM
Dave ChikaPosted Oct 21, 2009, 2:47 PM
its been a long time,i have a problem regarding this my project, is there any way i can display those data on a graph like appearance,histogram or pie chart??
sorry for bothering you again.
TobyPosted Oct 6, 2009, 3:58 PM
Dave ChikaPosted Sep 11, 2009, 3:30 PM
i hv tried ur recent codes for storing data on a Database but when i run the program i usually have have errors which states like this
ArgumentException was Unhandled
format of the initialisation string does not conform to specification starting at index 0.
pls do you have any idea where d problem might be from.
Thanks for your Anticipated response.
Danatas GerviPosted Sep 9, 2009, 1:34 AM
Use something like this:
if (separatedData.Length == 8)
{
this.Text = "Data Ok";
if (TimeToSaveData())// timeout checking
{
SaveTemperatureToDB(separatedData);
m_LastSaveTime = DateTime.Now;
}
And function:
DateTime m_LastSaveTime;
private bool TimeToSaveData()
{
return m_LastSaveTime.AddMinutes(3) < DateTime.Now;
}
Dave ChikaPosted Sep 8, 2009, 3:03 PM
the data should be collected peridically say every 2-3 minutes in other to minimize database memory usage in terms of storage.Also if there is any point/feature u may add to this my projects,pls do,i want it to be a standard device.(laughs).
Danatas GerviPosted Sep 8, 2009, 6:17 AM
Sure you can!
I can guess that you want to store all data in DB – for future statistic or something…
:)
If so, you can do it in same delegate – that inserts data to check boxes.
The function for this should be like:
if (separatedData.Length == 8)
{
this.Text = "Data Ok";
SaveTemperatureToDB(separatedData);// function call
…….
private void SaveTemperatureToDB(string[] separatedData)
{
SqlConnection dbConnect = new SqlConnection("conn string");
dbConnect.Open();
string sSql = "INSERT INTO Temeratures ([col_temp1] , [col_temp2] ,[col_temp3] ) VALUES (";
for (int iii = 0; iii < separatedData.Length; iii++)
{
if (iii != separatedData.Length - 1) // not last element
{
sSql += separatedData[iii] + ",";
}
else // last element
{
sSql += separatedData[iii] + ")";
}
}
SqlCommand sqlCommand = new SqlCommand(sSql,dbConnect);
sqlCommand.ExecuteNonQuery();
dbConnect.Close();
}
This is example for MS-SQL client, but for every other will be same prinsip.
I have written the simplest example of dynamic query building – without protection and optimisations (that can be implemented using sql params and exeption catching) and so on…
Also – you should change the table name and colums names in sSql string – I do not know exact names in your DB.
Dave ChikaPosted Sep 7, 2009, 3:38 PM
pls am sorry to disturb you so soon.
am calling on you for one more problem am having now,pls hw can i be retrieving and storing these data on the 8 textboxes to an Access database using SQL or any.
Danatas GerviPosted Sep 6, 2009, 1:25 AM
I will be glad to help you.
:-)
Dave ChikaPosted Sep 5, 2009, 3:29 PM
YOUR DA BOMB!!!!!.
the code is working perfectely,Men i am very happy for your Help.THANKS A MILLION!!!!!!!!!!!!!!.
pls i like to be communicating with,i u to be my MENTOR in programming.here is my address [email protected].
Am into embedded programing but i want to be intergrating them to my PC,am an electronic Enthuasist.
once again THANKS BROTHER.can i be free to ask you any Question in future?
Danatas GerviPosted Sep 3, 2009, 12:23 PM
Aha, now it is more clear…..
So, try this code :
string m_serialData; // data needs to besaved beetween events
bool m_readEnd = false;
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
char dataChar = ' ';
while (serialPort1.BytesToRead > 0)
{
dataChar = (char)serialPort1.ReadChar();
if (dataChar != 'N')// reading only beetween 'N' ..... 'N'
{
m_serialData += dataChar; // collecting the data
}
else
{
m_readEnd = true;
break;
}
}
if (m_readEnd) // next 'N' found - needs to send collected data
{
SetTextToTextboxes(m_serialData);//end reading send string of data to main thread
m_serialData = ""; // data sent, ready for next
m_readEnd = false;
}
}
delegate void SetTextToView(string textData);
private void SetTextToTextboxes(string textData)
{
if (this.InvokeRequired)
{
SetTextToView tempDelegate = new SetTextToView(SetTextToTextboxes);
this.Invoke(tempDelegate, new object[] { textData });
}
else
{
//textData = "55,43,23,12,32,22,12,44,"; // example string - i cannot get anything from my port...
// now string contains only numbers and commas
string[] separatedData = textData.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // now string contains only numbers and commas
if (separatedData.Length == 8)
{
this.Text = "Data Ok";
textBox1.Text = separatedData[0]; // this is more optimal for this task
textBox2.Text = separatedData[1]; // instead common solution
textBox3.Text = separatedData[2]; // to set all needed data in one delegate
textBox4.Text = separatedData[3];
textBox5.Text = separatedData[4];
textBox6.Text = separatedData[5];
textBox7.Text = separatedData[6];
textBox8.Text = separatedData[7];
}
else
{
this.Text = "Data error";
}
}
}
I have commented important places, so it will (may ) help you to implement the idea in your progam.
Dave ChikaPosted Sep 2, 2009, 3:10 PM
i must say that am very happy with the attention ur givin me,i dnt knw how to say thank you.
u asked wat i will do with the other stream of data.okay let me give you more info about my project.
i will be measuring 8 temperature levels via 8 sensors(8 #Lm35) which is converted to digital signal using 8-channels ADC0808 chip(Analogue To Digital Converter),then fed to the microcontroller (89S52) that now does the conversion to Decimals(ASCii).The micocontroller now transmits the converted data to the serial port thereby displaying those values on 8 textboxes,it should also be updating those values on the textbox based on what the temperature level from the sensors. (Realtime)
i used hyperterminal to view the incoming data from the 89S52 which shows these stream of datas continuosly (i.e N44,34,12,54,32,11,23,43,N34,51,50,40,30,22,31,00,N05,02,03,07,09,08,18,16,N19.(the N signifies start of new reading)) the 89S52 continously supplies data about the cuurent temperature value to the serialport(GUI) so that it will continously be updating those textboxes.
The textboxes are updated (Realtime)as long as there is incoming data on the serial port.
These data which are displayed is also used to populate an Access Database for reference or storage purposes.
Danatas GerviPosted Sep 2, 2009, 2:55 AM
So, "N" is also separator. It is a surprise for me. :-)
This code:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string serialData = "";
//while (serialPort1.BytesToRead> 0)
//{
// serialData += (char)serialPort1.ReadChar();
//}
serialData = "N55,43,23,12,32,22,12,44,N55,43,23,12,32,22,12,44,N55,43,23,12,32,22,12,44"; // example string - i cannot get anything from my port...
string[] dataRows = serialData.Split(new char [] {'N'}, StringSplitOptions.RemoveEmptyEntries);
if (dataRows.Length > 0)
{
string[] separatedData = dataRows[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // only first row wil be displayed...
if (separatedData.Length == 8)// data validation
{
SetText(textBox1, separatedData[0]);// this is not best kind of binding
SetText(textBox2, separatedData[1]);// but will work
SetText(textBox3, separatedData[2]);
SetText(textBox4, separatedData[3]);
SetText(textBox5, separatedData[4]);
SetText(textBox6, separatedData[5]);
SetText(textBox7, separatedData[6]);
SetText(textBox8, separatedData[7]);
}
else
{
SetText(this, "Error in received data"); // data is not valid
}
}
}
can show only first row in received data.
I do not (cannot guess) know, what you want to do with other rows (we have only 8 textboxes on form)….
May be you want to show stream of real time data - changes every second or something.
Dave ChikaPosted Sep 1, 2009, 4:02 PM
thank you very much for the code but it gives me "Error in received data" when i plug my device.but if run it without it displays fine,i dnt know where the problem is.the data from the microcontroller comes like this N22,11,23,34,54,44,22,13,N55,34,22etc the N shows the begining of a new reading.i tryed increasing my baudrate but is nt working.
Danatas GerviPosted Aug 31, 2009, 12:07 PM
You problem is that event of serial port rises in other thread.
Here is example:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string serialData = "";
//while (serialPort1.BytesToRead> 0)
//{
// serialData += (char)serialPort1.ReadChar();
//}
serialData = "N55,43,23,12,32,22,12,44"; // example string - i cannot get anything from my port...
string [] separatedData = serialData.Split(','); // may be here you needs more smart splitting
if (separatedData.Length == 8)// data validation
{
SetText(textBox1, separatedData[0]);// this is not best kind of binding
SetText(textBox2, separatedData[1]);// but will work
SetText(textBox3, separatedData[2]);
SetText(textBox4, separatedData[3]);
SetText(textBox5, separatedData[4]);
SetText(textBox6, separatedData[5]);
SetText(textBox7, separatedData[6]);
SetText(textBox8, separatedData[7]);
}
else
{
SetText(this, "Error in received data"); // data is not valid
}
}
delegate void SetTextToControl(Control viewControl, string textData);
private void SetText(Control viewControl, string textData)
{
if (viewControl.InvokeRequired)
{
SetTextToControl tempDelegate = new SetTextToControl(SetText);
viewControl.Invoke(tempDelegate, new object[] { viewControl, textData });
}
else
{
viewControl.Text = textData;
}
}
Dave ChikaPosted Aug 31, 2009, 8:25 AM
[code]
#include
sbit start=P3^3;
sbit end=P3^4;
sbit oe=P3^5;
sbit ale=P3^6;
sbit clock=P3^2;
sbit relay1=P1^3;
sbit relay2=P1^4;
sbit relay3=P1^5;
sbit relay4=P1^6;
unsigned char temp;
unsigned char aray[]={'0','1','2','3','4','5','6','7','8','9'};
void delay()
{
int k,l;
for(k=0;k<40;k++)
for(l=0;l<80;l++);
}
void initSerial()
{
TMOD=0x22;
TH1=0xFD;
SCON=0x50;
TR1=1;
EA=1;
ES=1;
}
void usDelay(int a)
{
TH0=256-(a/1.085);
TR0=1;
ET0=1;
}
void timerRoutine() interrupt 1
{
clock=~clock;
}
void sendChar(unsigned char ch)
{
SBUF=ch;
while(!TI);
TI=0;
}
void serialRoutine() interrupt 4
{
if(RI==1)
{
temp=SBUF;
switch(temp)
{
case('a'):
relay1=1;
break;
case('b'):
relay1=0;
break;
case('c'):
relay2=1;
break;
case('d'):
relay2=0;
break;
case('e'):
relay3=1;
break;
case('f'):
relay3=0;
break;
case('g'):
relay4=1;
break;
case('h'):
relay4=0;
}
RI=0;
}
}
void sendReading(unsigned char val)
{
unsigned char l,m;
l=val%10;
m=(val/10)%10;
sendChar(aray[m]);
sendChar(aray[l]);
sendChar(',');
}
void latch()
{
ale=0;
delay();
ale=1;
}
void startConv()
{
delay();
start=0;
delay();
start=1;
}
void wait()
{
while(end==1);
}
void get()
{
delay();
oe=0;
delay();
oe=1;
}
void main()
{
unsigned char sensor=0,reading;
P1=0;
initSerial();
usDelay(50);
while(1)
{
if(sensor==8)
sensor=0;
if(sensor==0)
sendChar('N');
P1=(P1&0xf8)+sensor;
latch();
startConv();
wait();
get();
reading=P2;
sendReading(reading);
sensor++;
}
}
[/code]
my problem is,afta creating an instance of serial port,uner the DataReceived event Handler,i could receive the data from the 89s52 ASCII e.g N55,43,23,12,32,22,12,N32,41,25,51,etc.so i want each of the 8 data to be displayed on 8 textboxes intead of one.
my GUI design has 8 textboxes to display these data
Danatas GerviPosted Aug 30, 2009, 6:30 AM
This data should be received as byte array (byte [] mcResivedData) and
converted to string by following code:
string resultString = "";
byte[] mcResivedData = { 55, 23, 12, 34, 42, 33, 22, 23, 55, 23, 12, 34, 42, 33, 22, 23 };
foreach (byte rByte in mcResivedData)
{
// here you can add any checking and logic - how you want to se (or dont) any byte with spesific value
resultString += (char)rByte;
}
this.Text = resultString;
*if amount of data is big (more than 1000 bytes at onse) better use StringBuilder instead string.