how do i communicate in serial

zriuz

#1 ◈ 2025-01-14

I just found out my VB course is not even going to touch the subject of serial or usb communication. is there some info or books that you guis coild point me to?.

Type-GS-R-Turbo

#2 ◈ 2025-01-14

Re: how do i communicate in serial

I've actually found lots of good information on the internet.

Are you hell bent on learning VB? Personally, I prefer C# since my roots are in C, and C is a way more respected language than Basic or Visual Basic for that matter...

The point I'm trying to make is that I can post some serial port communication code for C# that can literally be implemented in 4 lines of code.

(Of course I'm utilizing the not yet universally supported .NET framework) but Microsoft actually does a fairly decent job of pushing these frameworks via windows update, so my experience in recent time has been good)

Nowadays, Microsoft has information on their knowledgebase with examples written in Visual Basic and C# so I'm sure that if you're more confortable with Basic, you can do it just as easily.

In the .NET framework, there is a SerialPort class, so writing to a serial port (and I don't have my code in front of me, so don't try this verbatim) is done something like this:

Code:
using System.Ports.IO

SerialPort DarrensPort = new SerialPort();
byte tempByte;

DarrensPort.BaudRate = 9600;
DarrensPort.StopBits = StopBits.One; //The number of stopbits is an enumerated data type, its pretty clean actually.
DarrensPort.Parity = Parity.None; //Parity is also enumerated

DarrensPort.Open();

DarrensPort.Write("Hello"); //While you can write a string to the serial port like this, keep in mind that strings in C# have the length of the string as the first character and that will get sent down the port.

DarrensPort.Write(0x55);

tempByte = DarrensPort.ReadByte();

char[] charArray = new char[DarrensPort.bytestoRead];

charArray = DarrensPort.ReadExisting();


I can post up real code when I have it in front of me, but that's basically how it goes.

I'm just now getting into doing native USB stuff, so as soon as I have that figured out I can post some information. From what I understand, USB is A LOT different. Every device has an address, and each device has a certain number of "Endpoints" and you can have multiple channels of data within those endpoints. I haven't dug too deep but I can easily see why it is a preferable method of communication since you can have one channel designated as a data channel with the other being a command channel, rather than having to find clever ways to mix the two like on a serial port.

tunedtyper

#3 ◈ 2025-01-14

Re: how do i communicate in serial

Type-GS-R-Turbo wrote:
I've actually found lots of good information on the internet.

Are you hell bent on learning VB? Personally, I prefer C# since my roots are in C, and C is a way more respected language than Basic or Visual Basic for that matter...

The point I'm trying to make is that I can post some serial port communication code for C# that can literally be implemented in 4 lines of code.

(Of course I'm utilizing the not yet universally supported .NET framework) but Microsoft actually does a fairly decent job of pushing these frameworks via windows update, so my experience in recent time has been good)

Nowadays, Microsoft has information on their knowledgebase with examples written in Visual Basic and C# so I'm sure that if you're more confortable with Basic, you can do it just as easily.

In the .NET framework, there is a SerialPort class, so writing to a serial port (and I don't have my code in front of me, so don't try this verbatim) is done something like this:

Code:
using System.Ports.IO

SerialPort DarrensPort = new SerialPort();
byte tempByte;

DarrensPort.BaudRate = 9600;
DarrensPort.StopBits = StopBits.One; //The number of stopbits is an enumerated data type, its pretty clean actually.
DarrensPort.Parity = Parity.None; //Parity is also enumerated

DarrensPort.Open();

DarrensPort.Write("Hello"); //While you can write a string to the serial port like this, keep in mind that strings in C# have the length of the string as the first character and that will get sent down the port.

DarrensPort.Write(0x55);

tempByte = DarrensPort.ReadByte();

char[] charArray = new char[DarrensPort.bytestoRead];

charArray = DarrensPort.ReadExisting();


I can post up real code when I have it in front of me, but that's basically how it goes.

I'm just now getting into doing native USB stuff, so as soon as I have that figured out I can post some information. From what I understand, USB is A LOT different. Every device has an address, and each device has a certain number of "Endpoints" and you can have multiple channels of data within those endpoints. I haven't dug too deep but I can easily see why it is a preferable method of communication since you can have one channel designated as a data channel with the other being a command channel, rather than having to find clever ways to mix the two like on a serial port.

are you kidding VB6.0 ftw.. lol

j/k

Type-GS-R-Turbo

#4 ◈ 2025-01-14

Re: how do i communicate in serial

*slaps head*

zriuz

#5 ◈ 2025-01-14

Re: how do i communicate in serial

I'm on an intro to visual basic course now,we use VB8. In september I'll take the advance course fallowed by two C++ semesters. I'm on an electronic engineer course so i definitely need a way to communicate with my devices.

is usb supported by the serial port class?

Type-GS-R-Turbo

#6 ◈ 2025-01-14

Re: how do i communicate in serial

Quote:
is usb supported by the serial port class?


Yes and no. In the straightforward hardcore USB sense, no. A USB port and a serial port are completely different animals. However there are lots of pieces of hardware out there (Moates, hondata, EFILive I think) to name a few that use a chip by the company FTDI. (http://www.ftdichip.com)

This chip, when connected to a USB port, creates a serial port on your computer. This serial port can be interfaced just like the standard issue serial ports we all used to know and love.

This makes it easy to have a "USB" device since coding for a serial port is a stroll in the park.

This is why when you plug an Ostrich into your computer it shows up under Ports (COM and LPT) in your Device Manager.

While it is easy to code and requires little setup on your microcontroller, the downside is performance. You'll never hit the 480Mbps that USB 2.0 claims to attain. This is because on the output of the FTDI chip, you are still sending serial data at the baud rate you specified in your code. When you code for 9600bps, on your best day you'll see less than 1KB/sec.

The questions you have to ask yourself is whether speed is what you need and otherwise, are you trying to impress anyone with the project.