Answered by:
Serial Port

Question
-
Hi my name is Alan.
This is my program that must read from serial port. I solve the problem of threading with the instruction Application.DoEvents(); that instruction permit to the program don't block the principal thread without difficult instructions. but when i do the parsing of the message (byte[] messaggio = new byte[51];) the user interface visualize only zeroes, this is the problem. i create message with 51 positions because sometimes the bytes read on serial port exced 41 bytes as must be the most width of the message.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace prova2 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form1.handshake = HandSh.Text; Form1.baudRate = BaudR.Text; Form1.dataBitss = DataB.Text; Form1.parits = parita.Text; Form1.nomeporta = resource1.Text; Form1.StopBitss = StopB.Text; this.Hide(); new Form1().Show(); } private void button2_Click(object sender, EventArgs e) { Application.Exit(); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.Threading; using System.IO; namespace prova2 { public partial class Form1 : Form { public static bool continua = true; public static int byteritornati = 0; public static byte[] messaggio = new byte[51]; public static SerialPort portaseriale; public static int bytenelbuffer; public static Thread threadLettura; public static bool GpsLetto; public static string baudRate; public static string parits; public static string handshake; public static string dataBitss; public static string nomeporta; public static string StopBitss; public static byte checks(byte[] mex) { int lunghezza = 0; byte ch=0; lunghezza = mex.Length - 2; ch = mex[0]; for (int i = 1; i == lunghezza; i++) { ch = (byte)(ch ^ mex[i]); } return ch; } public static void Read() { while (continua) { try { int i = 0; int j; for (j = 0; j == 50; j++) { messaggio[j] = 0; } byte byteletto = 0; GpsLetto = true; bytenelbuffer = portaseriale.BytesToRead; if (bytenelbuffer > 0) { while ((byte)portaseriale.ReadByte() != 0x80); messaggio[0] = 128; i++; while (GpsLetto) { byteletto = (byte)portaseriale.ReadByte(); if (byteletto == 0x80) { i = 0; } messaggio[i] = (byteletto); if (byteletto == 129) { GpsLetto = false; byteritornati = i; } i++; } } } catch (TimeoutException) { MessageBox.Show("é scaduto il TimeOut"); } catch (InvalidOperationException) { MessageBox.Show("Operazione Invalida Eseguita"); } catch (ArgumentNullException) { MessageBox.Show("Argomento Nullo"); } catch (ArgumentOutOfRangeException) { MessageBox.Show("Argomento fuori dall'intervallo"); } catch (ArgumentException) { MessageBox.Show("Errore nell'argomento"); } } } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; button3.Enabled = true; byte[] GPS_Data2 = new byte[41]; byte[] GPS_Data_Extra2 = new byte[25]; byte[] Sensor_Data2 = new byte[15]; byte Anno = 0; byte Mese = 0; byte Giorno = 0; byte Ora = 0; byte Minuti = 0; byte Secondi = 0; byte Fix = 0; byte SatV = 0; byte SatU = 0; byte Pdop = 0; byte Hdop = 0; byte Vdop = 0; float Lat = 0; float Long = 0; float Geo = 0; float Dir = 0; float Vel = 0; float Altezza = 0; ushort Odo = 0; float Gyro = 0; byte Decimi = 0; byte[] lat1 = new byte[4]; byte[] long1 = new byte[4]; byte[] geo1 = new byte[4]; byte[] dir1 = new byte[4]; byte[] vel1 = new byte[4]; byte[] altezza1 = new byte[4]; byte[] odo1 = new byte[2]; byte[] gyro1 = new byte[4]; int dataBits = int.Parse(dataBitss); int bR = int.Parse(baudRate); portaseriale = new SerialPort(nomeporta, bR, (Parity)Enum.Parse(typeof(Parity), parits), dataBits, (StopBits)Enum.Parse(typeof(StopBits), StopBitss)); //portaseriale.Handshake = (Handshake)Enum.Parse(typeof(Handshake), handshake); try { portaseriale.WriteTimeout = 500; portaseriale.ReadTimeout = 2000; portaseriale.Open(); } catch (ArgumentOutOfRangeException) { MessageBox.Show("apertura1"); } catch (UnauthorizedAccessException) { MessageBox.Show("apertura2"); } catch (ArgumentException) { MessageBox.Show("apertura3"); } catch (InvalidOperationException) { MessageBox.Show("apertura4"); } catch (IOException) { } byte[] GPS_Data = new byte[6]; byte[] GPS_Data_Extra = new byte[6]; byte[] Sensor_Data = new byte[6]; /*Sensor_Data[0] = 128; Sensor_Data[1] = 6; Sensor_Data[2] = 1; Sensor_Data[3] = 3; Sensor_Data[4] = 132; Sensor_Data[5] = 129; GPS_Data_Extra[0] = 128; GPS_Data_Extra[1] = 6; GPS_Data_Extra[2] = 1; GPS_Data_Extra[3] = 2; GPS_Data_Extra[4] = 133; GPS_Data_Extra[5] = 129;*/ GPS_Data[0] = 128; GPS_Data[1] = 6; GPS_Data[2] = 1; GPS_Data[3] = 1; GPS_Data[4] = 134; GPS_Data[5] = 129; try { portaseriale.Write(GPS_Data, 0, 6); Thread.Sleep(500); portaseriale.Write(GPS_Data_Extra, 0, 6); Thread.Sleep(500); portaseriale.Write(Sensor_Data, 0, 6); Thread.Sleep(500); } catch (InvalidOperationException) { } catch (ArgumentNullException) { } catch (ArgumentOutOfRangeException) { } catch (ArgumentException) { } catch (TimeoutException) { } threadLettura = new Thread(Read); try { threadLettura.Start(); //threadLettura.IsBackground(); } catch (ThreadStateException) { } catch (OutOfMemoryException) { } while (continua) { //string mexes; //mexes = System.Text.ASCIIEncoding.ASCII.GetString(messaggio); lung.Text = byteritornati.ToString(); messer.Text = System.Text.ASCIIEncoding.ASCII.GetString(messaggio); Application.DoEvents(); if (byteritornati == 40) { for (int i = 0; i == 40; i++) { GPS_Data2[i] = messaggio[i]; } byte checkschecksum1 = checks(GPS_Data2); if (GPS_Data2[39] == checksum1) { lat1[0] = GPS_Data2[3]; lat1[1] = GPS_Data2[4]; lat1[2] = GPS_Data2[5]; lat1[3] = GPS_Data2[6]; Lat = BitConverter.ToSingle(lat1, 0); if (Lat != 0) { lati.Text = Lat.ToString(); } long1[0] = GPS_Data2[7]; long1[1] = GPS_Data2[8]; long1[2] = GPS_Data2[9]; long1[3] = GPS_Data2[10]; Long = BitConverter.ToSingle(long1, 0); if (Long != 0) { longi.Text = Long.ToString(); } altezza1[0] = GPS_Data2[11]; altezza1[1] = GPS_Data2[12]; altezza1[2] = GPS_Data2[13]; altezza1[3] = GPS_Data2[14]; Altezza = BitConverter.ToSingle(altezza1, 0); if (Altezza != 0) { alte.Text = Altezza.ToString(); } geo1[0] = GPS_Data2[24]; geo1[1] = GPS_Data2[25]; geo1[2] = GPS_Data2[26]; geo1[3] = GPS_Data2[27]; Geo = BitConverter.ToSingle(geo1, 0); geoi.Text = Geo.ToString(); dir1[0] = GPS_Data2[28]; dir1[1] = GPS_Data2[29]; dir1[2] = GPS_Data2[30]; dir1[3] = GPS_Data2[31]; Dir = BitConverter.ToSingle(dir1, 0); dire.Text = Dir.ToString(); vel1[0] = GPS_Data2[32]; vel1[1] = GPS_Data2[33]; vel1[2] = GPS_Data2[34]; vel1[3] = GPS_Data2[35]; Vel = BitConverter.ToSingle(vel1, 0); velo.Text = Vel.ToString(); Anno = GPS_Data2[15]; year.Text = Anno.ToString(); Mese = GPS_Data2[16]; month.Text = Mese.ToString(); Giorno = GPS_Data2[17]; day.Text = Giorno.ToString(); Ora = GPS_Data2[18]; hour.Text = Ora.ToString(); Minuti = GPS_Data2[19]; minute.Text = Minuti.ToString(); Secondi = GPS_Data2[20]; second.Text = Secondi.ToString(); Fix = GPS_Data2[21]; efi.Text = Fix.ToString(); SatV = GPS_Data2[22]; sat1.Text = SatV.ToString(); SatU = GPS_Data2[23]; sat2.Text = SatU.ToString(); Pdop = GPS_Data2[36]; pdo.Text = Pdop.ToString(); Hdop = GPS_Data2[37]; hdo.Text = Hdop.ToString(); Vdop = GPS_Data2[38]; vdo.Text = Vdop.ToString(); } } else if (byteritornati == 24) { for (int i = 0; i == 24; i++) { GPS_Data_Extra2[i] = messaggio[i]; } byte checkschecksum2 = checks(GPS_Data_Extra2); if (GPS_Data_Extra2[23] == checksum2) { lat1[0] = GPS_Data_Extra2[3]; lat1[1] = GPS_Data_Extra2[4]; lat1[2] = GPS_Data_Extra2[5]; lat1[3] = GPS_Data_Extra2[6]; Lat = BitConverter.ToSingle(lat1, 0); lati.Text = (Lat.ToString() + " Extra"); long1[0] = GPS_Data_Extra2[7]; long1[1] = GPS_Data_Extra2[8]; long1[2] = GPS_Data_Extra2[9]; long1[3] = GPS_Data_Extra2[10]; Long = BitConverter.ToSingle(long1, 0); longi.Text = (Long.ToString() + " Extra"); dir1[0] = GPS_Data_Extra2[11]; dir1[1] = GPS_Data_Extra2[12]; dir1[2] = GPS_Data_Extra2[13]; dir1[3] = GPS_Data_Extra2[14]; Dir = BitConverter.ToSingle(altezza1, 0); dire.Text = (Dir.ToString() + " Extra"); vel1[0] = GPS_Data_Extra2[15]; vel1[1] = GPS_Data_Extra2[16]; vel1[2] = GPS_Data_Extra2[17]; vel1[3] = GPS_Data_Extra2[18]; Vel = BitConverter.ToSingle(vel1, 0); velo.Text = (Vel.ToString() + " Extra"); Ora = GPS_Data_Extra2[19]; hour.Text = (Ora.ToString() + " Extra"); Minuti = GPS_Data_Extra2[20]; minute.Text = (Minuti.ToString() + " Extra"); Secondi = GPS_Data_Extra2[21]; second.Text = (Secondi.ToString() + " Extra"); Decimi = GPS_Data_Extra2[22]; deci.Text = (Decimi.ToString() + " Extra"); } } else if (byteritornati == 14) { for (int i = 0; i == 14; i++) { Sensor_Data2[i] = messaggio[i]; } byte checkschecksum3 = checks(Sensor_Data2); if (Sensor_Data2[13] == checksum3) { odo1[0] = Sensor_Data2[3]; odo1[1] = Sensor_Data2[4]; Odo = BitConverter.ToUInt16(odo1, 0); odom.Text = (Odo.ToString() + " Sensor"); gyro1[0] = Sensor_Data2[5]; gyro1[1] = Sensor_Data2[6]; gyro1[2] = Sensor_Data2[7]; gyro1[3] = Sensor_Data2[8]; Gyro = BitConverter.ToSingle(gyro1, 0); giro.Text = (Gyro.ToString() + " Sensor"); Ora = Sensor_Data2[9]; hour.Text = (Ora.ToString() + " Sensor"); Minuti = Sensor_Data2[10]; minute.Text = (Minuti.ToString() + " Sensor"); Secondi = Sensor_Data2[11]; second.Text = (Secondi.ToString() + " Sensor"); Decimi = Sensor_Data2[12]; deci.Text = (Decimi.ToString() + " Sensor"); } } } } private void button2_Click(object sender, EventArgs e) { continua = false; Application.Exit(); } private void lung_TextChanged(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { byte[] CloseAll = new byte[6]; button1.Enabled = true; button3.Enabled = false; CloseAll[0] = 128; CloseAll[1] = 6; CloseAll[2] = 1; CloseAll[3] = 0; CloseAll[4] = 135; CloseAll[5] = 129; try { portaseriale.Write(CloseAll, 0, 6); } catch (InvalidOperationException) { } catch (ArgumentNullException) { } catch (TimeoutException) { } continua = false; try { threadLettura.Join(); } catch (ThreadStateException) { } catch (ThreadInterruptedException) { } portaseriale.Close(); } private void Form1_Load(object sender, EventArgs e) { } } } Wednesday, October 1, 2008 2:35 PM
Answers
-
Hello Alan
Thank you for your post! Unfortunately there is not a current forum to post the question you are asking so I would recommend that you post your question to one of the Windows Embedded located here:http://msdn.microsoft.com/en-us/embedded/aa731160.aspx
Hope that helps!
Vijay Mullick Tier 2 Application Support Server and Tools Online Engineering Live Services Team- Proposed as answer by Vijay Mullick Tuesday, October 21, 2008 11:40 AM
- Marked as answer by Vijay Mullick Thursday, November 6, 2008 1:10 PM
Tuesday, October 21, 2008 11:40 AM